134

I want to have a thin gray border around a UITextView. I have gone through the Apple documentation but couldn't find any property there. Please help.

Josue Espinosa
  • 5,009
  • 16
  • 47
  • 81
Ali
  • 5,499
  • 7
  • 25
  • 28

15 Answers15

315
#import <QuartzCore/QuartzCore.h>

....

// typically inside of the -(void) viewDidLoad method
self.yourUITextView.layer.borderWidth = 5.0f;
self.yourUITextView.layer.borderColor = [[UIColor grayColor] CGColor];
Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
  • 62
    I didn't think it really needed much explaining, view is the UITextView, and the code goes wherever you'd set up the view (awakeFromNib or viewDidLoad are two possible places). Since there was no code given there's no way to give good context in response. – Kendall Helmstetter Gelner May 06 '11 at 16:10
  • XCode don't let me to set border for layer. It says that layer is read-only. I made UIView (where put some elements) and trying to set border to that view. Trying to do this `self.myView.layer.borderWidth ...`, but as I said, layer is read-only, so layer don't have any methods or variables to set – Paulius Vindzigelskis Jul 13 '12 at 10:28
  • 2
    Did you import QuartzCore? You could also try getting out a CALayer from self.myView and setting borderWidth on that. It's settable. – Kendall Helmstetter Gelner Jul 13 '12 at 22:36
  • Some more information on "layer" being read-only in UIView (the layer property is, but you can set values in the layer for a view): http://stackoverflow.com/questions/12837077/isnt-property-layer-of-uiview-be-readonly – Kendall Helmstetter Gelner Apr 12 '14 at 07:35
44

Add the following for rounded corners:

self.yourUITextview.layer.cornerRadius = 8; 
App Dev Guy
  • 5,396
  • 4
  • 31
  • 54
user542584
  • 2,651
  • 2
  • 25
  • 29
27

Here's the code I used, to add a border around my TextView control named "tbComments" :

self.tbComments.layer.borderColor = [[UIColor grayColor] CGColor];
self.tbComments.layer.borderWidth = 1.0;
self.tbComments.layer.cornerRadius = 8;

And here's what it looks like:

enter image description here

Easy peasy.

Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
19

I add UIImageView as a subview of the UITextView. This matches the native border on a UITextField, including the gradient from top to bottom:

enter image description here

