UIView设置圆角

1、比较简单的情况,UIView四个角都是圆角:

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

    aView.frame = CGRectMake(0, 0, 300, 200);
    aView.backgroundColor = [UIColor redColor];

    //设置圆角边框

    aView.layer.cornerRadius = 8;

    aView.layer.masksToBounds = YES;

    //设置边框及边框颜色

    aView.layer.borderWidth = 8;

    aView.layer.borderColor =[ [UIColor grayColor] CGColor];

    [self.view addSubview:aView];

2、设置四个角中的某个或者某几个为圆角

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

    aView.frame = CGRectMake(0, 0, 300, 200);
    aView.backgroundColor = [UIColor redColor];

    [self.view addSubview:aView];

    //设置所需的圆角位置以及大小
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:aView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = aView.bounds;
    maskLayer.path = maskPath.CGPath;
    aView.layer.mask = maskLayer;

其中,UIRectCornerBottomLeft,UIRectCornerBottomRight是可以选择的角。

typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
    UIRectCornerTopLeft     = 1 << 0,
    UIRectCornerTopRight    = 1 << 1,
    UIRectCornerBottomLeft  = 1 << 2,
    UIRectCornerBottomRight = 1 << 3,
    UIRectCornerAllCorners  = ~0UL
};

上面的枚举是可以供选择的角,分别是:“左上角”、“右上角”、“左下角”、“右下角”。

时间: 2024-07-31 19:49:11

UIView设置圆角的相关文章

iOS UIView设置圆角

UIView设置圆角 1.比较简单的情况,UIView四个角都是圆角: UIView *aView = [[UIView alloc] init]; aView.frame = CGRectMake(0, 0, 300, 200); aView.backgroundColor = [UIColor redColor]; //设置圆角边框 aView.layer.cornerRadius = 8; aView.layer.masksToBounds = YES; //设置边框及边框颜色 aView

IOS-tableView 设置圆角

// 设置 tableView整体的圆角设置    //    /// 设置圆角//    v_tableview.layer.cornerRadius = 7;//    v_tableview.layer.masksToBounds = YES;//    /// 设置边框//    v_tableview.layer.borderWidth = 1;//    v_tableview.layer.borderColor = [COLOR(189, 189, 189, 1) CGColor]

iOS设置圆角的四种方法

一.设置CALayer的cornerRadius cornerRadius属性影响layer显示的background颜色和前景框border,对layer的contents不起作用.故一个imgView(类型为UIImageView)的image不为空,设置imgView.layer的cornerRadius,是看不出显示圆角效果的,因为image是imgView.layer的contents部分. 这种情况下将layer的masksToBounds属性设置为YES,可以正确的绘制出圆角效果.

iOS之用xib给控件设置圆角、边框效果

xib中为各种控件设置圆角 通过代码的方式设置 @interface ViewController () @property (weak, nonatomic) IBOutlet UIView *myView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.myView.layer.masksToBounds = YES; self.myView.layer.cornerRa

xib设置lable设置圆角和边框,颜色

//设置圆角 layer.cornerRadius ,注意该 key 对应 Value 的 type 应该设置为 String layer.masksToBounds ,注意该 key 对应 Value 的 type 应该设置为 Boolean , 当右侧出现对号时为YES *注意*:经过测试,UILabel 必须设置设置 masksToBounds 这一键值对,才会出现圆角效果:UIButton.UIView.UIImageView 只需设置 layer.cornerRadius 这一键值对就

IOS 设置圆角用户头像

在App中有一个常见的功能,从系统相册或者打开照相机得到一张图片,然后作为用户的头像.从相册中选取的图片明明都是矩形的图片,但是展示到界面上却变成圆形图片,这个神奇的效果是如何实现的呢? 请大家跟着下面的步骤,去实现选取并展示圆角头像的功能吧! 一.设置显示头像的圆角图片 1. 显示用户头像用UIImageView实现,添加默认图片后效果如下图所示,头像显示为矩形图片. 代码实现: // ViewController.m // SetUserImage // // Created by jere

ios开发storyboard设置圆角按钮

1.很多人都知道,通常设置一个 Button后者其他的UIView子类的圆角,需要使用如下的语句 <span style="font-family:SimSun;font-size:18px;">self.button.layer.cornerRadius=10;//即可 </span> <span style="font-family:SimSun;font-size:18px;"> </span> 说明:这会用到l

iOS图片设置圆角

一般我们在iOS开发的过程中设置圆角都是如下这样设置的. imageView.clipsToBounds = YES; [imageView.layer setCornerRadius:50]; 这样设置会触发离屏渲染,比较消耗性能.比如当一个页面上有十几头像这样设置了圆角 会明显感觉到卡顿. 这种就是最常用的,也是最耗性能的. 注意:ios9.0之后对UIImageView的圆角设置做了优化,UIImageView这样设置圆角 不会触发离屏渲染,ios9.0之前还是会触发离屏渲染.而UIBut

Java 图片设置圆角(设置边框,旁白)

/** * 图片设置圆角 * @param srcImage * @param radius * @param border * @param padding * @return * @throws IOException */ public static BufferedImage setRadius(BufferedImage srcImage, int radius, int border, int padding) throws IOException{ int width = srcI