3

如何使用borderwidth和borderColor制作半圆角(顶角圆角)textview或tableview?在此处输入图像描述

4

3 回答 3

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];
于 2012-06-12T11:45:56.823 回答
1

首先删除所有会生成边框的代码(xib 或 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
于 2014-03-07T17:53:37.597 回答
0

我想使用下面的代码

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

也使用 yourTableView insted yourTextView

于 2012-06-12T11:39:00.273 回答