只有在範圍從零開始時,字符串的下劃線才起作用。如果我從1開始,那麼它的工作原理。綠色的顏色無論如何工作。如何使用NSAttributedString強調字符串的一部分objective-c
+ (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.
它可能涉及:http://stackoverflow.com/questions/25956183/nsmutableattributedstrings-attribute-nsstrikethroughstyleattributename-doesnt – Larme 2014-10-18 04:13:00