我遇到的问题是我希望能够在TextView中更改某些文本的textColor.我使用的是串联字符串,只是希望我附加到TextView文本中的字符串.似乎我想要使用的是NSMutableAttributedString
,但我没有找到任何有关如何在Swift中使用它的资源.到目前为止我所拥有的是这样的:
let string = "A \(stringOne) with \(stringTwo)" var attributedString = NSMutableAttributedString(string: string) textView.attributedText = attributedString
从这里我知道我需要找到需要更改textColor的单词范围,然后将它们添加到属性字符串中.我需要知道的是如何从attributionString中找到正确的字符串,然后更改它们的textColor.
由于我的评分太低,我无法回答我自己的问题,但这是我找到的答案
我通过翻译来翻译一些代码来找到我自己的答案
更改NSAttributedString中子字符串的属性
以下是Swift中的实现示例:
let string = "A \(stringOne) and \(stringTwo)" var attributedString = NSMutableAttributedString(string:string) let stringOneRegex = NSRegularExpression(pattern: nameString, options: nil, error: nil) let stringOneMatches = stringOneRegex.matchesInString(longString, options: nil, range: NSMakeRange(0, attributedString.length)) for stringOneMatch in stringOneMatches { let wordRange = stringOneMatch.rangeAtIndex(0) attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.nameColor(), range: wordRange) } textView.attributedText = attributedString
由于我想要更改多个字符串的textColor,我将创建一个帮助函数来处理它,但这适用于更改textColor.
SWIFT 4.2
let main_string = "Hello World" let string_to_color = "World" let range = (main_string as NSString).range(of: string_to_color) let attribute = NSMutableAttributedString.init(string: main_string) attribute.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red , range: range) txtfield1 = UITextField.init(frame:CGRect(x:10 , y:20 ,width:100 , height:100)) txtfield1.attributedText = attribute
克里斯的回答对我来说是一个很大的帮助,所以我用他的方法变成了一个我可以重用的功能.这让我为子串指定颜色,同时为字符串的其余部分添加另一种颜色.
static func createAttributedString(fullString: String, fullStringColor: UIColor, subString: String, subStringColor: UIColor) -> NSMutableAttributedString { let range = (fullString as NSString).rangeOfString(subString) let attributedString = NSMutableAttributedString(string:fullString) attributedString.addAttribute(NSForegroundColorAttributeName, value: fullStringColor, range: NSRange(location: 0, length: fullString.characters.count)) attributedString.addAttribute(NSForegroundColorAttributeName, value: subStringColor, range: range) return attributedString }
Swift 2.1更新:
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
self.helpText
是一个UILabel
出路.
在之前的帖子中已经给出了答案,但我有不同的方法
Swift 3x:
var myMutableString = NSMutableAttributedString() myMutableString = NSMutableAttributedString(string: "Your full label textString") myMutableString.setAttributes([NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: CGFloat(17.0))! , NSForegroundColorAttributeName : UIColor(red: 232 / 255.0, green: 117 / 255.0, blue: 40 / 255.0, alpha: 1.0)], range: NSRange(location:12,length:8)) // What ever range you want to give yourLabel.attributedText = myMutableString
希望这对任何人都有帮助!
我看到你在某种程度上回答了这个问题,但是在不使用正则表达式回答标题问题的情况下提供一种更简洁的方法:
要更改文本长度的颜色,您需要知道字符串中有颜色字符的开始和结束索引,例如
var main_string = "Hello World" var string_to_color = "World" var range = (main_string as NSString).rangeOfString(string_to_color)
然后转换为属性字符串并使用NSForegroundColorAttributeName的'add attribute':
var attributedString = NSMutableAttributedString(string:main_string) attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor() , range: range)
您可以设置的其他标准属性列表可以在Apple的文档中找到
Swift 4.2和Swift 5对字符串的部分进行着色。
在扩展String时使用NSMutableAttributedString的一种非常简单的方法。这也可以用于给整个字符串中的多个单词上色。
为扩展名添加新文件,文件->新建->新文件,名称为ex。“ NSAttributedString + TextColouring”并添加代码
import UIKit extension String { func attributedStringWithColor(_ strings: [String], color: UIColor, characterSpacing: UInt? = nil) -> NSAttributedString { let attributedString = NSMutableAttributedString(string: self) for string in strings { let range = (self as NSString).range(of: string) attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range) } guard let characterSpacing = characterSpacing else {return attributedString} attributedString.addAttribute(NSAttributedString.Key.kern, value: characterSpacing, range: NSRange(location: 0, length: attributedString.length)) return attributedString } }
现在,您可以在所需的任何视图控制器上全局使用:
let attributedWithTextColor: NSAttributedString = "Doc, welcome back :)".attributedStringWithColor(["Doc", "back"], color: UIColor.black) myLabel.attributedText = attributedWithTextColor
NSAttributedStringKey.foregroundColor
例如,如果要更改NavBar中的字体:
self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedStringKey.font: UIFont.systemFont(ofSize: 22), NSAttributedStringKey.foregroundColor: UIColor.white]
您可以使用此扩展程序,我将对其进行测试
import Foundation import UIKit extension NSMutableAttributedString { convenience init (fullString: String, fullStringColor: UIColor, subString: String, subStringColor: UIColor) { let rangeOfSubString = (fullString as NSString).range(of: subString) let rangeOfFullString = NSRange(location: 0, length: fullString.count)//fullString.range(of: fullString) let attributedString = NSMutableAttributedString(string:fullString) attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: fullStringColor, range: rangeOfFullString) attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: subStringColor, range: rangeOfSubString) self.init(attributedString: attributedString) } }