How to make a half rounded (Top corner rounded) textview or tableview with the borderwidth and borderColor?
Asked
Active
Viewed 3,363 times
3

Cintu
- 913
- 2
- 16
- 32
-
Refer this link: http://stackoverflow.com/questions/1824463/how-to-style-uitextview-to-like-rounded-rect-text-field – Dee Jun 12 '12 at 11:01
-
http://stackoverflow.com/a/10167334/653513 – Rok Jarc Jun 12 '12 at 11:06
-
@Dee: OP wants to round only certain corners which has to be done by using `bezierPathWithRoundedRect` – Rok Jarc Jun 12 '12 at 11:09
-
I am using the same as ur links.. But when i am giving the border width and border color, the corner border color is getting chopped off. – Cintu Jun 12 '12 at 11:14
-
@rokjarc: I used bezierPathWithRoundedRect. but when i am giving border inner part is not rounded. I have attached the screen shot too. – Cintu Jun 12 '12 at 11:24
-
Try this: textView.layer.borderWidth = 2.0f; // this value vary as per your desire textView.layer.borderColor = [[UIColor greenColor] CGColor]; textView.layer.masksToBounds = YES; textView.layer.cornerRadius = 12.0f; // this value vary as per your desire – Dee Jun 12 '12 at 11:25
-
@Dee : I need only certain corners to be rounded not all the corners. As per your comment all the corners will be rounded. – Cintu Jun 12 '12 at 11:28
-
@Cintu: does bandejapaisa's answer help you? looks promissing. – Rok Jarc Jun 12 '12 at 11:48
-
I am using the same code. The outer part of the border is rounded and the inner part is still as the square. please check the screen shot in my question. – Cintu Jun 12 '12 at 11:54
3 Answers
2
This isn't perfect, but you can work from this:
#import <QuartzCore/CoreAnimation.h>
(and also link to QuartzCore.framework in your project), then..
self.textView.layer.borderColor = [UIColor redColor].CGColor;
self.textView.layer.borderWidth = 4.0f;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.textView.bounds
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
cornerRadii:CGSizeMake(7.0, 7.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.textView.bounds;
maskLayer.path = maskPath.CGPath;
self.textView.layer.mask = maskLayer;
[maskLayer release];

bandejapaisa
- 26,576
- 13
- 94
- 112
-
1I am using the same code.But still it is not perfectly rounded. Please see the screen shot. – Cintu Jun 12 '12 at 11:49
1
First remove all code that would generate a border (xib or layer.borderWidth) and them use this method below that I created in a UIView Category:
#import "UIView+RoundedCorners.h"
@implementation UIView (RoundedCorners)
#pragma mark - Public
- (void)addBorderWithRoundedCorners:(UIRectCorner)roundedCorners radius:(CGFloat)radius color:(UIColor *)color
{
self.clipsToBounds = NO;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:roundedCorners cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
maskLayer.lineWidth = 1.0;
maskLayer.strokeColor = color.CGColor;
maskLayer.fillColor = [UIColor clearColor].CGColor;
[self.layer addSublayer:maskLayer];
}
@end

Raphael Oliveira
- 7,751
- 5
- 47
- 55
0
i think use bellow code
[yourTextView.layer setBorderColor: [[UIColor redColor] CGColor]];
[yourTextView.layer setBorderWidth: 1.0];
[yourTextView.layer setCornerRadius:8.0f];
[yourTextView.layer setMasksToBounds:YES];
also use yourTableView insted of yourTextView

Paras Joshi
- 20,427
- 11
- 57
- 70