Cannot get a drop shadow on multiline nsstring

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.

Answers


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.


Need Your Help

Whether to use TPL or async /await

asp.net .net asynchronous task-parallel-library async-await

There is an existing third party Rest API available which would accept one set of input and return the output for the same. (Think of it as Bing's Geo coding service, which would accept address and

how to copy a masked password to another textbox and convert it to text

c# wpf textbox

I'm kinda new at this, I was just wondering if this is possible to do.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.