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

时间:2013-04-16 13:23:53

标签: iphone ios uitextfield core-text

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

enter image description here

所以我想问的问题是

1)他们是否使用CoreText来执行此操作是因为他们只使用普通的UILabelUIText

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

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

有人会善意指点我某个方向或教程吗?

1 个答案:

答案 0 :(得分:7)

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

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

  2. 在6.0之前的iOS版本中,为了在文字周围获得白色笔触颜色,您必须使用CoreGraphicsCoreText

  3. 关于动态调整大小,旋转和移动文本,他们无疑只是使用手势识别器来调整视图的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;
    

    有关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