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

查看:2165
本文介绍了使用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的单词范围,然后将它们添加到属性字符串中。我需要知道的是如何从attributionString中找到正确的字符串,然后更改它们的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

我通过翻译来自翻译某些代码来找到我自己的答案

I found my own answer by translating from translating some code from

更改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.

推荐答案

我看到你在某种程度上回答了这个问题,但提供了一种更简洁的方法,而没有使用正则表达式来回答标题问题:

I see you have answered the question somewhat, but to provide a slightly more concise way without using regex to answer to the title question:

要更改文本长度的颜色,您需要知道字符串中有颜色字符的开始和结束索引,例如

To change the colour of a length of text you need to know the start and end index of the coloured-to-be characters in the string e.g.

var main_string = "Hello World"
var string_to_color = "World"

var range = (main_string as NSString).rangeOfString(string_to_color)

然后你转换为属性字符串并使用'add attribute' with NSForegroundColorAttributeName:

Then you convert to attributed string and use 'add attribute' with NSForegroundColorAttributeName:

var attributedString = NSMutableAttributedString(string:main_string)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor() , range: range)

可以在以下位置找到可以设置的其他标准属性的列表 Apple的文档

A list of further standard attributes you can set can be found in Apple's documentation

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

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