Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView border in swift 3

Is there any way to set the active border color and inactive border color to UITextView? For example, if textview is focused to input text, then border color will be red, otherwise textview border color set to gray. Thank you.

like image 890
Lead Developer Avatar asked Oct 14 '17 19:10

Lead Developer


1 Answers

One way would be to implement textViewDidBeginEditing(_:) and textViewDidEndEditing(_:) from UITextViewDelegate in your class where you want to change borderColor property:

class ViewController: UIViewController, UITextViewDelegate {

  ...

  override func viewDidLoad() {
    super.viewDidLoad()
    self.textView.delegate = self
    self.textView.layer.borderWidth = 1.0
  }

  func textViewDidBeginEditing(_ textView: UITextView) {
    textView.layer.borderColor = UIColor.redColor.cgColor
  } 

  func textViewDidEndEditing(_ textView: UITextView) {
    textView.layer.borderColor = UIColor.clearColor.cgColor
  }
}
like image 175
Ozgur Vatansever Avatar answered Nov 12 '22 06:11

Ozgur Vatansever