这是CoreText吗? - 鸟舍文字捏缩放扩展

Sam*_*m B 6 iphone uitextfield core-text ios

我正在研究制作一个用户可以改变其大小和方向的应用程序UITextField.我正在研究Aviary app,我看到的是用户不仅可以增加或减少文本的大小,还可以改变其方向.

在此输入图像描述

所以我想问的问题是

1)他们使用CoreText这个他们使用只是普通的旧办UILabelUIText

2)我不知道你怎么能动态调整大小?他们在用UIView吗?

3)我用谷歌搜索了CoreText教程,示例,检查了苹果文档和示例项目,查看了cocoa控件和github,我仍然没有看到任何示例代码或教程来说明这是如何完成的.

有人能够指点我某个方向或教程吗?

Rob*_*Rob 7

关于字体的外观,有几个选项:

  1. 您可以使用标准UILabel来实现您在此处看到的大部分效果(字体,大小,颜色,旋转).超出标准的一个特征是UILabel字体周围的白色笔划.但是,有效的iOS 6,你还可以实现红色文本周围白色笔划的效果,UILabel使用其attributedText属性标准,这是一个NSAttributedString.

  2. 在6.0之前的iOS版本中,要在文本周围实现白色笔触颜色,您必须使用CoreGraphicsCoreText.

关于动态调整大小,旋转和移动文本,它们无疑只是使用手势识别器来调整transform视图的属性(更准确地说,可能调整transform旋转,调整centerframe拖动,以及调整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;
Run Code Online (Sandbox Code Playgroud)

有关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);
}
Run Code Online (Sandbox Code Playgroud)

在我的GitHub CoreText演示中可以找到更完整的上述实现.


这是一个非常简单的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
Run Code Online (Sandbox Code Playgroud)