【发布时间】:2010-04-15 16:31:57
【问题描述】:
我想在UITextView 周围有一个细灰色边框。我浏览了 Apple 文档,但在那里找不到任何属性。请帮忙。
【问题讨论】:
标签: ios quartz-core
我想在UITextView 周围有一个细灰色边框。我浏览了 Apple 文档,但在那里找不到任何属性。请帮忙。
【问题讨论】:
标签: ios quartz-core
#import <QuartzCore/QuartzCore.h>
....
// typically inside of the -(void) viewDidLoad method
self.yourUITextView.layer.borderWidth = 5.0f;
self.yourUITextView.layer.borderColor = [[UIColor grayColor] CGColor];
【讨论】:
self.myView.layer.borderWidth ...,但正如我所说,图层是只读的,所以图层没有任何方法或变量要设置
为圆角添加以下内容:
self.yourUITextview.layer.cornerRadius = 8;
【讨论】:
这是我用来在名为“tbComments”的TextView 控件周围添加边框的代码:
self.tbComments.layer.borderColor = [[UIColor grayColor] CGColor];
self.tbComments.layer.borderWidth = 1.0;
self.tbComments.layer.cornerRadius = 8;
这就是它的样子:
简单易懂。
【讨论】:
我将UIImageView 添加为UITextView 的子视图。这与 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];
这些是我使用的 png 图像,以及 jpg 表示:
【讨论】:
code resizableImageWithCapInsets:UIEdgeInsetsMake(28, 14, 28, 14)
效果很好,但颜色应该是CGColor,而不是UIColor:
view.layer.borderWidth = 5.0f;
view.layer.borderColor = [[UIColor grayColor] CGColor];
【讨论】:
我相信上面的答案是针对以前版本的 Swift 的。我用谷歌搜索了一下,下面的代码适用于 Swift 4。只需将它分享给任何可能受益的人。
self.textViewName.layer.borderColor = UIColor.lightGray.cgColor
self.textViewName.layer.borderWidth = 1.0
self.textViewName.layer.cornerRadius = 8
编码愉快!
【讨论】:
self.textViewName.layer.borderColor = UIColor.systemGray4.cgColor
对于 Swift 编程,使用这个
tv_comment.layer.borderWidth = 2
tv_comment.layer.borderColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1).CGColor
【讨论】:
UIColor(white: 0.2, alpha: 1).CGColor
这与原始 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
}
【讨论】:
从 iOS 8 和 Xcode 6 开始,我现在发现最好的解决方案是将 UITextView 子类化并将子类标记为 IB_DESIGNABLE,这样您就可以在故事板中查看边框。
标题:
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface BorderTextView : UITextView
@end
实施:
#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
然后在storyboard中拖出你的UITextView并将它的类设置为BorderTextView
【讨论】:
使它起作用的事情(除了遵循这里的答案)是添加borderStyle 属性:
#import <QuartzCore/QuartzCore.h>
..
phoneTextField.layer.borderWidth = 1.0f;
phoneTextField.layer.borderColor = [[UIColor blueColor] CGColor];
phoneTextField.borderStyle = UITextBorderStyleNone;
【讨论】:
只是一个小小的补充。如果你把边框弄宽一点,它会干扰文本的左侧和右侧。为了避免这种情况,我添加了以下行:
self.someTextView.textContainerInset = UIEdgeInsetsMake(8.0, 8.0, 8.0, 8.0);
【讨论】:
在 Swift 3 中,您可以使用以下两行代码:
myText.layer.borderColor = UIColor.lightGray.cgColor
myText.layer.borderWidth = 1.0
【讨论】:
一个优雅的解决方案是在底部插入一个真正的 UITextField 并防止它随着内容滚动。这样,您甚至可以拥有正确的暗模式边框。 ?
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
}
}
【讨论】:
我在故事板中解决了这个问题,方法是在UITextView 后面放置一个完全禁用的UIButton,并使UITextView 的背景颜色为clearColor。这无需额外的代码或包即可工作。
【讨论】: