如何解决如何比较 Swift 中的字符?我有带有文本和 UITextField 的 UILabel
所以我有一个带有文本的 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 在代码中的作用。
一种方法是获取输入文本的长度并将其与相同长度的预期文本的子字符串进行比较:
ind_weights_800 = [0.1,0.4,0.2,0.05,0.25]
x_weight = cp.Variable(5)
constraints = []
constraints.append(cp.sum(x_weight) == 1)
# this is the constraint I don't know how to write
ind_sum = 0
for i in range(10):
ind_sum = ind_sum + cp.minimum(x_weight[i],ind_weights_800[i])
constraints.append(ind_sum >= 0.8)
编辑
因为实际问题是根据单个字符是否匹配为它们着色:
您可以为此使用属性字符串,获取不正确的字符范围并将红色作为属性应用于这些范围:
在你的视图控制器中定义这个辅助函数
@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
}
可以进行优化 - 例如将当前的 1 个字符长度范围转换为连续字符不正确的单个范围。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。