سؤال

There doesn't seem to be a property to change the kerning. Is the best way to get the UILabel from C4Label and then go from there?

هل كانت مفيدة؟

المحلول

Unfortunately, yes.

C4Label was originally designed when iOS5 was out, and didn't need any modifying when iOS6 was released... Prior to iOS6 the use of attributed strings was unavailable in UILabel. Since C4Label essentially wraps an instance of UILabel it, too, didn't (and still doesn't) have direct access to the use of attributed strings.

However, given C4Label wraps UILabel is possible to add kerning in the following way:

-(void)setup {
    //Create a label
    C4Label *l = [C4Label labelWithText:@"this is kerning"];
    l.numberOfLines = 2;

    //Create a key
    NSString *kernKey = (NSString *)kCTKernAttributeName;
    //Create a value
    NSNumber *kernValue = @20;
    //Create an attributed string
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
    //set the attributed string with the text, using the value as a dictionary
    attStr = [attStr initWithString:l.text attributes:@{kernKey:kernValue}];

    //set and center the text
    l.label.attributedText = attStr;
    l.textAlignment = ALIGNTEXTCENTER;

    //resize and position the frame
    l.frame = CGRectMake(0,0,320, 80);
    l.center = self.canvas.center;

    [self.canvas addLabel:l];
}

C4Label is on the list for being modernized, but hasn't been gotten to yet.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top