1

I'm using the following to draw text inside a Bezier Path. How can i adjust this to allow the text to autosize.

EDIT

I was able to update to iOS7 methods but still nothing. I can autosize text within a UILabel fine, but because this is CGContext it is harder

        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);
Tricertops
  • 8,492
  • 1
  • 39
  • 41
DaynaJuliana
  • 1,144
  • 1
  • 14
  • 33

1 Answers1

0

Try using this method of NSAttributedString:

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

Where the context will provide you actualScaleFactor.

The usage is something like this:

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.
Tricertops
  • 8,492
  • 1
  • 39
  • 41