I'm trying to get multiline text to draw with a drop shadow without using deprecated APIs. It works fine for a single line. The relevant code looks like this:
-(void)drawRect:(CGRect)rect
{
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
paragraph.lineBreakMode = NSLineBreakByWordWrapping;
paragraph.alignment = NSTextAlignmentCenter;
UIFont *f = [UIFont systemFontOfSize:20.0];
NSMutableDictionary *attributes = [NSMutableDictionary new];
[attributes setValuesForKeysWithDictionary:@{ NSFontAttributeName : f,
NSParagraphStyleAttributeName : paragraph,
NSForegroundColorAttributeName : [UIColor blueColor] }];
NSShadow * shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(4,4);
shadow.shadowColor = [UIColor redColor];
[attributes setValue:shadow forKey:NSShadowAttributeName];
rect.origin.y = 100;
[@"test string on one line" drawInRect:rect withAttributes:attributes];
rect.origin.y = 150;
[@"test string spanning more than one line" drawInRect:rect withAttributes:attributes];
}
and the output looks like this:
I have tested this on iPhone 5 (7.1.2), iPhone 6 (8.0), building with xCode 6. I have also tested it on the iPhone 5 when building with xCode 5.
Some more experimentation, and I discovered that the answer is to use an NSAttributedString.
While this does not show a shadow:
NSString *s = @"test string spanning more than one line"
[s drawInRect:rect withAttributes:attributes]
This does:
NSAttributedString *as = [[NSAttributedString alloc] initWithString:s attributes:attributes];
[as drawInRect:rect];
I don't think this is documented anywhere, would love to hear otherwise.