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.

- 5,009
- 16
- 47
- 81

- 5,499
- 7
- 25
- 28
15 Answers
#import <QuartzCore/QuartzCore.h>
....
// typically inside of the -(void) viewDidLoad method
self.yourUITextView.layer.borderWidth = 5.0f;
self.yourUITextView.layer.borderColor = [[UIColor grayColor] CGColor];

- 74,769
- 26
- 128
- 150
-
62I 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
-
2Did 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
Add the following for rounded corners:
self.yourUITextview.layer.cornerRadius = 8;

- 5,396
- 4
- 31
- 54

- 2,651
- 2
- 25
- 29
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:
Easy peasy.

- 27,846
- 7
- 149
- 159
I add UIImageView
as a subview of the UITextView
. This matches the native border on a UITextField
, including the gradient from top to bottom:
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:

- 5,949
- 9
- 41
- 52

- 26,102
- 25
- 102
- 183
-
-
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
-
3With 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
Works great, but the color should be a CGColor
, not UIColor
:
view.layer.borderWidth = 5.0f;
view.layer.borderColor = [[UIColor grayColor] CGColor];

- 5,949
- 9
- 41
- 52

- 9,757
- 2
- 65
- 61
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!

- 6,492
- 9
- 46
- 76

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

- 7,073
- 7
- 61
- 71
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
}

- 8,880
- 5
- 40
- 58
you can add border to UITextView from the Storyboard - Identity Inspector - User Defined Runtime Attribute

- 9,404
- 1
- 52
- 40
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

- 1,441
- 2
- 19
- 31
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;

- 23,101
- 16
- 132
- 246
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);

- 66
- 4
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
}
}

- 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
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.
-
3Of course, this doesn't work in iOS 7, as the buttons have no edges (the buttons look like labels now) – Mike Gledhill Apr 08 '14 at 07:43