我正在研究创建一个用户可以更改UITextField
的大小和方向的应用。我正在研究Aviary app,我看到的是用户不仅可以增加或减少文本的大小,还可以改变其方向。
所以我想问的问题是
1)他们是否使用CoreText
来执行此操作是因为他们只使用普通的UILabel
或UIText
?
2)我不知道你怎么能动态调整大小?他们使用的是UIView
吗?
有人会善意指点我某个方向或教程吗?
答案 0 :(得分:7)
关于字体的外观,有几个选项:
您可以使用标准UILabel
来实现您在此处看到的大部分效果(字体,大小,颜色,旋转)。超出标准UILabel
的一个特征是字体周围的白色笔划。但是,有效的iOS 6,您还可以使用attributedText
属性NSAttributedString
来标准UILabel
来实现红色文本周围的白色笔划效果。
在6.0之前的iOS版本中,为了在文字周围获得白色笔触颜色,您必须使用CoreGraphics或CoreText。
关于动态调整大小,旋转和移动文本,他们无疑只是使用手势识别器来调整视图的transform
属性(更准确地说,可能会调整transform
进行旋转,调整center
或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 Demonstration中找到更完整的上述实现。
这是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