swift - 突出显示文本中的差异

标签 swift string parsing text

我想突出显示两个字符串之间的文本差异。第一个字符串是正确的字符串,第二个字符串是用户输入的字符串。它显示了两者之间的拼写和语法错误。下面的例子 enter image description here

我有一个解决方案,先检查每个单词,然后检查每个字母,它在某种程度上确实有效,但并非在所有情况下都有效。有更好的方法吗?

另一个它无法正常工作的例子。

enter image description here

“The”应该是完全红色的,在士兵中只有“i”和“s”应该是红色的。所有“进入”都应该是红色的。

func colorTextDiff(correctStr:String, answerText:String) -> NSAttributedString {

        var hintTextIndex = 0

        let attribute = NSMutableAttributedString.init(string: correctStr)

        attribute.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.black , range: NSRange.init(location: 0, length: correctStr.count))
        attribute.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize:14.0), range: NSRange.init(location: 0, length: correctStr.count))

        let correctWords = correctStr.split(separator: " ")
        var answerWords = answerText.split(separator: " ")

        var answerWordIndex = 0
        //match on words, when a word doesnt match test the word's character
        for correctWord in correctWords {
            if answerWordIndex>=answerWords.count { break}
            let answerWord = answerWords[answerWordIndex]
            var wrongChar = 0, answerCharIndex = 0
            print("words ", correctWord, " ", answerWord)
            if correctWord.lowercased() != answerWord.lowercased() {
                for c in correctWord {
                    if answerCharIndex>=answerWord.count {
                        let len = correctWord.count-answerCharIndex
                        attribute.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red , range: NSRange.init(location: hintTextIndex, length: len))
                        hintTextIndex += len+1
                        break
                    }
                    let correctChar = String(c)
                    let answerChar = String(answerWord)[answerCharIndex..<answerCharIndex+1]
                    print("chars ", correctChar, " ", answerChar)
                    if correctChar.lowercased() != answerChar.lowercased() {
                        print("hint index: ", hintTextIndex)
                        attribute.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red , range: NSRange.init(location: hintTextIndex+1, length: 1))
                        wrongChar+=1
                    }

                    answerCharIndex+=1

                    hintTextIndex+=1
                }

            } else {
                hintTextIndex += correctWord.count+1
            }
            if(wrongChar<correctWord.count) {answerWordIndex+=1 } //probably a missed word not missed typed word
        }




        hintTextIndex+=1
        return attribute
}

最佳答案

这是一个很酷的问题。

这是我的解决方案:

extension NSMutableAttributedString {
  func setCharacterColor(at location: Int) {
    let range = NSRange(location: location, length: 1)
    self.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: range)
  }
}


extension Array where Element == Character {
  func index(of character: Character, greaterThan index: Int) -> Int? {
    for i in index...self.count where self[i] == character {
      return i
    }
    return nil
  }
}


let correctString = "The soldiers stormed into the village just after sunrise"
let incorrectString = "solder stormed village just after sunrise"
let correctArray = Array(correctString)
let incorrectArray = Array(incorrectString)
var correctMutableString = NSMutableAttributedString(string: correctString)

var currentPosition = 0
for char in incorrectArray {
  guard let position = correctArray.index(of: char, greaterThan: currentPosition) else {
    continue
  }
  while currentPosition < position {
    correctMutableString.setCharacterColor(at: currentPosition)
    currentPosition = currentPosition + 1
  }
  currentPosition = position + 1
}

labelCorrect.attributedText = correctMutableString
labelIncorrect.attributedText = NSMutableAttributedString(string: incorrectString)

enter image description here

它仍然是 n 次方,但这个问题总是会相当计算。

它的工作原理是循环,直到找到正确字符串中的字符,并且该字符与错误字符串中的字符相匹配。然后它会突出显示在此过程中经过的所有字符。关键是它只突出显示新字符,而不突出显示之前已经循环过的字符。

关于swift - 突出显示文本中的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54526591/

相关文章:

php - 在 PHP 中解析结构化文本数据

C# SQL Server CE 在解析查询时出错

swift - 如何让 swift 在合理的时间内编译出基础数学

ios - 在动态 UITableViewCell 的顶部添加一个单元格

javascript - 用html元素替换某些字符

java - 对包含多个元素的数组进行排序

java - SimpleDateFormat 在 java 中返回一个奇怪的值

ios - 如何调用类的函数 - Swift

swift - 带有 NSLinkAttributeName 的 NSAttributedString 中的颜色属性被忽略

python - 按字符数将文本拆分为行