高效切圆角方法

UIView

UIView *view = [[UIView alloc] init];

view.backgroundColor = [UIColor blackColor];

view.layer.cornerRadius =3.f;

// 以下两行,任写一行

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

说明:UIButton 的背景图片,如果是复杂的图片,可以依靠 UI 切图来实现。如果是简单的纯色背景图片,可以利用代码绘制带圆角的图片。

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

// 设置 UIButton 的背景图片。

[button setBackgroundImage:image forState:UIControlStateNormal];

背景图片绘制方法

+ (UIImage *)pureColorImageWithSize:(CGSize)size color:(UIColor *)color cornRadius:(CGFloat)cornRadius {

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f,0.0f, size.width, size.height)];

view.backgroundColor = color;

view.layer.cornerRadius = cornerRadius;

// 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数是屏幕密度

UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);

[view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

returnimage;

}


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;

returnself;

}

绘制四个角的遮罩(使用场景受限)

在 UIImageView 上添加一个四个角有内容,其它部分是透明的视图,只对 UIImageView 圆角部分进行遮挡。但要保证被遮挡的部分背景色要与周围背景相同,避免穿帮。所以当 UIImageView 处于一个复杂的背景时,是不适合使用这个方法的。

最不推荐做法(当一个页面只有少量圆角图片时才推荐使用)

UIImageView *imageView = [[UIImageView alloc] init];

imageView.layer.cornerRadius =5.f;

imageView.layer.masksToBounds = YES;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 iOS 客户端开发中,经常碰到圆角视图的需求,本文简单总结一下 UIView 及其子类的一些切圆角方法,并且...
    郑一一一一阅读 14,199评论 19 70
  • //设置尺寸为屏幕尺寸的时候self.window = [[UIWindow alloc] initWithFra...
    LuckTime阅读 850评论 0 0
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,158评论 5 13
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,606评论 6 30
  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    十年一品温如言1008阅读 1,763评论 0 3