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及更高版本上运行,但我想现在它仍然是事实上的平台.
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)
Sea*_*ory 34
在界面生成器中设置文本视图后.
@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)
@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)
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)
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)
这些是我使用的图像:

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+上.
| 归档时间: |
|
| 查看次数: |
132864 次 |
| 最近记录: |