在一个字符串中多次使用NSAttributedString

时间:2019-05-09 16:02:35

标签: swift xcode nsattributedstring

我有一个字符串,例如"F (R U R' U') (R U R' U') (R U R' U') F'"。我正在使用NSAttributedString搜索括号(R U R' U')中的文本,并将其替换为相同的文本,只是颜色不同。我正在使用的代码是

let mutableAttributedString = NSMutableAttributedString(string: algToShow) 
var searchRange = NSRange(location: 0, length: algToShow.count)
var foundRange1 = NSRange()
foundRange1 = (algToShow as NSString).range(of: "(R U R' U')", options: NSString.CompareOptions.caseInsensitive, range: searchRange)
if foundRange1.location != NSNotFound || foundRange2.location != NSNotFound || foundRange3.location != NSNotFound || foundRange4.location != NSNotFound || foundRange5.location != NSNotFound {
            // found an occurrence of the substring! do stuff here
            searchRange.location = foundRange1.location + foundRange1.length
            mutableAttributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: foundRange1)

但是,它仅突出显示第一组文本/括号;其他被完全忽略。 问题是如何检查括号出现多少次,并替换每个括号?

谢谢。

2 个答案:

答案 0 :(得分:3)

这是正则表达式模式\((.*?)\),它将在括号内找到匹配项,请test模式

您可以使用以下方法并通过上述模式

func regexMatches(_ pattern: String, string: String) throws -> [NSTextCheckingResult]? {
    let regex = try NSRegularExpression(pattern: pattern)
    let range = NSRange(string.startIndex..<string.endIndex, in: string)
    return regex.matches(in: string, options: [], range: range)
}

用法

let algToShow = "F (R U R' U') (R U R' U') (R U R' U') F'"
let mutableAttributedString = NSMutableAttributedString(string: algToShow)
if let matches = try? regexMatches("\\((.*?)\\)", string: algToShow) {
    for match in matches {
        mutableAttributedString.addAttribute(.foregroundColor, value: UIColor.red, range: match.range)
    }
}

答案 1 :(得分:0)

我找到了一种方法:

我首先检查方括号,从不绑定内容。完成此操作后,我将更精确地确定最严格的。例如:

func regexMatchesToString(_ pattern: String, string: String) throws -> [String]? {
        let regex = try NSRegularExpression(pattern: pattern)
        let range = NSRange(string.startIndex..<string.endIndex, in: string)
        let results = regex.matches(in: string, options: [], range: range)
        return results.map {
            String(string[Range($0.range, in: string)!])
        }
    }
    func regexMatches(_ pattern: String, string: String) throws -> [NSTextCheckingResult]? {
        let regex = try NSRegularExpression(pattern: pattern)
        let range = NSRange(string.startIndex..<string.endIndex, in: string)
        return regex.matches(in: string, options: [], range: range)
    }

    string = algAtCell //The text displayed in my UITableView
    atStr = NSMutableAttributedString(string: string)

if let stringMatches1 = try? regexMatchesToString("\\((.*?)\\)", string: string) {
        for triggerString in stringMatches1 {
            print("String \(string)")
            if triggerString == ("(R U R' U')") {
                let matches = try? regexMatches("\\((R U R' U')\\)", string: string)
                for match in matches! {
                    atStr.addAttribute(.foregroundColor, value: UIColor.init(displayP3Red: 255/255, green: 10/255, blue: 10/255, alpha: 1.0), range: match.range)
                }

希望如果有人需要它会有所帮助。