2012-06-12 226 views
3

如何使用borderwidth和borderColor制作半角圆角(顶角圆角)textview或tableview? enter image description here如何制作带边框的半圆角(顶角圆角)texview?

+0

请参考以下链接: HTTP://计算器。 com/questions/1824463/how-to-style-uitextview-to-like-rounded-rect-text-field – Dee

+0

http://stackoverflow.com/a/10167334/653513 –

+0

@Dee:OP只想绕一定角度这必须通过使用'bezierPathWithRoundedRect' –

回答

0

我想用波纹管代码

[yourTextView.layer setBorderColor: [[UIColor redColor] CGColor]]; 
    [yourTextView.layer setBorderWidth: 1.0]; 
    [yourTextView.layer setCornerRadius:8.0f]; 
    [yourTextView.layer setMasksToBounds:YES]; 

也使用yourTableView insted的yourTextView

+0

不,我只需要一些像素边框圆角的顶角。 – Cintu

2

这不是完美的,但你可以从这个工作:

#import <QuartzCore/CoreAnimation.h> 

(也链接到您的项目QuartzCore.framework),然后..

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

我使用的是相同的代码,但仍然不完美。请参阅屏幕截图。 – Cintu

1

首先删除所有代码,将产生一个边界(厦门国际银行或layer.borderWidth),并使用下面这个方法我在一个UIView类创建:

#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