如何为UITextView设置圆角?

8
如何为UITextView设置圆角?
4个回答

20
首先导入文件。
#import <QuartzCore/QuartzCore.h>

然后设置你的文本视图的属性

yourTextViewName.layer.cornerRadius = kCornerRadius;

其中kCornerRadius是您设置的圆角半径常量。


我遇到了一个错误:“使用未声明的标识符kCornerRadius”。 - Jared Chu

5

请尝试这个,它一定会有效。

您需要导入以下内容:

QuartzCore/QuartzCore.h


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

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];

这个问题很久以前就发布了.. :) - Arun Abraham

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


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