ios 有边框的 UITextView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2647164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bordered UITextView
提问by Ali
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.
我想在 a 周围有一个细的灰色边框UITextView
。我已经浏览了 Apple 文档,但在那里找不到任何属性。请帮忙。
回答by Kendall Helmstetter Gelner
#import <QuartzCore/QuartzCore.h>
....
// typically inside of the -(void) viewDidLoad method
self.yourUITextView.layer.borderWidth = 5.0f;
self.yourUITextView.layer.borderColor = [[UIColor grayColor] CGColor];
回答by user542584
Add the following for rounded corners:
为圆角添加以下内容:
self.yourUITextview.layer.cornerRadius = 8;
回答by Mike Gledhill
Here's the code I used, to add a border around my TextView
control named "tbComments" :
这是我使用的代码,用于在TextView
名为“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.
十分简单。
回答by Ben Packard
I add UIImageView
as a subview of the UITextView
. This matches the native border on a UITextField
, including the gradient from top to bottom:
我添加UIImageView
为UITextView
. 这匹配 a 上的原生边框UITextField
,包括从上到下的渐变:
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:
这些是我使用的 png 图像和 jpg 表示:
回答by Matt Connolly
Works great, but the color should be a CGColor
, not UIColor
:
效果很好,但颜色应该是CGColor
,而不是UIColor
:
view.layer.borderWidth = 5.0f;
view.layer.borderColor = [[UIColor grayColor] CGColor];
回答by Sruit A.Suk
for Swift Programming, use this
对于 Swift 编程,使用这个
tv_comment.layer.borderWidth = 2
tv_comment.layer.borderColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1).CGColor
回答by IronmanX46
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.
我相信以上答案适用于以前版本的 Swift。我用谷歌搜索了一下,下面的代码适用于 Swift 4。只是分享给任何可能受益的人。
self.textViewName.layer.borderColor = UIColor.lightGray.cgColor
self.textViewName.layer.borderWidth = 1.0
self.textViewName.layer.cornerRadius = 8
Happy Coding!
快乐编码!
回答by Matias Elorriaga
this is as close as I could from an original UITextField
这与原始 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
}
回答by Sloba
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);
回答by Amr Angry
In Swift 3, you may use the following two lines:
在 Swift 3 中,您可以使用以下两行:
myText.layer.borderColor = UIColor.lightGray.cgColor
myText.layer.borderWidth = 1.0