8

我正在使用 CATextLayer,为了在 iOS 中使用自定义字体,我知道有一种简单的方法可以使用自定义字体,Fonts provided by application但这是不同的字体。我想知道有没有办法改变每个字符之间的间距?我没有找到任何财产这样做!

编辑:

- (void)viewWillAppear:(BOOL)animated {
    CTFontRef font = [self newCustomFontWithName:@"yeki" 
                                          ofType:@"ttf" 
                                      attributes:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:16.f] 
                                                                             forKey:(NSString *)kCTFontSizeAttribute]];


    CGRect screenBounds = [[UIScreen mainScreen] bounds];

    normalTextLayer_ = [[CATextLayer alloc] init];
    normalTextLayer_.font = font;
    normalTextLayer_.string = str;
    normalTextLayer_.wrapped = YES;
    normalTextLayer_.foregroundColor = [[UIColor purpleColor] CGColor];
    normalTextLayer_.fontSize = 50.f;
    normalTextLayer_.alignmentMode =  kCAAlignmentRight;

    normalTextLayer_.frame = CGRectMake(0.f,100.f, screenBounds.size.width, screenBounds.size.height /1.f);
    [self.view.layer addSublayer:normalTextLayer_];
    CFRelease(font);
}
4

1 回答 1

12

您可以为图层分配NSAttributedString(or NSMutableAttributedString) 而不是纯文本NSString,并使用kCTKernAttributeName属性(请参阅核心文本字符串属性参考)来调整字距。

例子:

CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica"), 30, NULL);
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                            (id)font, kCTFontAttributeName, 
                            [NSNumber numberWithFloat:15.0], kCTKernAttributeName, 
                            (id)[[UIColor greenColor] CGColor], kCTForegroundColorAttributeName, nil];
NSAttributedString *attributedString = [[[NSAttributedString alloc] initWithString:@"Hello World" attributes:attributes] autorelease];
CFRelease(font);
myTextLayer.string = attributedString;

这应该会在 Helvetica 30 中为您提供绿色文本,并增加字符间距。

于 2011-09-04T19:03:36.353 回答