我正在使用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);
}
答案 0 :(得分:12)
您可以为图层分配NSAttributedString
(或NSMutableAttributedString
)而不是普通NSString
,并使用kCTKernAttributeName
属性(请参阅Core Text String Attributes Reference)调整字距调整。
示例:
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中为您提供增加字符间距的绿色文本。