0

我是 Swift 5 的新手,正在做我的第一个项目。

我想更改标签内某些文本的颜色,这就是我的标签的样子:

“你需要注意blue文本,更要注意red文本。再试一次,这样就没有更多blue/red文本了”

我想将所有“蓝色”更改为蓝色,将所有“红色”更改为红色。做了一些研究,这就是我发现的:

extension NSMutableAttributedString {

    func setColor(color: UIColor, forText stringValue: String) {
       let range: NSRange = self.mutableString.range(of: stringValue, options: .caseInsensitive)
        self.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
    }

}

let mainString = popUpLabel.text!
let blueString = "màu xanh" //(which is blue)
let redString = "màu đỏ" //(whish is red)
let attributedString = NSMutableAttributedString.init(string: mainString)
attributedString.setColor(color: UIColor(red: 0.24, green: 0.65, blue: 0.96, alpha: 1.00), forText: blueString)
attributedString.setColor(color: UIColor(red: 0.99, green: 0.20, blue: 0.20, alpha: 1.00), forText: redString)
popUpLabel.attributedText = attributedString

并且还尝试了这个:

let mainString = popUpLabel.text!
let blueString = "màu xanh" //(which is blue)
let redString = "màu đỏ" //(which is red)
let attributedString = NSMutableAttributedString.init(string: mainString)
let blueRange = (mainString as NSString).range(of: blueString)
let redRange = (mainString as NSString).range(of: redString)
attributedString.addAttributes([NSAttributedString.Key.foregroundColor : UIColor(red: 0.24, green: 0.65, blue: 0.96, alpha: 1.00)], range: blueRange)
attributedString.addAttributes([NSAttributedString.Key.foregroundColor : UIColor(red: 0.99, green: 0.20, blue: 0.20, alpha: 1.00)], range: redRange)
popUpLabel.attributedText = attributedString

两者都适用于第一个蓝色和红色,但第二个蓝色和红色保持黑色,我不知道为什么。

4

2 回答 2

0

range(of:仅查找搜索字符串的第一次出现。

一个可能的解决方案是正则表达式,模式搜索两者redblue相应地更改颜色。

let string = "You need to pay attention to blue texts, and even more to red texts. Try again so there is no more blue / red texts"

let pattern = "(red|blue)"

var attributedString = NSMutableAttributedString(string: string)
let regex = try! NSRegularExpression(pattern: pattern)
let matches = regex.matches(in: string, range: NSRange(string.startIndex..., in: string))
for match in matches {
    let range = Range(match.range, in: string)!
    let color : UIColor
    if string[range] == "red" {
        color = UIColor(red: 0.99, green: 0.20, blue: 0.20, alpha: 1.00)
    } else {
        color = UIColor(red: 0.24, green: 0.65, blue: 0.96, alpha: 1.00)
    }
    attributedString.addAttribute(.foregroundColor, value: color, range: match.range)
}
于 2020-06-03T14:59:00.543 回答
0

您还可以使用转换为属性字符串的 html 文本。看看这个答案

答案示例:

let htmlText = "<medium><b><font color='#2f3744'>IPL Tamil Web Series Episode #3 | யாருடா Swetha ? | Tamil Comedy Web Series | Being Thamizhan</font></b></medium> has been succesfully scheduled on <medium><b><font color='#2f3744'>2018-05-23 08:51 PM</font></b></medium>"
    let encodedData = htmlText.data(using: String.Encoding.utf8)!
    var attributedString: NSAttributedString

    do {
        attributedString = try NSAttributedString(data: encodedData,
                                                  options:
       [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html,
        NSAttributedString.DocumentReadingOptionKey.characterEncoding: NSNumber(value: String.Encoding.utf8.rawValue)],
     documentAttributes: nil)
    } catch let error as NSError {
        print(error.localizedDescription)
    } catch {
        print("error")
    }
于 2020-06-03T15:02:59.713 回答