在 Swift 中使用 NSMutableAttributedString 更改特定文本的颜色 [英] Changing specific text's color using NSMutableAttributedString in Swift

查看:123
本文介绍了在 Swift 中使用 NSMutableAttributedString 更改特定文本的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是我希望能够更改 TextView 中某些文本的 textColor.我正在使用连接的字符串,并且只想要我附加到 TextView 文本中的字符串.看来我想要使用的是 NSMutableAttributedString,但我没有找到有关如何在 Swift 中使用它的任何资源.到目前为止,我所拥有的是这样的:

The issue I am having is that I want to be able to change the textColor of certain text in a TextView. I am using a concatenated string, and just want the strings I am appending into the TextView's text. It appears that what I want to use is NSMutableAttributedString, but I am not finding any resources of how to use this in Swift. What I have so far is something like this:

let string = "A (stringOne) with (stringTwo)"
var attributedString = NSMutableAttributedString(string: string)
textView.attributedText = attributedString

从这里我知道我需要找到需要更改 textColor 的单词范围,然后将它们添加到属性字符串中.我需要知道的是如何从attributedString 中找到正确的字符串,然后更改它们的textColor.

From here I know I need to find the range of words that need to have their textColor changed and then add them to the attributed string. What I need to know is how to find the correct strings from the attributedString, and then change their textColor.

由于我的评分太低,我无法回答我自己的问题,但这是我找到的答案

Since I have too low of a rating I can't answer my own question, but here is the answer I found

我通过翻译来自

更改 NSAttributedString 中子字符串的属性

这是在 Swift 中的实现示例:

Here is the example of implementation in 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.

Since I am wanting to change the textColor of multiple Strings I will make a helper function to handle this, but this works for changing the textColor.

推荐答案

let mainString = "Hello World"
let stringToColor = "World"

SWIFT 5

let range = (mainString as NSString).range(of: stringToColor)

let mutableAttributedString = NSMutableAttributedString.init(string: mainString)
mutableAttributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: range)

textField = UITextField.init(frame: CGRect(x:10, y:20, width:100, height: 100))
textField.attributedText = mutableAttributedString

SWIFT 4.2

let range = (mainString as NSString).range(of: stringToColor)
    

let mutableAttributedString = NSMutableAttributedString.init(string: mainString)
mutableAttributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: range)     
    
textField = UITextField.init(frame: CGRect(x:10, y:20, width:100, height: 100))
textField.attributedText = mutableAttributedString

这篇关于在 Swift 中使用 NSMutableAttributedString 更改特定文本的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