UI控件笔记(二):UI之UILabel和UIImageView
来源:互联网 发布:弥塞拉 知乎 编辑:程序博客网 时间:2024/05/18 00:20
一、UILabel的独特属性们
UILabel *lab = [[UILabelalloc] initWithFrame:CGRectMake(0,20, 320, 44)];
lab.backgroundColor = [UIColorbrownColor];
[self.windowaddSubview:lab];
[lab release];
1、文字属性
lab.text = @"首 页";
2、文字颜色
lab.textColor = [UIColorwhiteColor];
3、文字字体及大小
lab.font = [UIFontsystemFontOfSize:22];//系统字体
lab.font = [UIFontboldSystemFontOfSize:22];//粗体
4、文字对齐方式,默认左对齐
lab.textAlignment =NSTextAlignmentLeft;
lab.textAlignment =NSTextAlignmentRight;
lab.textAlignment =NSTextAlignmentCenter;
5、文字大小自适应宽度(文字的大小会根据label的宽度来变化),一般当我们的文字长度超出不多的时候,可控的时候,用这个凑活凑活
lab.adjustsFontSizeToFitWidth =YES;//会适应
6、文字换行
lab.numberOfLines =0;//0表示无限换行,但是行数还是要受这个label的高度和字体大小的影响
7、根据文字内容来确定label的高度
A、准备一个字典(一个关于字体的字典)
NSDictionary *dic =@{NSFontAttributeName:[UIFontsystemFontOfSize:16]};
//请记住现在你写的字体和字号,一会儿label的字体和字号要和这个一样
B、准备一个最大宽度和最大高度
CGSize maxSize =CGSizeMake(200,MAXFLOAT);//这次的label最宽200,高度无限一会儿算出高度来
C、准备label的文字字符串
NSString *labText =@"阿斯顿飞的时间按发髻上的看法是金";
D、根据准备好的文字、字体、size来计算这个字符串放在label中,需要多宽,多高的label,得到一个矩形
CGRect rect = [labTextboundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOriginattributes:dic context:nil];
注意:1、用一个字符串来调用方法,第一个参数是label的宽度和高度上限,第二个参数是换行条件,这里设置为根据每个字所占的矩形来算,第三个参数是字体参数,第四个是说明,一般写nil就行
2、返回值是一个CGRect矩形
E、得到这个矩形的宽和高,这个宽和高将给label的frame赋值
float width = rect.size.width;
float height = rect.size.height;
F、根据算好的宽和高来实例化一个label
UILabel *secondLab = [[UILabelalloc] initWithFrame:CGRectMake(0,64, width, height)];
G、把计算高度的字符串赋值给这个label的text属性
secondLab.text = labText;
H、设置label的字体和字号,记得和上面计算时候用的一样
secondLab.font = [UIFontsystemFontOfSize:16];
I、设置label的换行行数
secondLab.numberOfLines = 0;
J、设置label背景颜色
secondLab.backgroundColor = [UIColoryellowColor];
K、把label贴到父视图上
[self.windowaddSubview:secondLab];
L、别忘了内存管理
[secondLab release];
注意:ios6及其以前版本,UILabel的背景默认为白色,ios7之后的版本UILabel的背景默认为透明
//返回一个字符串所占位置的宽度
-(float)getWidthWithNSString:(NSString*)str
{
return [strboundingRectWithSize:CGSizeMake(220,18) options:NSStringDrawingUsesLineFragmentOriginattributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:14]}context:nil].size.width;
}
二、UI对图片的操作
UIImageView *img = [[UIImageViewalloc] initWithFrame:CGRectMake(0,20, 310, 465)];
1、把图片读进内存
UIImage *firstImage = [UIImageimageNamed:@"1.png"];//参数是图片名全名+扩展名
2、把内存中的图片赋值给imageView的image属性
img.image = firstImage;
[self.windowaddSubview:img];
[img release];
3、把图片读进内存的两个方法
UIImage *secondImage = [UIImageimageNamed:@"1_1.png"];//读进内存
UIImage *thirdImage = [UIImageimageWithContentsOfFile:[[NSBundlemainBundle] pathForResource:@"1_2"ofType:@"png"]];//现读文件
差别1:读进内存的方法,加载图片会快一些,因为直接从内存里取图片数据。现读文件的方法,加载图片会慢一些,因为是现从文件里读数据。
差别2:读进内存的方法,会占用内存,一个应用所能占用的内存不是无限的,所以不能什么都放到内存里。现读文件的方法,不占内存。
使用环境:如果是频繁使用的小图片,用读进内存的方法。如果是不常用的大图片,用现读文件的方法。举例:一个列表,每行都有一个下载按钮的图片,用读进内存的方法。一个应用,只在第一次使用的时候,有几页引导页面(全屏大小),现读文件
4确定一个imageView的大小,通过设置imageView的frame
4.1、先看一下图片大小,然后直接写frame
UIImageView *secondImageView = [[UIImageViewalloc] initWithFrame:CGRectMake(0,20, 360, 171)];
secondImageView.image = secondImage;
[self.windowaddSubview:secondImageView];
[secondImageView release];
4.2、根据读进来的图片的大小来确认image
UIImageView *thirdImageView = [[UIImageViewalloc] initWithFrame:CGRectMake(0,200, thirdImage.size.width, thirdImage.size.height)];
thirdImageView.image = thirdImage;
[self.windowaddSubview:thirdImageView];
[thirdImageView release];
5、UIImageView的动画
NSMutableArray *picArr = [NSMutableArrayarrayWithCapacity:0];
for(int i =1;i<13;i++)
{
UIImage *image = [UIImageimageNamed:[NSStringstringWithFormat:@"player%d.png",i]];
[picArr addObject:image];//数组里存的是UIImage对象,不是图片名字符串
}
//做一个UIImageView
UIImageView *animateImage = [[UIImageViewalloc] initWithFrame:CGRectMake(20,400, 60, 60)];
animateImage.animationImages = picArr;//把要动画的图片Image数组赋值
animateImage.animationDuration = 0.5;//动画持续时长,是一轮的时长
animateImage.animationRepeatCount = 0;//重复轮数
[self.windowaddSubview:animateImage];
[animateImage release];
//开始转
[animateImage startAnimating];
- UI控件笔记(二):UI之UILabel和UIImageView
- UI之UILabel和UIImageView
- UI基础2控件:UILabel,UIImageView
- UI-基本控件( UILabel UITextField UIImageView UIButton)
- UI 一一 常用控件-UILabel,UIImageView
- UI控件之UILabel
- UI基础学习之(二):UILabel
- UI -UIView和UILabel控件
- IOS开发UI系列之常用控件 UILabel, UITextField, UIButton, UIImageView等常用属性与方法
- iOS基础——UI控件之UIView、UITextField、UILabel、UIColor、UIButton、UIImageView
- UI控件笔记(五):UI之UIViewController和NSUserDefaults
- ui控件之UIImageView(imageView的动画效果)
- 05-常用UI控件之 UIImageView
- iOS UI基础控件之UIImageView
- UI控件 - UIImageView
- IOS UI控件 - UIImageView
- UI控件--UIImageView
- IOS入门学习笔记(普通UI控件--UILabel)
- easyui datagrid动态修改editor为combobox的options
- 代理协议委托一句话说明
- android编译命令分析
- Lucene学习总结之三:Lucene的索引文件格式(2)
- ioctl'was not declared in this scope
- UI控件笔记(二):UI之UILabel和UIImageView
- Django笔记 数据备份和恢复
- Mysql数据库5.7以上版本免安装版本配置
- UI控件笔记(三):UI之UIButton的属性
- Android Studio配置
- HTML5实现手机摇一摇功能
- linux系统中的时间及操作函数
- iOS获取通讯录联系人信息
- ios app上架AppStore注意事项及流程