关于字体的外观,有几个选项:
您可以使用标准UILabel
实现您在此处看到的大部分效果(字体、大小、颜色、旋转)。超越标准的一项功能UILabel
是字体周围的白色笔划。但是,有效的iOS 6,你也可以用标准的方式实现红色文字周围白色描边的效果UILabel
使用它的attributedText http://developer.apple.com/library/ios/documentation/uikit/reference/UILabel_Class/Reference/UILabel.html#//apple_ref/doc/uid/TP40006797-CH3-SW55属性,这是一个NSAttributedString https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/Reference/Reference.html.
在 6.0 之前的 iOS 版本中,要实现文本周围的白色描边颜色,您必须使用核心图形 http://developer.apple.com/library/ios/#documentation/CoreGraphics/Reference/CoreGraphics_Framework/_index.html or CoreText https://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/CoreText_Programming/Introduction/Introduction.html.
关于文本的动态调整大小、旋转和移动,他们无疑只是使用手势识别器来调整文本transform
视图的属性(更准确地说,可能调整transform
对于旋转,调整center
or frame
用于拖动和调整frame
和用于调整大小的字体大小(如果您只是使用scale
转换,最终可能会出现不希望的像素化)。
如果您在 Stack Overflow 上搜索有关手势识别器以及视图拖动、调整大小和旋转的问题,您会得到很多不错的结果。
在 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 演示 https://github.com/robertmryan/CoreText-Demonstration.
这是一个非常简单的 Core Graphics 实现drawRect
编写文本并在文本周围添加轮廓:
@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