1

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

编辑

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

1 回答 1

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.
于 2015-04-20T09:46:54.307 回答