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(または)をレイヤーに割り当て、属性(コアテキスト文字列属性リファレンスを参照)を使用してカーニングを調整できます。NSMutableAttributedStringNSStringkCTKernAttributeName

例:

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 に答える