在 PaintCode CGContext 的标签中自动调整文本大小



我正在使用以下内容在贝塞尔路径中绘制文本。如何调整它以允许文本自动调整大小。

编辑

我能够更新到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);

尝试使用以下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.

相关内容

  • 没有找到相关文章