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.
#import <QuartzCore/QuartzCore.h>
....
// typically inside of the -(void) viewDidLoad method
self.yourUITextView.layer.borderWidth = 5.0f;
self.yourUITextView.layer.borderColor = [[UIColor grayColor] CGColor];
Works great, but the color should be a CGColor
, not UIColor
:
view.layer.borderWidth = 5.0f;
view.layer.borderColor = [[UIColor grayColor] CGColor];
you can add border to UITextView from the Storyboard - Identity Inspector - User Defined Runtime Attribute
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
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);
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.