フォントの外観に関しては、いくつかのオプションがあります。
標準UILabel
を使用して、ここに表示されているほとんどの効果 (フォント、サイズ、色、回転) を実現できます。標準を超えている 1 つの機能はUILabel
、フォントの周りの白いストロークです。しかし、有効な iOS 6 では、UILabel
そのattributedText
プロパティ ( NSAttributedString
.
6.0 より前の iOS バージョンでは、テキストの周りに白い線の色を実現するには、CoreGraphicsまたはCoreTextを使用する必要がありました。
テキストの動的なサイズ変更、回転、および移動に関してはtransform
、ビューのプロパティを調整するジェスチャ認識機能を使用していることは間違いありません (より正確には、おそらくtransform
回転の調整、center
またはframe
のドラッグの調整、および と の両方のframe
フォント サイズの調整)。サイズ変更用 (scale
変換のみを使用すると、望ましくないピクセレーションが発生する可能性があります)。
スタック オーバーフローでジェスチャ レコグナイザーや、ビューのドラッグ、サイズ変更、回転に関する質問を検索すると、かなりのヒットが得られます。
iOS 6 では、赤いテキストにUILabel
.
// create attributes dictionary
NSDictionary *attributes = @{
NSFontAttributeName : [UIFont systemFontOfSize:48.0],
NSForegroundColorAttributeName : [UIColor redColor],
NSStrokeColorAttributeName : [UIColor whiteColor],
NSStrokeWidthAttributeName : @(-3)
};
// make the attributed string
NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:@"bigcat"
attributes:attributes];
self.label.attributedText = stringToDraw;
iOS の属性付き文字列については、次を参照してください。
iOS 6 より前のバージョンの iOS をサポートする必要がある場合は、CoreText または CoreGraphics を使用する必要があります。CoreText レンディションは次のようになります。
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
if (!self.text)
return;
// create a font
CTFontRef sysUIFont = CTFontCreateUIFontForLanguage(kCTFontSystemFontType, self.fontSize, NULL);
// create attributes dictionary
NSDictionary *attributes = @{
(__bridge id)kCTFontAttributeName : (__bridge id)sysUIFont,
(__bridge id)kCTForegroundColorAttributeName : (__bridge id)[self.fillColor CGColor],
(__bridge id)kCTStrokeColorAttributeName : (__bridge id)[self.borderColor CGColor],
(__bridge id)kCTStrokeWidthAttributeName : @(-3)
};
// make the attributed string
NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:self.text
attributes:attributes];
// begin drawing
CGContextRef context = UIGraphicsGetCurrentContext();
// flip the coordinate system
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// create CTLineRef
CTLineRef line = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)stringToDraw);
// figure out the size (which we'll use to center it)
CGFloat ascent;
CGFloat descent;
CGFloat width = CTLineGetTypographicBounds(line, &ascent, &descent, NULL);
CGFloat height = ascent + descent;
CGSize stringSize = CGSizeMake(width, height);
// draw it
CGContextSetTextPosition(context,
(self.bounds.size.width - stringSize.width) / 2.0,
(self.bounds.size.height - stringSize.height + descent) / 2.0);
CTLineDraw(line, context);
// clean up
CFRelease(line);
CFRelease(sysUIFont);
}
上記のより完全な実装は、私のGitHub CoreText Demonstrationにあります。
drawRect
これは、テキストの周りにアウトラインを付けてテキストを書き込む、非常に単純な Core Graphics の実装です。
@implementation CustomView
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
if (!self.text)
return;
// begin drawing
CGContextRef context = UIGraphicsGetCurrentContext();
// flip the coordinate system
CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(M_PI_4 / 2.0));
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// write the text
CGContextSelectFont (context, "Helvetica", self.fontSize, kCGEncodingMacRoman);
CGContextSetTextDrawingMode (context, kCGTextFillStroke);
CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextSetLineWidth(context, 1.5);
CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextShowTextAtPoint (context, 40, 100, [self.text UTF8String], [self.text length]);
}
@end