mbm29414
mbm29414

Reputation: 11598

CATextLayer ignores NSShadow?

I have a custom UIView that I've created that has several custom UIControl instances which are non-rectangular buttons (instead of using UIButton instances). I am doing this because I need to be able to draw non-rectangular buttons and have them respond to taps only in their non-rectangular regions.

Because UIControl doesn't come with a UILabel, I am using a CATextLayer to add in this functionality. I'd like the text I am drawing (by setting layer.string equal to an NSAttributedString) to have a shadow applied to it, but it appears that CATextLayer ignores any NSShadow attributes.

I can't find any documentation to tell me either way, but in practice, the following does not work:

NSMutableParagraphStyle *s  = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
s.lineSpacing               = 0.9f;
NSShadow *shadow            = [[NSShadow alloc] init];
shadow.shadowColor          = [UIColor orangeColor];
shadow.shadowOffset         = CGSizeMake(10,10);    
NSDictionary *textAttrs     = @{NSFontAttributeName           : [UIFont someFont],
                                NSParagraphStyleAttributeName : [s copy],
                                NSShadowAttributeName         : shadow};

That is, the CATextLayer is displayed as you would expect, except for the NSShadow. So, the above code works exactly the same (from what I can tell) as this code:

NSMutableParagraphStyle *s  = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
s.lineSpacing               = 0.9f;
NSDictionary *textAttrs     = @{NSFontAttributeName           : [UIFont someFont],
                                NSParagraphStyleAttributeName : [s copy]};

Does anybody know for sure whether what I am doing is supposed to work? I am testing using the iOS 9.3 simulator, so I don't think it's an iOS 10 beta issue.

If it doesn't work that way, does anybody know of a better solution for non-rectangular UIButton replacements?

Upvotes: 5

Views: 236

Answers (0)

Related Questions