iOS7's deprecation of NSString's drawAtPoint:forWidth:withFont:minFontSize:actualFontSize:lineBreakMode:baselineAdjustment:
It is a little more complicated than before and you cannot use a minimum font size, but have to use minimum font scale factor. There is also a bug in the iOS SDK, which breaks it for most use cases (see notes at the bottom). Here is what you have to do:
// Create text attributesNSDictionary *textAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:18.0]};// Create string drawing contextNSStringDrawingContext *drawingContext = [[NSStringDrawingContext alloc] init];drawingContext.minimumScaleFactor = 0.5; // Half the font sizeCGRect drawRect = CGRectMake(0.0, 0.0, 200.0, 100.0);[string drawWithRect:drawRect options:NSStringDrawingUsesLineFragmentOrigin attributes:textAttributes context:drawingContext];
Notes:
There seems to be a bug in the iOS 7 SDK at least up to version 7.0.3: If you specify a custom font in the attributes, the miniumScaleFactor is ignored. If you pass nil for the attributes, the text is scaled correctly.
The
NSStringDrawingUsesLineFragmentOrigin
option is important. It tells the text drawing system, that the drawing rect's origin should be at the top left corner.There is no way to set the baselineAdjustment using the new method. You would have to do that yourself by calling
boundingRectWithSize:options:attributes:context:
first and then adjusting the rect before you pass it todrawWithRect:options:attributes:context
.
After googling for a long time I did not find a solution working under iOS7.
Right now I use the following workaround, knowing that it is very ugly.
I render a UILabel in memory, take a screenshot and draw that.
UILabel is able to shrink the text correctly.
Perhaps someone finds it useful.
UILabel *myLabel = [[UILabel alloc] initWithFrame:myLabelFrame];myLabel.font = [UIFont fontWithName:@"HelveticaNeue-BoldItalic" size:16];myLabel.text = @"Some text that is too long";myLabel.minimumScaleFactor = 0.5;myLabel.adjustsFontSizeToFitWidth = YES;myLabel.backgroundColor = [UIColor clearColor];UIGraphicsBeginImageContextWithOptions(myLabelFrame.size, NO, 0.0f);[[myLabel layer] renderInContext:UIGraphicsGetCurrentContext()];UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();[screenshot drawInRect:myLabel.frame];
just create an NS dictionary with that key and attribute so it would be
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: @"15", @"minFontSize", @"value2", @"key2", nil];//in key2 and value2 you could set any other of the attributes included in the first method[yourString drawInRect:rect withAttributes:attributes];