为PaintCode CGContext自动调整标签中的文本

时间:2015-04-09 22:34:24

标签: cocoa-touch uilabel paint-code

我正在使用以下内容在Bezier路径中绘制文本。如何调整此选项以允许文本自动调整大小。

修改

我能够更新到iOS7方法,但仍然没有。我可以在UILabel中自动调整文本,但因为这是CGContext更难

        NSString* textContent = @"LOCATION";

        NSMutableParagraphStyle* locationStyle = NSMutableParagraphStyle.defaultParagraphStyle.mutableCopy;
        locationStyle.alignment = NSTextAlignmentCenter;

        NSDictionary* locationFontAttributes = @{NSFontAttributeName: [UIFont fontWithName:myFont size: 19], NSForegroundColorAttributeName: locationColor, NSParagraphStyleAttributeName: locationStyle};
        CGFloat locationTextHeight = [textContent boundingRectWithSize: CGSizeMake(locationRect.size.width, INFINITY)  options: NSStringDrawingUsesLineFragmentOrigin attributes: locationFontAttributes context: nil].size.height;
        CGContextSaveGState(context);
        CGContextClipToRect(context, locationRect);
        [textContent drawInRect: CGRectMake(CGRectGetMinX(locationRect), CGRectGetMinY(locationRect) + (CGRectGetHeight(locationRect) - locationTextHeight) / 2, CGRectGetWidth(locationRect), locationTextHeight) withAttributes: locationFontAttributes];
        CGContextRestoreGState(context);

1 个答案:

答案 0 :(得分:0)

尝试使用NSAttributedString的这种方法:

- (CGRect)boundingRectWithSize:(CGSize)size
                       options:(NSStringDrawingOptions)options
                       context:(NSStringDrawingContext *)context;

context将为您提供actualScaleFactor

用法是这样的:

NSAttributedString *string = ...;
NSStringDrawingContext *context = [NSStringDrawingContext new];
context.minimumScaleFactor = 0.5; // Set your minimum value.

CGRect bounds = [string boundingRectWithSize:maxSize
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                     context:context];
CGFloat scale = context. actualScaleFactor;
// Use this scale to multiply font sizes in the string, so it will fit.