ios之如何让图片显示成圆形的样式/设置控件边框大小以及颜色
比如说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];
再来看看layer层还带有什么属性,可以设置什么效果呢,一个是可以设置控件的边框大小以及颜色,有时候可以方便我们对项目控件的测试说白了也就是更加清晰显示出控件的位置把比如说tableView 的cell 设置后可以很清楚看到每个cell的大小范围,其次还可以实现点击让控件有选中的效果,更加高级点就是通过layer层对控件进行动画的绘制
下面是苹果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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。