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.
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:
@1x
@2x
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
}
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
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;
Add the following for rounded corners:
self.yourUITextview.layer.cornerRadius = 8;
In Swift 3, you may use the following two lines:
myText.layer.borderColor = UIColor.lightGray.cgColor
myText.layer.borderWidth = 1.0