带边框的UITextView

134

我想在一个UITextView周围加上细灰色的边框。我已经查阅了苹果的文档,但未能找到任何属性。请帮忙。

15个回答

315
#import <QuartzCore/QuartzCore.h>

....

// typically inside of the -(void) viewDidLoad method
self.yourUITextView.layer.borderWidth = 5.0f;
self.yourUITextView.layer.borderColor = [[UIColor grayColor] CGColor];

62
我认为这不需要太多的解释,"view"是指UITextView,在你设置视图的任何地方添加代码(awakeFromNib或viewDidLoad都可以)。由于没有给出代码,所以无法提供良好的上下文。 - Kendall Helmstetter Gelner
XCode 不允许我为图层设置边框。它说该图层是只读的。我创建了一个 UIView(在其中放置一些元素),并尝试为该视图设置边框。我尝试使用 self.myView.layer.borderWidth ... 这样做,但正如我所说,图层是只读的,因此图层没有任何可用于设置的方法或变量。 - Paulius Vindzigelskis
2
你导入了QuartzCore吗?你也可以尝试从self.myView中获取CALayer并在上面设置边框宽度。它是可设置的。 - Kendall Helmstetter Gelner
关于UIView中“layer”属性只读的更多信息(layer属性是只读的,但您可以为视图中的层设置值):http://stackoverflow.com/questions/12837077/isnt-property-layer-of-uiview-be-readonly - Kendall Helmstetter Gelner

44

添加以下代码以实现圆角效果:

self.yourUITextview.layer.cornerRadius = 8; 

27

这是我使用的代码,为我的名为"tbComments"的TextView控件添加边框:

self.tbComments.layer.borderColor = [[UIColor grayColor] CGColor];
self.tbComments.layer.borderWidth = 1.0;
self.tbComments.layer.cornerRadius = 8;

以下是它的外观:

输入图像描述

非常简单。


19
我将UIImageView作为UITextView的子视图添加。这与UITextField上的原生边界匹配,包括从上到下的渐变:

enter image description here

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 表示:

@1x

@2x

enter image description here


好的解决方案。这些图片是你的吗?我们可以自由使用它们吗? - James Webster
这些图像是基于苹果自己应用程序中使用的图像“建模”的。事实上,我提取了它们并且很少或没有进行任何更改。请自行决定是否使用。目标是模仿本地外观和感觉,因此很难想出看起来非常不同的东西。值得一提的是,我已经使用这个图像发布并获得了批准的应用程序,没有任何问题。 - Ben Packard
看起来png图像文件已经被删除了——我不打算不断更新它们的新位置,所以如果jpg对你不起作用,我很抱歉。我可以通过评论在有需求时重新上传。 - Ben Packard
3
使用附带的图片时,以下code代码最佳: resizableImageWithCapInsets:UIEdgeInsetsMake(28, 14, 28, 14) - RefuX
WikiUpload链接(用于两个图像)现在无法找到图像文件。 ;-( - Mike Gledhill

18

代码工作的很好,但颜色应该是CGColor而不是UIColor

view.layer.borderWidth = 5.0f;
view.layer.borderColor = [[UIColor grayColor] CGColor];

4
#import <QuartzCore/QuartzCore.h> - kolinko

8

我相信以上答案适用于早期版本的Swift。我进行了一些谷歌搜索,以下代码可用于Swift 4。只是分享给可能有用的人。

self.textViewName.layer.borderColor = UIColor.lightGray.cgColor
self.textViewName.layer.borderWidth = 1.0
self.textViewName.layer.cornerRadius = 8

愉快编码!


使用Swift5和深色模式,您甚至可以使用系统颜色:self.textViewName.layer.borderColor = UIColor.systemGray4.cgColor - multitudes

4

对于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 - Leo

3

这是我能接近原始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
}

2
您可以从Storyboard的“Identity Inspector”-“User Defined Runtime Attribute”中为UITextView添加边框。请参考下图:enter image description here

0

从iOS 8和Xcode 6开始,我现在发现最好的解决方案是子类化UITextView并将子类标记为IB_DESIGNABLE,这将允许您在Storyboard中查看边框。

标题:

#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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接