比如说QQ登陆头像显示出来的就是圆形的,但实际上它的图片并非就是圆形,而是通过对layer层进行绘制而成的。说到layer每个控件都会有layer层属性所以可以把任意的控件都可以设置成圆形,或是椭圆型看项目需要而定
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"头像.png"]]; imageView.frame = CGRectMake(100, 100, 100, 100); // 通过layer层设置控件的边角半径(每个控件都会有一个layer层所有其他控件也都能实现圆形效果) imageView.layer.cornerRadius = 50; // 同时也要设置超出部分不让显示 这样就可以达到一个圆形的效果图片- imageView.clipsToBounds = YES; [mainView.view addSubview:imageView];
下面是苹果API
@property CGFloat cornerRadius; //设置边角半径 /* The width of the layer's border, inset from the layer bounds. The * border is composited above the layer's content and sublayers and * includes the effects of the `cornerRadius' property. Defaults to * zero. Animatable. */ @property CGFloat borderWidth; // 设置控件的边框宽度 /* The color of the layer's border. Defaults to opaque black. Colors * created from tiled patterns are supported. Animatable. */ @property CGColorRef borderColor; // 设置控件的边框颜色 /* The opacity of the layer, as a value between zero and one. Defaults * to one. Specifying a value outside the [0,1] range will give undefined * results. Animatable. */ @property float opacity; // 设置控件的透明度有部分动画会用到 /* When true, and the layer's opacity property is less than one, the * layer is allowed to composite itself as a group separate from its * parent. This gives the correct results when the layer contains * multiple opaque components, but may reduce performance. * * The default value of the property is read from the boolean * UIViewGroupOpacity property in the main bundle's Info.plist. If no * value is found in the Info.plist the default value is YES for * applications linked against the iOS 7 SDK or later and NO for * applications linked against an earlier SDK. */
// 只需要这两句话就可以设置完成 imageView.layer.borderWidth = 2; // float类型数据 imageView.layer.borderColor = [UIColor redColor].CGColor; // 颜色需要是CGColor
ios之如何让图片显示成圆形的样式/设置控件边框大小以及颜色
原文地址:http://blog.csdn.net/mr_rog/article/details/41315793