美文网首页
切圆角总结

切圆角总结

作者: ios_stand | 来源:发表于2017-12-02 17:32 被阅读0次

UIView(不包括其子类)

UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor blackColor];
view.layer.cornerRadius = 3.f;
// 以下两行,任写一行或者一行都不写(两属性默认都是NO)
//view.layer.masksToBounds = NO;
//view.clipToBounds = NO;
// 以下两行,千万不要加!
//view.layer.masksToBounds = YES;
//view.clipToBounds = YES;

注意点:UIView 只要设置图层的 cornerRadius 属性即可(不明白的话,可以看看官方文档里对 cornerRadius 的描述),如果设置 layer.masksToBounds = YES,会造成不必要的离屏渲染。

UITextField

//UITextField有两种实现方法

// 天然支持设置圆角边框
UITextField *textField = [[UITextField alloc] init];
textField.borderStyle = UITextBorderStyleRoundedRect;

// 与 UIView 类似
UITextField *textField = [[UITextField alloc] init];
textField.layer.cornerRadius = cornerRadius;

UITextView

// 与 UIView 类似
UITextView *textView = [[UITextView alloc] init];
textView.layer.cornerRadius = cornerRadius;

UILabel

UILabel *label = [[UILabel alloc] init];
// 重点在此!!设置视图的图层背景色,千万不要直接设置 label.backgroundColor
label.layer.backgroundColor = [UIColor grayColor].CGColor;
label.layer.cornerRadius = cornerRadius;

UIButton

//无背景图片,与 UIView 类似
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor redColor];
btn.layer.cornerRadius = 8.0f;

//有背景图片,如果是复杂的图片,可以依靠 UI 切图来实现
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:image forState:UIControlStateNormal];

UIImageView

贝塞尔曲线切割圆角(UIImageView添加分类)

- (UIImageView *)roundedRectImageViewWithCornerRadius:(CGFloat)cornerRadius {
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornerRadius];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = bezierPath.CGPath;
    self.layer.mask = layer;

    return self;
}

图片切割圆角(UIImage添加分类)

- (UIImage *)circleImage
{
    // NO代表透明
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
    // 获得上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 添加一个圆
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextAddEllipseInRect(ctx, rect);
    // 裁剪
    CGContextClip(ctx);
    // 将图片画上去
    [self drawInRect:rect];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    // 关闭上下文
    UIGraphicsEndImageContext();
    return image;
}

当一个页面只有少量圆角图片时才推荐使用

UIImageView *imageView = [[UIImageView alloc] init];
imageView.layer.cornerRadius = 5.f;
imageView.layer.masksToBounds = YES;
屏幕截屏
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //把控件上的图层渲染到上下文,layer只能渲染
    //[self.view.layer drawInContext:ctx];
    [self.view.layer renderInContext:ctx];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

相关文章

  • iOS 常用组件-高效切圆角方法总结

    iOS 常用组件-高效切圆角方法总结 iOS 常用组件-高效切圆角方法总结

  • 切某个位置圆角

    切某个位置圆角

  • 切View圆角

    切view的上左 上右圆角

  • 给UIView切任意圆角

    可以切任意圆角,效果如图

  • css扩展

    1.background-radius 背景切脚 切圆角 将边框从直角转换为圆角 格式:border-radius...

  • 圆角

    圆角 在很多地方,圆角是一种常用的视图效果。它会让你整个页面显得更佳优美柔和。这里结合日常运用,总结一下设置圆角的...

  • iOS核心动画高级技巧三(视觉效果)

    目录 圆角图层边框阴影图层蒙版拉伸过滤组透明总结 一 圆角 CALayer有一个叫做conrnerRadius的属...

  • iOS开发--切指定圆角的方法

    工作中经常需要将一个图形或者控件切为圆角,切为圆形或者四角都切为圆角的方法很常见也很简单,这里着重介绍一下切指定边...

  • 切圆角总结

    UIView(不包括其子类) 注意点:UIView 只要设置图层的 cornerRadius 属性即可(不明白的话...

  • Android 中常见切圆角的方式

    Android 切圆角的方式 Android 中有哪些可以切圆角的实现方式呢? 本文总结一下常用的方式。以下内容分...

网友评论

      本文标题:切圆角总结

      本文链接:https://www.haomeiwen.com/subject/aaxnbxtx.html