Nathan McKaskle
Nathan McKaskle

Reputation: 3083

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.

Upvotes: 14

Views: 27558

Answers (2)

NickCoder
NickCoder

Reputation: 1530

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

  }
}

Upvotes: 9

Raymond
Raymond

Reputation: 3670

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.
    }


}

Upvotes: 39

Related Questions