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 に答える