如何制作一个半圆角(上部圆角)文本视图并带边框?

3
如何制作带有边框宽度和边框颜色的半圆角(顶部圆角)文本视图或表格视图?enter image description here

https://dev59.com/t2kw5IYBdhLWcg3wDWTL#10167334 - Rok Jarc
我正在使用与您链接相同的内容。但是当我设置边框宽度和边框颜色时,角落的边框颜色被裁剪掉了。 - Cintu
@rokjarc:我使用了bezierPathWithRoundedRect,但是当我添加边框时内部部分没有被圆角化。我也附上了屏幕截图。 - Cintu
尝试这个:textView.layer.borderWidth = 2.0f; // 这个值根据你的需要而变化 textView.layer.borderColor = [[UIColor greenColor] CGColor]; textView.layer.masksToBounds = YES; textView.layer.cornerRadius = 12.0f; // 这个值根据你的需要而变化 - Dee
@Dee:我只需要某些角落是圆角而不是所有的角落。根据您的评论,所有的角落都会被圆角化。 - Cintu
显示剩余4条评论
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];

1
我正在使用相同的代码,但仍然无法完美地进行圆角处理。请查看屏幕截图。 - Cintu

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

0

我认为使用下面的代码

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

应该使用yourTableView而不是yourTextView


不,我只需要顶部圆角并带有一些像素边框。 - Cintu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接