如何使用 NSAttributedString Objective-c 在字符串的一部分下划线 [英] How to underline part of string with NSAttributedString objective-c
本文介绍了如何使用 NSAttributedString Objective-c 在字符串的一部分下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
仅当范围从零开始时才对字符串进行下划.如果我从 1 开始,那么它就起作用了.无论如何,绿色都有效.
Underling a string only works when the range starts from zero. If i start it at 1 then it works. The green colour works regardless.
+ (NSAttributedString*)returnNSAttributedString:(NSString*)string range:(NSRange)range WithColour:(UIColor*)colour WithUnderLine:(BOOL)underline {
NSMutableAttributedString *attributedString =
[[NSMutableAttributedString alloc] initWithString:string];
if (underline) {
[attributedString addAttributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)} range:range];
}
[attributedString addAttribute:NSForegroundColorAttributeName value:colour range:range];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSRangeFromString(string)];
return attributedString;
}
它适用于 iOS 7 而不是 iOS 8.
it works on iOS 7 not iOS 8.
推荐答案
您可以使用 NSUnderlineStyleAttributeName 和 NSUnderlineColorAttributeName 属性.你可以这样使用它:
You can use the NSUnderlineStyleAttributeName and NSUnderlineColorAttributeName attributes. You can use it like this:
NSRange foundRange = [wordString rangeOfString:@"Base Mix"];
if (foundRange.location != NSNotFound)
{
[wordString beginEditing];
[wordString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1] range:foundRange];
[wordString addAttribute:NSUnderlineColorAttributeName value:[NSColor redColor] range:foundRange];
[wordString endEditing];
}
这篇关于如何使用 NSAttributedString Objective-c 在字符串的一部分下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文