Ios 使用NSMutableAttributeString为字符串中的单词添加颜色
我正在尝试为字符串中的两个单词添加颜色。这是我正在使用的代码:Ios 使用NSMutableAttributeString为字符串中的单词添加颜色,ios,swift,xcode,uicolor,nsmutableattributedstring,Ios,Swift,Xcode,Uicolor,Nsmutableattributedstring,我正在尝试为字符串中的两个单词添加颜色。这是我正在使用的代码: var HighScore:Int = 0 var CurrentScore:Int = 0 let stringOne = "You have managed to score \(CurrentScore). Current record is \(self.HighScore). Play again and give it another try!" let
var HighScore:Int = 0
var CurrentScore:Int = 0
let stringOne = "You have managed to score \(CurrentScore). Current record is \(self.HighScore). Play again and give it another try!"
let stringTwo = "\(CurrentScore)"
let stringThree = "\(HighScore)"
let range1 = (stringOne as NSString).range(of: stringTwo)
let range2 = (stringOne as NSString).range(of: stringThree)
let attributedText = NSMutableAttributedString.init(string: stringOne)
attributedText.addAttribute(NSForegroundColorAttributeName, value: UIColor.init(netHex: 0x00b4ff) , range: range1)
attributedText.addAttribute(NSForegroundColorAttributeName, value: UIColor.init(netHex: 0x00b4ff) , range: range2)
gameOverDescriptionLabel.attributedText = attributedText
我遇到的问题是,如果CurrentScore
和HighScore
相同(例如:2和2),range2
上的颜色仍然保持白色,但如果它们不相等(2和1或1和2),则两者都会得到我选择的颜色
有什么建议吗?如果当前分数和高分是同一字符串,则搜索后者会找到前者(因为搜索从开头开始) 有很多其他更好的方法可以做到这一点
- 在第一次搜索结果之后开始的范围内执行第二次搜索(
具有,但您没有使用它)range(of:)
- 与其寻找高分的范围,不如搜索周围的样板文件(“您已成功得分”等等),然后找出数字必须在哪里
- 使用或正则表达式查找字符串中嵌入的数值表达式
- 我的最爱:用“不可见”属性标记每个数字,并搜索该属性,以便可靠地找到数字(示例)
- 在第一次搜索结果之后开始的范围内执行第二次搜索(
具有,但您没有使用它)range(of:)
- 与其寻找高分的范围,不如搜索周围的样板文件(“您已成功得分”等等),然后找出数字必须在哪里
- 使用或正则表达式查找字符串中嵌入的数值表达式
- 我的最爱:用“不可见”属性标记每个数字,并搜索该属性,以便可靠地找到数字(示例)
- 不搜索范围的解决方案是为当前分数和高分创建两个单独的NSMutableAttributeString,然后将所有内容附加在一起
let currentScoreString = NSMutableAttributedString(...)
let highscoreString = NSMutableAttributedString(...)
let finalString = NSMutableAttributedString(string: "You have managed to score ").append(currentScoreString)....
不搜索范围的解决方案是为当前分数和高分创建两个单独的NSMutableAttributeString,然后将所有内容附加在一起
let currentScoreString = NSMutableAttributedString(...)
let highscoreString = NSMutableAttributedString(...)
let finalString = NSMutableAttributedString(string: "You have managed to score ").append(currentScoreString)....
将其添加到
.swift
文件的顶部或底部:
extension NSMutableAttributedString {
func bold(_ text:String) -> NSMutableAttributedString {
let attrs:[String:AnyObject] = [NSForegroundColorAttributeName: UIColor.init(netHex: 0x00b4ff)]
let boldString = NSMutableAttributedString(string:"\(text)", attributes:attrs)
self.append(boldString)
return self
}
func normal(_ text:String)->NSMutableAttributedString {
let normal = NSAttributedString(string: text)
self.append(normal)
return self
}
}
下面是代码的用法,您可以按自己的意愿编辑它,但我已经做了,这样您就可以轻松地将它复制并粘贴到您的项目中:
let formattedString = NSMutableAttributedString()
formattedString
.normal("You have managed to score ")
.bold("\(CurrentScore)")
.normal(". Current record is ")
.bold("\(HighScore)")
.normal(". Play again and give it another try!")
gameOverDescriptionLabel.attributedText = formattedString
将其添加到
.swift
文件的顶部或底部:
extension NSMutableAttributedString {
func bold(_ text:String) -> NSMutableAttributedString {
let attrs:[String:AnyObject] = [NSForegroundColorAttributeName: UIColor.init(netHex: 0x00b4ff)]
let boldString = NSMutableAttributedString(string:"\(text)", attributes:attrs)
self.append(boldString)
return self
}
func normal(_ text:String)->NSMutableAttributedString {
let normal = NSAttributedString(string: text)
self.append(normal)
return self
}
}
下面是代码的用法,您可以按自己的意愿编辑它,但我已经做了,这样您就可以轻松地将它复制并粘贴到您的项目中:
let formattedString = NSMutableAttributedString()
formattedString
.normal("You have managed to score ")
.bold("\(CurrentScore)")
.normal(". Current record is ")
.bold("\(HighScore)")
.normal(". Play again and give it another try!")
gameOverDescriptionLabel.attributedText = formattedString
我试着做
currentScoreString.append((CurrentScore为Int)as!NSAttributedString)
,但这使它崩溃了。如果我将CurrentScore用作任何对象
,也会崩溃。你不能只是将int转换成nsattributed字符串!您必须初始化一个新的NSAttributedString对象。我在前两行中做了一些更改来说明这一点。这是我做的另一次尝试:-没有错误,但如果它们相等,颜色仍然不会改变…我尝试了currentScoreString.append((CurrentScore as Int)as!nsAttribute String)
,但这使它崩溃。如果我将CurrentScore用作任何对象
,也会崩溃。你不能只是将int转换成nsattributed字符串!您必须初始化一个新的NSAttributedString对象。我在前两行中做了一些更改来说明这一点。这是我做的另一次尝试:-没有错误,但如果它们相等,颜色仍然不会改变…我不明白如何在代码中实现“不可见”属性。仍然没有解决我的问题。好吧,我给了你四个非常好的想法,最后一个我给了你实际的代码来演示这项技术。我在这里的工作已经完成了。现在的问题似乎是,当你听到答案时,你不知道答案。没有必要骄傲,妈妈。我没有在NSMutableAttributedString
上做太多工作,所以我不太确定到底要做什么。请对您的评论更友好一些。我不明白如何在代码中实现“隐形”属性。仍然没有解决我的问题。好吧,我给了你四个非常好的想法,最后一个我给了你实际的代码来演示这项技术。我在这里的工作已经完成了。现在的问题似乎是,当你听到答案时,你不知道答案。没有必要骄傲,妈妈。我没有在NSMutableAttributedString
上做太多工作,所以我不太确定到底要做什么。请对你的评论更友好一些。它很有魅力。谢谢!:))工作起来很有魅力。谢谢!:))