【问题标题】:Underlined text of a UITextField causes the whole text field to get underlinedUITextField 的带下划线的文本会导致整个文本字段带下划线
【发布时间】:2020-12-11 08:42:14
【问题描述】:

当您在 UITextField 的某些文本下划线然后单击它时,如果光标正好在带下划线的文本的右侧或内部,则整个文本字段都会被带下划线。有没有办法避免这种情况?

override func viewDidLoad() {
    super.viewDidLoad()

    let textField1 = UITextField(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
    textField1.borderStyle = UITextField.BorderStyle.roundedRect
    textField1.allowsEditingTextAttributes = true
    view.addSubview(textField1)

    let textField2 = UITextField(frame: CGRect(x: 20, y: 150, width: 300, height: 40))
    textField2.borderStyle = UITextField.BorderStyle.roundedRect
    textField2.allowsEditingTextAttributes = true
    view.addSubview(textField2)
}

【问题讨论】:

  • 改用 UITextView。
  • @AdilSoomro:谢谢。那行得通。 UITextView 下划线的行为正确,但需要做一些工作才能使其看起来和行为像 UITextField。您必须自己做占位符文本,并且必须确保 Return 键不会开始新行。我使用 UILabel 作为占位符文本。在 textView(_:shouldChangeTextIn:replacementText) 我检查了用于隐藏和取消隐藏占位符标签的空文本。在同一个函数中,我检查了换行符。我还必须使用插图来正确定位文本。

标签: ios swift uitextfield underline


【解决方案1】:

这就是我所做的,结合了其他 SO 天才的解决方案。 请注意,视图控制器中的每个 textView 必须具有从 0 开始的唯一顺序标记。 如果有人有改进,我将不胜感激。

var textViewPlaceholder1: UILabel!
var textViewPlaceholders = [UILabel]()
let kFontSize: CGFloat = 17.0
let kPlaceholderLeftInset: CGFloat = 8.0

override func viewDidLoad() {
    super.viewDidLoad()
    
    let textView1Frame = CGRect(x: 20.0, y: 100.0, width: 300.0, height: 34.0)
    let textView1 = makeTextView(withFrame: textView1Frame)
    textView1.tag = 0
    // For testing: textView1.attributedText = NSAttributedString(string: "One two three", attributes: [.foregroundColor : UIColor.systemRed, .font : UIFont.systemFont(ofSize: kFontSize)])
    view.addSubview(textView1)

    let textViewPlaceholder1Frame = CGRect(x: textView1Frame.origin.x + kPlaceholderLeftInset,
                                           y: textView1Frame.origin.y,
                                           width: textView1Frame.width,
                                           height: textView1Frame.height)
    textViewPlaceholder1 = UILabel(frame: textViewPlaceholder1Frame)
    textViewPlaceholder1.attributedText = NSAttributedString(string: "Text view 1...", attributes: [.foregroundColor : UIColor.systemGray3, .font : UIFont.systemFont(ofSize: kFontSize)])
    textViewPlaceholder1.isHidden = true
    textViewPlaceholders.append(textViewPlaceholder1)
    view.addSubview(textViewPlaceholder1)

    let textField2 = UITextField(frame: CGRect(x: 20.0, y: 150.0, width: 300.0, height: 34.0))
    textField2.font = .systemFont(ofSize: kFontSize)
    textField2.borderStyle = UITextField.BorderStyle.roundedRect
    textField2.allowsEditingTextAttributes = true
    textField2.placeholder = "Text field 2..."
    view.addSubview(textField2)
}

func makeTextView(withFrame: CGRect) -> UITextView {
    let textView = UITextView(frame: withFrame)
    textView.delegate = self
    textView.layer.cornerRadius = 5.0
    textView.layer.borderWidth = 1.0
    textView.layer.borderColor = UIColor.systemGray4.cgColor
    textView.textContainerInset = UIEdgeInsets(top: 7.0, left: 2.0, bottom: 5.0, right: 5.0)
    textView.font = .systemFont(ofSize: kFontSize)
    textView.allowsEditingTextAttributes = true
    return textView
}

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    guard text.rangeOfCharacter(from: CharacterSet.newlines) == nil else {
        // textView.resignFirstResponder() // uncomment this to close the keyboard when return key is pressed
        return false
    }
    if (range.location > 0 || text.count != 0) {
        textViewPlaceholders[textView.tag].isHidden = true
    } else {
        textViewPlaceholders[textView.tag].isHidden = false
    }
    return true
}

【讨论】: