Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a Border for TextView (Xcode 6.1)

I've tried finding the answer to this and every time it's assuming I know way too much. I'm a total beginner. I just created a new, blank application. I dragged the TextView to the storyboard. What do I do next to give it a border?

There is no code other than the default, autogenerated code at this point.

like image 511
Nathan McKaskle Avatar asked Nov 13 '14 20:11

Nathan McKaskle


People also ask

How to give border to textview in swift?

You can do this by choosing the assistant editor. Than control drag a line from the outlet in the code to the textfield. After the textfield is connected, you can add code to make a border in the viewDidLoad function. class ViewController: UIViewController { @IBOutlet var text : UITextField?


2 Answers

Here are the steps: If you let Xcode create a project, go to the ViewController.swift file. Here you can create an outlet.

@IBOutlet var text : UITextField?

Now you can connect the text outlet to the textfield in the storyboard. You can do this by choosing the assistant editor. Than control drag a line from the outlet in the code to the textfield.

After the textfield is connected, you can add code to make a border in the viewDidLoad function.

class ViewController: UIViewController {
    @IBOutlet var text : UITextField?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        text!.layer.borderWidth = 1
        text!.layer.borderColor = UIColor.redColor().CGColor

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
like image 68
Raymond Avatar answered Nov 04 '22 20:11

Raymond


In Swift 3 and Xcode 10, tested and working in (swift 5 and code 12)

first, create an outlet of your text view
And In your viewdidload put these two lines

class ViewController: UIViewController {

  @IBOutlet weak var textView: UITextView!

  override func viewDidLoad() {
      super.viewDidLoad()

      self.textView.layer.borderColor = UIColor.lightGray.cgColor
      self.textView.layer.borderWidth = 1

  }
}
like image 40
NickCoder Avatar answered Nov 04 '22 19:11

NickCoder