您的位置:首页 > 产品设计 > UI/UE

UIKit-UIView常用设置

2016-03-11 09:47 441 查看
1. sizeToFit内容自适应
有时候UIView的尺寸不好直接设置,需要根据内容来改变UIView的尺寸,这就需要使用sizeToFit方法。
以UILabel为例,使用sizeToFit方法后,可以根据UILabel中字符串大小自动调整UILabel的大小
UIButton使用sizeToFit方法,会根据button标题的大小自动调整UIButton的大小



- (void)viewDidLoad{
[super viewDidLoad];
//零尺寸UILabel
UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];
//设置中心
label.center = CGPointMake(0,self.view.center.y);
//label背景色,便于看其尺寸
[label setBackgroundColor:[UIColorgrayColor]];
//label字体颜色
label.textColor = [UIColorwhiteColor];
//label文字内容
label.text =@"UILabel会根据我的长度变化大小";
//sizeToFit
[label sizeToFit];
//显示label
[self.view addSubview:label];
}
2.UIImageView设置圆形镶嵌边框
//带环形边框的图片
UIImageView *imageView= [[UIImageView alloc]initWithFrame:CGRectMake(0,0,50,50)];
// 边框颜色
[imageView.layersetBorderColor:[UIColorgreenColor].CGColor];
// 边框宽度
[imageView.layersetBorderWidth:2];UIKit-UIView常用设置
// 边框圆角
[imageView.layersetCornerRadius:25];
// 将layer下面的layer遮住
[imageView.layersetMasksToBounds:YES];
3.关于UIImageView的图片资源加载问题
参考文章:http://blog.sina.com.cn/s/blog_60e2dbcf01014bfm.html
// 初始化
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(100,200,120,120)];
// 需要设置图片UIImage:
//第一种:
[imageView setImage:[UIImageimageNamed:@"image.jpeg"]];
//第二种:
NSString *filePath=[[NSBundlemainBundle]pathForResource:@"image"ofType:@"jpeg"];
UIImage *images=[UIImageimageWithContentsOfFile:filePath];
//[imageViewsetImage:images];
//第三种:
NSData *data=[NSDatadataWithContentsOfFile:filePath];
UIImage *image2=[UIImageimageWithData:data];
[imageViewsetImage:image2];
其中第一 第二种属于一种,共两种:
1)用imageNamed的方式加载时,系统会把图像Cache到内存。如果图像比较大,或者图像比较多,用这种方式会消耗很大的内存,而且释放图像的内存是一件相对来说比较麻烦的事情。例如:如果利用imageNamed的方式加载图像到一个动态数组NSMutableArray,然后将将数组赋予一个UIView的对象的animationImages进行逐帧动画,那么这将会很有可能造成内存泄露。并且释放图像所占据的内存也不会那么简单。但是利用imageNamed加载图像也有自己的优势。对于同一个图像系统只会把它Cache到内存一次,这对于图像的重复利用是非常有优势的。例如:你需要在一个TableView里重复加载同样一个图标,那么用imageNamed加载图像,系统会把那个图标Cache到内存,在Table里每次利用那个图像的时候,只会把图片指针指向同一块内存。这种情况使用imageNamed加载图像就会变得非常有效。
 
2)利用NSData方式加载时,图像会被系统以数据方式加载到程序。当你不需要重用该图像,或者你需要将图像以数据方式存储到数据库,又或者你要通过网络下载一个很大的图像时,请尽量使用imageWithData的方式加载图像。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: