开发者

how to set rounded corner for a UITextView?

开发者 https://www.devze.com 2023-01-19 16:20 出处:网络
how to set rounded corner fo开发者_如何学Gor a UITextView ?fist import the file #import <QuartzCore/QuartzCore.h>

how to set rounded corner fo开发者_如何学Gor a UITextView ?


fist import the file

#import <QuartzCore/QuartzCore.h>

and then set the property of your text view

yourTextViewName.layer.cornerRadius = kCornerRadius;

where kCornerRadius is a constant you set as a radius for corner


Try this it will work for sure

you have to import

QuartzCore/QuartzCore.h


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


I define an category class for UITextView in .h:

@interface UITextView (RoundedCorner)
-(void) roundedCornerDefault;
-(void) roundedCornerWithRadius:(CGFloat) radius
                borderColor:(CGColorRef) color
                borderWidth:(CGFloat) width;
@end

and the implementation class:

#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

Example to use it:

#import "UITextView+RoundedCorner.h"
...
[self.myTextView roundedCornerDefault];


Work for me as below code and step

  1. create textView var

    @IBOutlet weak var currentAddressOutlet: KMPlaceholderTextView!
    
  2. create this function

    private func setBorderForTextView() {
    self.currentAddressOutlet.layer.borderColor = UIColor.lightGray.cgColor
    self.currentAddressOutlet.layer.borderWidth = 0.5
    self.currentAddressOutlet.layer.cornerRadius = 5
    }
    
  3. call above function in your viewDidLoad

    override func viewDidLoad() {
    super.viewDidLoad()
    setupTable()
    setBorderForTextView()
    } 
    
  4. Result is here

how to set rounded corner for a UITextView?

0

精彩评论

暂无评论...
验证码 换一张
取 消