在NSString或nsmutablestring中不可能有多种颜色。我听说过一点NSAttributedString,它是在iPad SDK 3.2(或大约3.2)中引入的,并且在iPhone SDK 4.0 beta版中可用。
我想要一根有三种颜色的绳子。
我不使用3个独立的NSString的原因,是因为三个NSAttributedString子字符串的长度经常改变,所以我宁愿,不使用任何计算重新定位3个独立的NSString对象。
如果使用NSAttributedString是可能的,我怎么做以下-(如果不可能使用NSAttributedString,你会怎么做):
编辑:
记住,@"first", @"second"和@"third"随时会被其他字符串替换。所以使用硬编码的NSRange值是行不通的。
带属性字符串扩展的更简单的解决方案。
extension NSMutableAttributedString {
// this function attaches color to string
func setColorForText(textToFind: String, withColor color: UIColor) {
let range: NSRange = self.mutableString.range(of: textToFind, options: .caseInsensitive)
self.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: range)
}
}
试试这个,看看(在Swift 3和4中测试)
let label = UILabel()
label.frame = CGRect(x: 120, y: 100, width: 200, height: 30)
let first = "first"
let second = "second"
let third = "third"
let stringValue = "\(first)\(second)\(third)" // or direct assign single string value like "firstsecondthird"
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: stringValue)
attributedString.setColorForText(textToFind: first, withColor: UIColor.red) // use variable for string "first"
attributedString.setColorForText(textToFind: "second", withColor: UIColor.green) // or direct string like this "second"
attributedString.setColorForText(textToFind: third, withColor: UIColor.blue)
label.font = UIFont.systemFont(ofSize: 26)
label.attributedText = attributedString
self.view.addSubview(label)
以下是预期结果:
我认为,使用正则表达式来查找应用属性的范围是一种非常方便的方法。我是这样做的:
NSMutableAttributedString *goodText = [[NSMutableAttributedString alloc] initWithString:articleText];
NSRange range = [articleText rangeOfString:@"\\[.+?\\]" options:NSRegularExpressionSearch|NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
[goodText addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Georgia" size:16] range:range];
[goodText addAttribute:NSForegroundColorAttributeName value:[UIColor brownColor] range:range];
}
NSString *regEx = [NSString stringWithFormat:@"%@.+?\\s", [self.article.titleText substringToIndex:0]];
range = [articleText rangeOfString:regEx options:NSRegularExpressionSearch|NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
[goodText addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Georgia-Bold" size:20] range:range];
[goodText addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
}
[self.textView setAttributedText:goodText];
我正在搜索可用属性的列表,但在这里和类引用的第一页中都没有找到它们。所以我决定在这里发布相关信息。
标准属性
带属性字符串支持以下文本的标准属性。如果键不在字典中,则使用下面描述的默认值。
NSString *NSFontAttributeName;
NSString *NSParagraphStyleAttributeName;
NSString *NSForegroundColorAttributeName;
NSString *NSUnderlineStyleAttributeName;
NSString *NSSuperscriptAttributeName;
NSString *NSBackgroundColorAttributeName;
NSString *NSAttachmentAttributeName;
NSString *NSLigatureAttributeName;
NSString *NSBaselineOffsetAttributeName;
NSString *NSKernAttributeName;
NSString *NSLinkAttributeName;
NSString *NSStrokeWidthAttributeName;
NSString *NSStrokeColorAttributeName;
NSString *NSUnderlineColorAttributeName;
NSString *NSStrikethroughStyleAttributeName;
NSString *NSStrikethroughColorAttributeName;
NSString *NSShadowAttributeName;
NSString *NSObliquenessAttributeName;
NSString *NSExpansionAttributeName;
NSString *NSCursorAttributeName;
NSString *NSToolTipAttributeName;
NSString *NSMarkedClauseSegmentAttributeName;
NSString *NSWritingDirectionAttributeName;
NSString *NSVerticalGlyphFormAttributeName;
NSString *NSTextAlternativesAttributeName;
NSAttributedString编程指南
完整的类参考在这里。
在构建带属性字符串时,我更喜欢使用可变子类,只是为了让事情更简洁。
话虽如此,下面是如何创建一个三色带属性字符串:
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"firstsecondthird"];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(11,5)];
在浏览器中输入。警告实现者
显然你不会像这样硬编码。也许你可以这样做:
NSDictionary *wordToColorMapping = ....; //an NSDictionary of NSString => UIColor pairs
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@""];
for (NSString *word in wordToColorMapping) {
UIColor *color = [wordToColorMapping objectForKey:word];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
NSAttributedString *subString = [[NSAttributedString alloc] initWithString:word attributes:attributes];
[string appendAttributedString:subString];
[subString release];
}
//display string