【问题标题】:How to make a UIView with optional rounded corners and border?如何制作带有可选圆角和边框的 UIView?
【发布时间】:2014-03-27 06:28:17
【问题描述】:

我将圆角半径应用于UIView,即UIRectCornerTopLeftUIRectCornerTopRight。当我应用这个时,边框在角落里消失了。如何避免这种情况?

这就是我对UIView 应用边框的方式:

 [self.middleView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(4, 4)];
 self.middleView.layer.borderWidth = 0.5f;
 self.middleView.layer.borderColor = [[UIColor colorWith8BitRed:0 green:0 blue:0 alpha:0.25]

这是我用于应用可选圆角的类别:

   #import "UIView+Roundify.h"

   @implementation UIView (Roundify)
  - (void)addRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii {
   CALayer *tMaskLayer = [self maskForRoundedCorners:corners withRadii:radii];
   self.layer.mask = tMaskLayer;
   }

  - (CALayer*)maskForRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii {
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;

  UIBezierPath *roundedPath = [UIBezierPath bezierPathWithRoundedRect:
                             maskLayer.bounds byRoundingCorners:corners cornerRadii:radii];
    maskLayer.fillColor = [[UIColor whiteColor] CGColor];
    maskLayer.backgroundColor = [[UIColor clearColor] CGColor];
    maskLayer.path = [roundedPath CGPath];

   return maskLayer;
 }

【问题讨论】:

  • 可选的圆角和边框是什么意思?
  • @codes 尝试将strokeColor 设置为CAShapeLayer *maskLayer
  • @Akhilrajtr :添加了这个 - maskLayer.strokeLayer = [[UIColor blackColor] CGColor]; ...但没有工作

标签: ios objective-c uiview


【解决方案1】:

试试下面的代码吧

您要舍入 TopLeft 和 TopRight 的视图

   UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)];
   [view1 setBackgroundColor:[UIColor grayColor]];
   [self.view addSubview:view1];

如下代码设置角

UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:view1.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(5.0, 5.0)];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path = maskPath.CGPath;
view1.layer.mask = maskLayer;

输出是:

【讨论】:

  • 我可以得到圆角,但无法将边框应用于此视图。边框消失在角落
【解决方案2】:

找到了这段代码。还没有真正尝试过,但似乎是你需要的。

- (void)drawDashedBorderAroundView:(UIView *)v {

    //border definitions
    CGFloat cornerRadius = 10;
    CGFloat borderWidth = 2;
    NSInteger dashPattern1 = 8;
    NSInteger dashPattern2 = 8;
    UIColor *lineColor = [UIColor orangeColor];

    //drawing
    CGRect frame = v.bounds;

    CAShapeLayer *_shapeLayer = [CAShapeLayer layer];

    //creating a path
    CGMutablePathRef path = CGPathCreateMutable();

    //drawing a border around a view
    CGPathMoveToPoint(path, NULL, 0, frame.size.height - cornerRadius);
    CGPathAddLineToPoint(path, NULL, 0, cornerRadius);
    CGPathAddArc(path, NULL, cornerRadius, cornerRadius, cornerRadius, M_PI, -M_PI_2, NO);
    CGPathAddLineToPoint(path, NULL, frame.size.width - cornerRadius, 0);
    CGPathAddArc(path, NULL, frame.size.width - cornerRadius, cornerRadius, cornerRadius, -M_PI_2, 0, NO);
    CGPathAddLineToPoint(path, NULL, frame.size.width, frame.size.height - cornerRadius);
    CGPathAddArc(path, NULL, frame.size.width - cornerRadius, frame.size.height - cornerRadius, cornerRadius, 0, M_PI_2, NO);
    CGPathAddLineToPoint(path, NULL, cornerRadius, frame.size.height);
    CGPathAddArc(path, NULL, cornerRadius, frame.size.height - cornerRadius, cornerRadius, M_PI_2, M_PI, NO);

    //path is set as the _shapeLayer object's path
    _shapeLayer.path = path;
    CGPathRelease(path);

    _shapeLayer.backgroundColor = [[UIColor clearColor] CGColor];
    _shapeLayer.frame = frame;
    _shapeLayer.masksToBounds = NO;
    [_shapeLayer setValue:[NSNumber numberWithBool:NO] forKey:@"isCircle"];
    _shapeLayer.fillColor = [[UIColor clearColor] CGColor];
    _shapeLayer.strokeColor = [lineColor CGColor];
    _shapeLayer.lineWidth = borderWidth;
    _shapeLayer.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:dashPattern1], [NSNumber numberWithInt:dashPattern2], nil];
    _shapeLayer.lineCap = kCALineCapRound;

    //_shapeLayer is added as a sublayer of the view, the border is visible
    [v.layer addSublayer:_shapeLayer];
    v.layer.cornerRadius = cornerRadius;
}

这段代码添加了一条虚线,但您可以通过_shapeLayer.lineDashPattern 对其进行修改。

【讨论】:

    【解决方案3】:

    除非有一些我们不知道的特定要求,否则如果您想要做的只是圆角并有边框,则贝塞尔路径和蒙版是不必要的。我通常会这样做: myView.layer.borderWidth=2; myView.layer.cornerRadius=5;

    您是否只希望顶角圆角而不需要使用图层圆角?如果是这样,为什么不使用它然后覆盖一个薄视图以覆盖底部位?有点繁琐,但我发现您越能依靠标准控件来绘制自己,而不必进入核心图形,效果就越好。

    编辑:好的,考虑到它需要不使底角变圆,如果您在 UIView 上有一个具有 2 个子视图的类别怎么样:1 个具有 4 个圆角,另一个位于顶部(self bringSubviewToFront) 仅用非圆角条覆盖圆角视图的“页脚”,即宽度相等且高度很小的视图,等于圆角半径。如果您有纯色背景,则只需使两个子视图相同;如果您有一些纹理或图像背景,请使它们都透明并将纹理/图像放在超级视图(使用您的类别特定布局方法的父视图)上。最后,将边框放在同一个超级视图上。应该工作,让我知道你的想法。

    【讨论】:

    • 是的,我的要求是特定的。我在我的应用程序中的多个视图中使用此类别,其中只有前两个角要用边框进行圆角。我可以获得圆角但无法应用边界到那些
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    • 2013-10-14
    • 2016-08-18
    • 2012-02-17
    • 2011-05-26
    • 2012-06-09
    相关资源
    最近更新 更多