如何为UITextView设置圆角?
#import <QuartzCore/QuartzCore.h>
然后设置你的文本视图的属性
yourTextViewName.layer.cornerRadius = kCornerRadius;
其中kCornerRadius
是您设置的圆角半径常量。
请尝试这个,它一定会有效。
您需要导入以下内容:
QuartzCore/QuartzCore.h
UITextView* txtView = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 300, 100)];
txtView.layer.cornerRadius = 5.0;
txtView.clipsToBounds = YES;
@interface UITextView (RoundedCorner)
-(void) roundedCornerDefault;
-(void) roundedCornerWithRadius:(CGFloat) radius
borderColor:(CGColorRef) color
borderWidth:(CGFloat) width;
@end
以及实现类:
#import <QuartzCore/QuartzCore.h>
#import "UITextView+RoundedCorner.h"
@implementation UITextView (RoundedCorner)
-(void) roundedCornerDefault {
[self roundedCornerWithRadius:10
borderColor:[[UIColor grayColor] CGColor]
borderWidth:1];
}
-(void) roundedCornerWithRadius:(CGFloat) radius
borderColor:(CGColorRef) color
borderWidth:(CGFloat) width {
self.layer.cornerRadius = radius;
self.layer.borderColor = color;
self.layer.borderWidth = width;
self.clipsToBounds = YES;
}
@end
使用它的示例:
#import "UITextView+RoundedCorner.h"
...
[self.myTextView roundedCornerDefault];
按照以下代码和步骤为我工作
创建textView变量
@IBOutlet weak var currentAddressOutlet: KMPlaceholderTextView!
创建以下函数
private func setBorderForTextView() {
self.currentAddressOutlet.layer.borderColor = UIColor.lightGray.cgColor
self.currentAddressOutlet.layer.borderWidth = 0.5
self.currentAddressOutlet.layer.cornerRadius = 5
}
在viewDidLoad中调用上述函数
override func viewDidLoad() {
super.viewDidLoad()
setupTable()
setBorderForTextView()
}
结果在此处