如何将UITextview设置为喜欢Rounded Rect文本字段?

har*_*alb 169 uitextview ios

我使用文本视图作为评论作曲家.

在属性检查器中,我找不到任何类似边框样式属性的东西,以便我可以使用圆角矩形,类似于UITextField.

所以,问题是:如何风格UITextView像一个UITextField带有圆角的矩形?

luv*_*ere 283

没有必须选择的隐式样式,它涉及使用QuartzCore框架编写一些代码:

//first, you
#import <QuartzCore/QuartzCore.h>

//.....

//Here I add a UITextView in code, it will work if it's added in IB too
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 220, 200, 100)];

//To make the border look very close to a UITextField
[textView.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[textView.layer setBorderWidth:2.0];

//The rounded corner part, where you specify your view's corner radius:
textView.layer.cornerRadius = 5;
textView.clipsToBounds = YES;
Run Code Online (Sandbox Code Playgroud)

它只能在OS 3.0及更高版本上运行,但我想现在它仍然是事实上的平台.

  • 我发现添加以下顶部上面的代码使得边框看起来非常接近UITextField.[textView.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]]; [textView.layer setBorderWidth:2.0]; (46认同)
  • 确保:#import <QuartzCore/QuartzCore.h>.我有问题,因为我没有导入QuartzCore (11认同)
  • iOS 7 - borderWidth 1.0和alpha .2适用于默认样式. (4认同)

han*_*Dev 76

这段代码对我很有用:

    [yourTextView.layer setBackgroundColor: [[UIColor whiteColor] CGColor]];
    [yourTextView.layer setBorderColor: [[UIColor grayColor] CGColor]];
    [yourTextView.layer setBorderWidth: 1.0];
    [yourTextView.layer setCornerRadius:8.0f];
    [yourTextView.layer setMasksToBounds:YES];
Run Code Online (Sandbox Code Playgroud)

  • 我会说5px更接近文本字段,但这个答案的灰色比选择的答案要好. (3认同)
  • 当然,您需要为此答案添加 QuartzCore 框架并通过 #import &lt;QuartzCore/QuartzCore.h&gt; 导入它 (2认同)

Sea*_*ory 34

Swift 3版

在界面生成器中设置文本视图后.

@IBOutlet weak var textView: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()
    textView.layer.cornerRadius = 5     
    textView.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor
    textView.layer.borderWidth = 0.5  
    textView.clipsToBounds = true
}
Run Code Online (Sandbox Code Playgroud)

Swift 2.2版本

@IBOutlet weak var textView: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()
    textView.layer.cornerRadius = 5     
    textView.layer.borderColor = UIColor.grayColor().colorWithAlphaComponent(0.5).CGColor
    textView.layer.borderWidth = 0.5  
    textView.clipsToBounds = true
}
Run Code Online (Sandbox Code Playgroud)

  • 使 .borderWidth = 0.7 使它看起来更接近我在 iOS 11 上看到的当前样式。 (2认同)

Sur*_*rma 27

编辑:您必须导入

#import <QuartzCore/QuartzCore.h>
Run Code Online (Sandbox Code Playgroud)

使用角半径.

试试这个肯定会起作用

UITextView* txtView = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 300, 100)];
txtView.layer.cornerRadius = 5.0;
txtView.clipsToBounds = YES;
Run Code Online (Sandbox Code Playgroud)

正如Rob想象的那样设置你想要的边框颜色是否相似UITextField然后需要通过添加以下行将边框宽度更改为2.0并将颜色更改为灰色

[textView.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]]; 
[textView.layer setBorderWidth:2.0];
Run Code Online (Sandbox Code Playgroud)

  • 我想知道为什么这不起作用。感谢您提供有关 QuartzCore 的提示。 (2认同)

Ben*_*ard 22

我想要真正的交易,所以我添加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];
Run Code Online (Sandbox Code Playgroud)

这些是我使用的图像:

在此输入图像描述 在此输入图像描述

  • 不好,滚动UITextView时背景会移动.猜猜我必须将它全部放在父UIView中. (4认同)
  • 我认为你还需要告诉 textView 有 UITextBorderStyle None 如果你还没有在 XCode 中设置它 (2认同)

Phi*_*hil 11

一种解决方案是在UITextView下面添加一个UITextField,使UITextView背景透明并禁用任何用户交互UITextField.然后在代码UITextField中用这样的东西改变框架

self.textField.frame = CGRectInset(self.textView.frame, 0, -2);
Run Code Online (Sandbox Code Playgroud)

您将拥有与文本字段完全相同的外观.

正如Jon所建议的那样,你应该把这段代码[UIViewController viewDidLayoutSubviews]放在iOS 5.0+上.


ken*_*ytm 5

为了获得最佳效果,您必须使用自定义(可拉伸)背景图像。这也是UITextField的圆形边框的绘制方式。