I've looked at ~10 questions on SO and am still coming up short.
I have a multiline UILabel (created in Interface Builder, numberOfLines set to 0) which renders normally like this:
I want to be underline "Terms of Service" and "Privacy Policy", so I added this code:
NSString *text = self.agreement.text;
NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:text];
NSRange privacyRange = [text rangeOfString:@"privacy policy" options:NSCaseInsensitiveSearch];
[aString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:privacyRange];
NSRange tosRange = [text rangeOfString:@"terms of service" options:NSCaseInsensitiveSearch];
[aString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:tosRange];
self.agreement.attributedText = aString;
But the result looks like this:
What do I need to do so both lines appear, with the appropriate ranges underlined?
Also, I'd prefer not to use a 3rd Party Library like OHAttributedLabel or TTTAttributedLabel since this is the only place in my app where I need to underline a piece of text.
What I've Tried
- calling
sizeToFit
after setting the attributed text - using a UITextView instead. Both lines rendered correctly but I lost the center alignment.
- resetting
numberOfLines
andlineBreakMode
in code
Sascha asked me to upload two screenshots with the background colors set to something other than clear. Oddly enough, everything shows up as expected! Not sure what to make of this or what this is telling us.