我必须在字符串中显示一些带有下划线的文本。它在除iOS 8.2之外的所有平台上都能正常工作
这是我的代码。
//Terms of use
NSMutableAttributedString *attributedstring= [[NSMutableAttributedString alloc] initWithString:TERMSOFUSE attributes:nil];
NSRange linkRange = NSMakeRange(29, 18);
NSDictionary *linkAttributes = @{ NSForegroundColorAttributeName : [UIColor whiteColor],
NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) };
[attributedstring setAttributes:linkAttributes range:linkRange];
self.lbl_termsofuse.attributedText = attributedstring;
奇怪的是,上面的代码在iOS 8.2中工作正常,当我给出位置0而不是其他位置时。
感谢您的帮助
答案 0 :(得分:0)
只需调用此方法
- (void)underlineLabel:(UILabel*)lbl {
NSMutableAttributedString *attributedText;
if (!lbl.attributedText) {
attributedText = [[NSMutableAttributedString alloc] initWithString:lbl.text];
} else {
attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:lbl.attributedText];
}
long len = [lbl.text length];
if (self.underlineColor) {
[attributedText addAttribute:NSUnderlineColorAttributeName value:self.underlineColor range:NSMakeRange(0,len)];
} else {
[attributedText addAttribute:NSUnderlineColorAttributeName value:lbl.textColor range:NSMakeRange(0,len)];
}
[attributedText addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:1] range:NSMakeRange(0, len)];//Underline color
lbl.attributedText = attributedText;
}