[英]How can I compare characters in Swift? I have UILabel with text and UITextField
所以我有一個帶有文本的 UILabel,我必須在 UITextField 中寫入文本以檢查 UILabel 和 UITextField 的字符是否相似? 所以我在 C-Lang 和 C-lang 中有一些練習來解決這個問題,我們必須編寫整數索引來比較“UITextField”中“UILabel”中的索引。 所以我們有一個字符,如果標簽和文本字段中的字符相同,我們只需增加一個索引並必須比較下一個索引(字符)。
我需要比較每個字符。 例如,我們有一個字符串“Hello!”。 如果我們寫“Heklo?”,我們的字符“H”、“e”、“l”、“o”必須是綠色的,我們的“k”和“?”必須是綠色的。 必須是紅色的。
在 swift 中,我嘗試做同樣的事情,但它不起作用。
import UIKit
class ViewController: UIViewController {
private let textLabel: UILabel = {
let label = UILabel()
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "There are a text, which you must text to get the last character of this label!"
label.sizeToFit()
return label
}()
private let resultTextLabel: UILabel = {
let label = UILabel()
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
label.translatesAutoresizingMaskIntoConstraints = false
label.sizeToFit()
return label
}()
private let textField : UITextField = {
let text = UITextField(frame: CGRect(x: 20, y: 300, width: 300, height: 40))
text.placeholder = "Enter text Here!"
text.font = .systemFont(ofSize: 15)
text.autocorrectionType = .no
text.keyboardType = .default
text.returnKeyType = .done
text.clearButtonMode = .whileEditing
text.addTarget(self, action: #selector(checkWrongOrRight), for: .editingChanged)
return text
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(textLabel)
view.addSubview(textField)
view.addSubview(resultTextLabel)
setUpConstraints()
}
@objc func checkWrongOrRight(_ textField: UITextField) {
// ??????????????? I thought that a logic of app could be here
if textLabel.text == resultTextLabel.text {
textLabel.textColor = .green
} else {
textLabel.textColor = .red
}
}
func setUpConstraints() {
textLabel.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 20).isActive = true
textLabel.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -20).isActive = true
textLabel.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100).isActive = true
resultTextLabel.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 20).isActive = true
resultTextLabel.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -20).isActive = true
resultTextLabel.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 500).isActive = true
}
}
那么如何比較 UILabel 和 UITextField 中的字符呢?
為什么要比較 textLabel.text 和 resultTextLabel.text? 改成這樣。
@objc func checkWrongOrRight(_ textField: UITextField) {
if textLabel.text == textField.text {
textLabel.textColor = .green
} else {
textLabel.textColor = .red
}
}
我不確定 resultTextLabel 在代碼中的作用。
一種方法是獲取鍵入文本的長度並將其與相同長度的預期文本的子字符串進行比較:
@objc func checkWrongOrRight(_ textField: UITextField) {
guard let text = textLabel.text,
let input = textField.text
else {
textLabel.textColor = .red
return
}
if input == text.prefix(input.count) {
textLabel.textColor = .green
} else {
textLabel.textColor = .red
}
}
編輯
由於實際問題是根據單個字符是否匹配為它們着色:
您可以為此使用屬性字符串,獲取不正確的字符范圍並將紅色作為屬性應用於這些范圍:
在你的視圖控制器中定義這個輔助函數
// helper function that returns an attributed string to display
func displayString(input: String?, expected: String?) -> NSAttributedString {
guard let input = input,
let expected = expected
else {
return NSAttributedString(string: "")
}
// Get an array of NSRange objects of incorrect characters
let errorRanges = zip((0...), zip(input, expected).map(==)).filter { !$1 }.map { NSRange(location: $0.0, length: 1) }
// Set the default colour
let attString = NSMutableAttributedString(string: input, attributes: [.foregroundColor: UIColor.green])
// Set the colour for incorrect characters to red
errorRanges.forEach { range in
attString.addAttribute(.foregroundColor, value: UIColor.red, range: range)
}
return attString
}
你可以用它作為
@objc func checkWrongOrRight(_ textField: UITextField) {
textField.attributedText = displayString(input: textField.text, expected: textLabel.text)
}
可以進行優化 - 例如將當前的 1 個字符長度范圍轉換為連續字符不正確的單個范圍。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.