[英]Attributed text swift how to change color of unattributed text
我嘗試為我的應用設置夜間/白天選項。 我有類似"<font color="#aa2c2c">Red text</font>then back to normal "
。 因此,我需要紅色來保持紅色並僅更改未歸因的顏色。
這是我嘗試的方法:
text.enumerateAttributesInRange(NSRange(0..<text.length), options: []) { (attributes, range, _) -> Void in
for (attribute, object) in attributes {
NSLog("attr \(attribute) object \(object)")
}
}
}
因此,日志顯示類似“ attr NSColor對象UIDeviceRGBColorSpace 0 0 0 1”或“ attr NSStrokeColor對象UIDeviceRGBColorSpace 0 0 0 1”之類的東西。 那似乎是我需要更改為白色的黑色文本。
所以我把它放在循環中:
if object.name!.containsString("0 0 0 1"){
text.setAttributes(NSForegroundColorAttributeName, value: UIColor.whiteColor() , range: range)
}
我不確定這是否是最好的決定,但是我遇到了錯誤:調用中出現了額外的參數'value'。 這有什么問題,什么是僅替換非歸因文本顏色的最佳方法?
let text = "We tried to make this app as most intuitive as possible for you. If you have any questions don't hesitate to ask us. For a detailed manual just click here."
let linkTextWithColor = "click here"
let range = (text as NSString).rangeOfString(linkTextWithColor)
let attributedString = NSMutableAttributedString(string:text)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor() , range: range)
self.helpText.attributedText = attributedString
要么
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:4))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.