这是CoreText吗? - Aviary文本捏缩放扩展 [英] is this CoreText? - Aviary text pinch zoom expand
问题描述
我正在研究制作一个用户可以更改 UITextField
的大小和方向的应用。我正在研究Aviary app,我看到的是用户不仅可以增加或减少文本的大小,还可以改变其方向。
I am looking into making an app where a user can change the size and orientation of UITextField
. I was looking into Aviary app and what I see is that users can not only increase or decrease the size of the text but they can also change its orientation.
所以我想问的问题是
1)他们是否使用 CoreText
要做到这一点,他们只使用普通的旧 UILabel
或 UIText
?
1) Are they using CoreText
to do this are they using just plain old UILabel
or UIText
?
2)我不知道如何动态调整大小?他们使用的是 UIView
吗?
2) I don't know how you can dynamically resize? Are they using UIView
?
3)我搜索过CoreText教程,示例,检查过的apple文档和示例项目,查看了cocoa控件和github,我仍然没有看到任何示例代码或教程来说明如何完成。
3) I have googled CoreText tutorials, examples, checked apple docs and sample projects, looked into cocoa controls and github and I still don't see any sample code or tutorial that will show how this is done.
是否有人能够指点我某个方向或教程?
Would anyone be kind enough to point me in some direction or a tutorial?
推荐答案
关于字体的外观,有以下几种选择:
Regarding the appearance of the font, there are a couple of options:
-
您可以使用标准
UILabel
来实现大部分效果(字体,大小,颜色,旋转)看这里。超出标准UILabel
的一个功能是字体周围的白色笔划。但是,有效的iOS 6,您还可以使用其 UILabel 来实现红色文本周围的白色笔划效果。 apple.com/library/ios/documentation/uikit/reference/UILabel_Class/Reference/UILabel.html#//apple_ref/doc/uid/TP40006797-CH3-SW55\"rel =noreferrer>attributedText
属性,这是NSAttributedString
。
You can use the standard
UILabel
to achieve the most of the effects (font, size, color, rotation) you see here. The one feature that is beyond the standardUILabel
is the white stroke around the font. But, effective iOS 6, you can also achieve the effect of the white stroke around the red text with a standardUILabel
using itsattributedText
property, which is aNSAttributedString
.
在以前的iOS版本中到6.0,要实现文本周围的白色笔触颜色,你必须使用 CoreGraphics 或 CoreText 。
In iOS versions prior to 6.0, to achieve the white stroke color around the text, you had to use CoreGraphics or CoreText.
关于动态调整大小,旋转和移动文本,毫无疑问,只需使用手势识别器来调整视图的转换
属性(更准确地说,可能会调整 transform
进行旋转,调整中心
或框架
进行拖动,并调整框架
和调整大小的字体大小(如果你只是使用 scale
转换,你最终可能会出现不需要的像素化)。
Regarding the dynamic resizing, rotating, and moving of the text, they're undoubtedly just using gesture recognizers that adjust the transform
property of the view (more accurately, probably adjusting transform
for rotation, adjusting the center
or frame
for dragging, and adjusting both the frame
and font size for resizing (if you just use scale
transformation, you can end up with undesirable pixelation).
如果您在Stack Overflow中搜索有关手势识别器以及拖动,调整大小和旋转视图的问题,那么您将获得相当多的好评。
You'll get quite a few good hits if you search Stack Overflow for questions regarding gesture recognizers and dragging, resizing, and rotating of views.
在iOS 6中,如果你想要带有白色边框的红色文字,带有 UILabel
,你可以这样做:
In iOS 6, if you want the red text with a white border with a UILabel
, you could do something like:
// 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中属性字符串的信息,请参阅:
For information about attributed strings in iOS, see:
WWDC 2012 - #230 - 适用于iOS的高级归属字符串
如果您需要支持iOS 6之前的iOS版本,则必须使用CoreText或CoreGraphics。 CoreText再现可能如下所示:
If you need to support iOS versions prior to iOS 6, you have to use CoreText or CoreGraphics. A CoreText rendition might look like:
- (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演示。
这是一个非常简单的 drawRect
的核心图形实现,它在文本周围写下大纲:
Here is a very simple Core Graphics implementation of drawRect
that writes text with a outline around the text:
@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
这篇关于这是CoreText吗? - Aviary文本捏缩放扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!