Malav Soni
Malav Soni

Reputation: 2838

Underline in attributed text not working in iPhone 6+

I want to create a method that returns me attributed text.

here is the code that i am using

- (NSAttributedString *)getUnderlineAttributedStringForText:(NSString *)strWholeString andTextToHaveLink:(NSString *)strLink TextColor:(UIColor *)textColor LinkColor:(UIColor *)linkColor withFont:(UIFont*)font {

NSRange stringRange = NSMakeRange(0, strWholeString.length);
NSRange linkRange = [strWholeString rangeOfString:strLink];
NSLog(@"String Link :: %@",[strWholeString substringWithRange:linkRange]);
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:strWholeString];
// Paragraph Style
NSMutableParagraphStyle *paragrapStyle = NSMutableParagraphStyle.new;
paragrapStyle.alignment = NSTextAlignmentLeft;

[attributedString addAttribute:NSParagraphStyleAttributeName value:paragrapStyle range:stringRange];
[attributedString addAttribute:NSForegroundColorAttributeName value:textColor range:stringRange];
[attributedString addAttribute:NSFontAttributeName value:font range:stringRange];
[attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger: NSUnderlineStyleSingle] range:linkRange];
[attributedString addAttribute:NSForegroundColorAttributeName value:linkColor range:linkRange];

return attributedString;
}

// This is how I call the method 
[btnTermOfUse setAttributedTitle:[self getUnderlineAttributedStringForText:@"By signing up you agree to the Terms of Use" andTextToHaveLink:@"Terms of Use" TextColor:[UIColor darkGrayColor] LinkColor:[UIColor orangeColor] withFont:[UIFont systemFontOfSize:16]] forState:UIControlStateNormal];

This code works fine in all device except 6+.

Edits - in 6+ just underline is not showing text colour change for the link text. in 6+ just underline is not showing text colour change for the link text I can see the underline in iPhone 6 or 5S.

Any one have any idea regarding this?

I know there are already so many questions related to this but they didn't help me so I am writing here.

Upvotes: 3

Views: 665

Answers (1)

Hemang
Hemang

Reputation: 27050

Found a hack !! :(

you should write this line before setting up the text,

[btnTermOfUse titleLabel].numberOfLines = 0;

This will show you the line on device too.

Upvotes: 2

Related Questions