如何为UITextView设置圆角?

时间:2010-10-13 11:11:03

标签: iphone objective-c

如何为UITextView设置圆角?

4 个答案:

答案 0 :(得分:20)

拳头导入文件

#import <QuartzCore/QuartzCore.h>

然后设置文本视图的属性

yourTextViewName.layer.cornerRadius = kCornerRadius;

其中kCornerRadius是一个常数,你设置为角落的半径

答案 1 :(得分:5)

试试这个肯定会起作用

你必须导入

QuartzCore/QuartzCore.h


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

答案 2 :(得分:1)

我在.h:

中为UITextView定义了一个类别类
@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];

答案 3 :(得分:0)

按照以下代码和步骤为我工作

  1. 创建textView变量

    @IBOutlet weak var currentAddressOutlet: KMPlaceholderTextView!
    
  2. 创建此功能

    private func setBorderForTextView() {
    self.currentAddressOutlet.layer.borderColor = UIColor.lightGray.cgColor
    self.currentAddressOutlet.layer.borderWidth = 0.5
    self.currentAddressOutlet.layer.cornerRadius = 5
    }
    
  3. 在viewDidLoad中调用上述函数

    override func viewDidLoad() {
    super.viewDidLoad()
    setupTable()
    setBorderForTextView()
    } 
    
  4. 结果在这里

enter image description here