Post

Replies

Boosts

Views

Activity

Reply to Underlined text of a UITextField causes the whole text field to get underlined
I thought I'd add an example where you can see the difference between the underlining in a text field vs a text view. I made the text view look and behave like a text field. import UIKit class ViewController: UIViewController, UITextViewDelegate {     var textViewPlaceholder1: UILabel!     var textViewPlaceholders = [UILabel]() // needed if more than one text view     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         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     } }
Aug ’20