Customizing the Look of Your Textview or Textfield

September 15, 2019 ยท 1 minute read

Customizing the look of your textview or textfield

Can do it in viewDidLoad(), but if you have more than one you have to repeat code.

If only doing one add this code to viewDidLoad:

override func viewDidLoad()
{
    super.viewDidLoad()
	
    secondTextField.clipsToBounds = true
    secondTextField.layer.cornerRadius = 25.0
    secondTextField.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor
    secondTextField.layer.borderWidth = 1.5
}

If you want to share the appearance for multiple, create a custom UITextField or UITextView and assign class to those that need it.

import Foundation import UIKit

class CustomTextView: UITextView { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder)

    self.layer.cornerRadius = 25
    self.layer.borderWidth = 1.5
    self.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor
    self.clipsToBounds = true
    
    /*
     firstOperandTextView.layer.cornerRadius = 25
     firstOperandTextView.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor
     firstOperandTextView.layer.borderWidth = 1.5
     firstOperandTextView.clipsToBounds = true
     */
}

}

import Foundation import UIKit

class CustomTextField : UITextField { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder)

    self.layer.cornerRadius = 25.0
    self.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor
    self.layer.borderWidth = 1.5
    self.clipsToBounds = true
    
    /*
     secondTextField.clipsToBounds = true
     secondTextField.layer.cornerRadius = 25.0
     secondTextField.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor
     secondTextField.layer.borderWidth = 1.5
     */
}

}