Arun Abraham
Arun Abraham

Reputation: 4037

how to set rounded corner for a UITextView?

how to set rounded corner for a UITextView ?

Upvotes: 8

Views: 9432

Answers (4)

coders
coders

Reputation: 2477

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

enter image description here

Upvotes: 0

David.Chu.ca
David.Chu.ca

Reputation: 38654

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

Upvotes: 1

Gyani
Gyani

Reputation: 2241

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

Upvotes: 20

Suresh Varma
Suresh Varma

Reputation: 9740

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;

Upvotes: 5

Related Questions