textView.backgroundColor = [UIColor clearColor];
UIImageView *borderView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, textView.frame.size.width, textView.frame.size.height)];
borderView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
UIImage *textFieldImage = [[UIImage imageNamed:@"TextField.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 8, 15, 8)];
borderView.image = textFieldImage;
[textField addSubview: borderView];
[textField sendSubviewToBack: borderView];

These are the png images I use, and a jpg representation:

@1x

@2x

enter image description here

Venk
  • 5,949
  • 9
  • 41
  • 52
Ben Packard
  • 26,102
  • 25
  • 102
  • 183
  • Good solution. Are these images yours? Are we free to use them? – James Webster Dec 06 '12 at 12:57
  • These images are 'modeled' on those used in Apple's own apps. In truth I extracted them and made very few, if any, changes. Use at your own risk. The goal was to mimic the native look and feel so it is hard to come up with something that looks much different. For what it's worth, I have shipped and had approved apps using this image without issue. – Ben Packard Dec 21 '12 at 16:13
  • The png image files appear to have been removed - I don't intend to constantly update this with a new image location so my apologies if the jpg doesn't work for you. I can re-upload as and when requested via comment. – Ben Packard Dec 21 '12 at 16:14
  • 3
    With the image attached this works best: `code` resizableImageWithCapInsets:UIEdgeInsetsMake(28, 14, 28, 14) – RefuX Mar 05 '13 at 01:04
  • The WikiUpload links (for the two images) fails to find the image files now. ;-( – Mike Gledhill Apr 08 '14 at 07:41
18

Works great, but the color should be a CGColor, not UIColor:

view.layer.borderWidth = 5.0f;
view.layer.borderColor = [[UIColor grayColor] CGColor];
Venk
  • 5,949
  • 9
  • 41
  • 52
Matt Connolly
  • 9,757
  • 2
  • 65
  • 61
8

I believe the above answers are for the previous versions of Swift. I Googled a bit and the below code works for Swift 4. Just sharing it for whoever it may benefit.

self.textViewName.layer.borderColor = UIColor.lightGray.cgColor
self.textViewName.layer.borderWidth = 1.0
self.textViewName.layer.cornerRadius = 8

Happy Coding!

Lord Zsolt
  • 6,492
  • 9
  • 46
  • 76
IronmanX46
  • 558
  • 7
  • 16
  • With Swift5 and the dark mode you can even use the system colors: `self.textViewName.layer.borderColor = UIColor.systemGray4.cgColor` – multitudes Jun 14 '20 at 12:14
4

for Swift Programming, use this

tv_comment.layer.borderWidth = 2
tv_comment.layer.borderColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1).CGColor
Sruit A.Suk
  • 7,073
  • 7
  • 61
  • 71
3

this is as close as I could from an original UITextField

func updateBodyTextViewUI() {
    let borderColor = UIColor.init(red: 212/255, green: 212/255, blue: 212/255, alpha: 0.5)

    self.bodyTextView.layer.borderColor = borderColor.CGColor
    self.bodyTextView.layer.borderWidth = 0.8
    self.bodyTextView.layer.cornerRadius = 5
}
Matias Elorriaga
  • 8,880
  • 5
  • 40
  • 58
2

you can add border to UITextView from the Storyboard - Identity Inspector - User Defined Runtime Attribute

enter image description here

Dan Alboteanu
  • 9,404
  • 1
  • 52
  • 40
0

As of iOS 8 and Xcode 6, I now find the best solution is to subclass UITextView and mark the subclass as an IB_DESIGNABLE, which will allow you to view the border in storyboard.

Header:

#import <UIKit/UIKit.h>

IB_DESIGNABLE

@interface BorderTextView : UITextView

@end

Implementation:

#import "BorderTextView.h"

@implementation BorderTextView

- (void)drawRect:(CGRect)rect
{
    self.layer.borderWidth = 1.0;
    self.layer.borderColor = [UIColor blackColor].CGColor;
    self.layer.cornerRadius = 5.0f;
}

@end

Then just drag out your UITextView in storyboard and set its class to BorderTextView

mike
  • 1,441
  • 2
  • 19
  • 31
0

The thing that made it work (in addition to following the answers here) is adding the borderStyle attribute:

#import <QuartzCore/QuartzCore.h>
..

phoneTextField.layer.borderWidth = 1.0f;
phoneTextField.layer.borderColor = [[UIColor blueColor] CGColor];
phoneTextField.borderStyle = UITextBorderStyleNone;
abbood
  • 23,101
  • 16
  • 132
  • 246
0

Just a small addition. If you make the border a bit wider, it will interfere with the left and right side of text. To avoid that, I added the following line:

self.someTextView.textContainerInset = UIEdgeInsetsMake(8.0, 8.0, 8.0, 8.0);
Sloba
  • 66
  • 4
0

In Swift 3, you may use the following two lines:

myText.layer.borderColor = UIColor.lightGray.cgColor

myText.layer.borderWidth = 1.0
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Amr Angry
  • 3,711
  • 1
  • 45
  • 37
0

An elegant solution would be to insert a real UITextField on the bottom and prevent it to scroll with the content. This way you have even the correct dark mode borders.

class BorderedTextView: UITextView {

  let textField = UITextField()

  required init?(coder: NSCoder) {
    super.init(coder: coder)
    insertTextField()
  }

  override init(frame: CGRect, textContainer: NSTextContainer?) {
    super.init(frame: frame, textContainer: textContainer)
    insertTextField()
  }

  convenience init() {
    self.init(frame: .zero, textContainer: nil)
  }

  private func insertTextField() {
    delegate = self
    textField.borderStyle = .roundedRect
    insertSubview(textField, at: 0)
  }

  override func layoutSubviews() {
    super.layoutSubviews()
    textField.frame = bounds
  }
}

extension BorderedTextView: UITextViewDelegate {

  func scrollViewDidScroll(_ scrollView: UIScrollView) {
    textField.frame = bounds
  }
}
RyuX51
  • 2,779
  • 3
  • 26
  • 33
  • Does the UITextView show the borders when the view controller containing the UITextView is presented? It's not showing up for me, even if I set the values of UITextView layer.borderColor to UIColor.black.CGColor and layer.borderWidth to 1.0 in viewWillAppear(). – daniel Feb 11 '23 at 08:02
-3

I solved this problem in storyboard by putting a fully disabled UIButton behind the UITextView and making the background color of the UITextView clearColor. This works without requiring extra code or packages.

Venk
  • 5,949
  • 9
  • 41
  • 52
Brandon
  • 19
  • 5