UIButton的常用属性
来源:互联网 发布:酒店英语口语软件下载 编辑:程序博客网 时间:2024/05/21 22:29
UIButton的集成关系:
UIControl : UIView : UIResponder : NSObject
1、创建UIButton
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeContactAdd]; btn1.frame = CGRectMake(20, 20, 20, 20); [self.view addSubview:btn1]; /* UIButtonTypeCustom = 0, UIButtonTypeSystem, UIButtonTypeDetailDisclosure, 见图2 UIButtonTypeInfoLight, UIButtonTypeInfoDark, UIButtonTypeContactAdd, 见图1 UIButtonTypeRoundedRect == UIButtonTypeSystem, */
图1
图2
2、配置UIButton的title
.titleLabel //UIButton中的label,注意字体大小要通过label控件来设置 - setTitle:forState: //设置不同状态下button的title- titleForState: //获取不同状态下button的title- setAttributedTitle:forState: //不同状态下的attributedTitle- attributedTitleForState:- setTitleColor:forState: //不同状态下title的颜色- titleColorForState:- setTitleShadowColor:forState: //不同状态下title的阴影,注意要设置label控件下的shadowOffset属性才会出现效果- titleShadowColorForState:.reversesTitleShadowWhenHighlighted//高亮状态下是否保留title的阴影,默认不保留实例:运行效果见图3 UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(80, 20, 50, 20)]; [btn2 setTitle:@"按钮1" forState:UIControlStateNormal]; [btn2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [btn2 setTitleShadowColor:[UIColor greenColor] forState:UIControlStateNormal]; [btn2.titleLabel setShadowOffset:CGSizeMake(2.0, 2.0)]; [btn2.titleLabel setFont:[UIFont systemFontOfSize:13]]; [self.view addSubview:btn2];
图3
3、配置UIButton的Image
//按钮按下的状态为“highLight”高亮状态- setBackgroundImage:forState: //不同状态下的背景图片- backgroundImageForState:- setImage:forState: //不同状态下的button图片- imageForState:.adjustsImageWhenHighlighted //按钮按下时 Image颜色是否变暗,默认YES.adjustsImageWhenDisabled //按钮不能点击时 Image颜色是否变亮,默认YES.showsTouchWhenHighlighted //.tintColor // 实例: UIButton *btn3 = [[UIButton alloc]initWithFrame:CGRectMake(20, 100, 100, 40)]; [btn3 setEnabled:YES]; //1 [btn3 setImage:[UIImage imageNamed:@"normal"] forState:UIControlStateNormal]; //2 //[btn3 setImage:[UIImage imageNamed:@"highLight"] forState:UIControlStateHighlighted]; //3 [btn3 setAdjustsImageWhenHighlighted:YES]; //4 //[btn3 setImage:[UIImage imageNamed:@"disable"] forState:UIControlStateDisabled]; //5 [btn3 setAdjustsImageWhenDisabled:YES]; //6 [self.view addSubview:btn3];运行效果:enable为YES 正常状态效果见图4 高亮状态(按下)效果见图5enable为NO disable效果见图6加上3、5行代码 enable为YES 正常状态效果见图normal 高亮状态(按下)效果见图highLightenable为NO disable效果见图disable
图“normal ”
图“ highLight ”
图“disable”
图4
图5
图6
4、配置UIButton的边缘
.contentEdgeInsets //内容的边缘,包括title和Image.titleEdgeInsets //title的边缘.imageEdgeInsets //Image的边缘注意:1)参数传UIEdgeInsets UIEdgeInsetsMake(top, left, bottom, right) 2)是距离button中心点来进行移动的,如top == -10 即向上移动10pt top == 10 即想下移动10pt实例: UIButton *btn4 = [[UIButton alloc]initWithFrame:CGRectMake(20, 150, 100, 40)]; [btn4 setBackgroundColor:[UIColor greenColor]]; [btn4 setEnabled:YES]; [btn4 setImage:[UIImage imageNamed:@"addSDjia"] forState:UIControlStateNormal]; [btn4 setAdjustsImageWhenHighlighted:YES]; [btn4 setTitle:@"添加" forState:UIControlStateNormal]; [btn4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn4.titleLabel setFont:[UIFont boldSystemFontOfSize:15]]; [self.view addSubview:btn4]; //运行见图7 [btn4 setImageEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)]; //运行见图8 [btn4 setTitleEdgeInsets:UIEdgeInsetsMake(0, 15, 0, 0)]; //运行见图9 [btn4 setContentEdgeInsets:UIEdgeInsetsMake(0, -30, 0, 0)]; //运行见图10
图7
图8
图9
图10
5、得到button当前状态
.buttonType //按钮的类型.currentTitle //当前title.currentAttributedTitle //当前属性title.currentTitleColor //当前title颜色.currentTitleShadowColor//当前title阴影颜色.currentImage //当前Image.currentBackgroundImage //当前背景图.imageView //只读的imageView
0 0
- UIButton的常用属性
- ios UIButton的常用属性
- UIButton的常用属性设置
- IOS--UIButton的常用属性和方法
- 设置UIButton的一些常用属性
- 常用UIButton控件的属性列表
- UIButton常用属性
- UIButton常用属性
- UIButton常用属性总结
- UIButton常用属性介绍lzp
- UIButton常用方法和属性
- 按钮UIBUtton的常用属性及方法总结
- iOS学习笔记-009.UIButton的常用属性
- 扩展UIButton的属性
- UIButton的一些属性
- UIButton的属性
- UIButton的adjustsImageWhenHighlighted属性
- UIButton 的一些属性
- spring源码剖析(三)自定义标签实现及使用
- 使用Matlab进行特征选择
- COM教程(0-序章)
- android:inputType参数类型说明
- C语言实现printf的部分功能
- UIButton的常用属性
- ALV的负数符号显示
- 不得不看的Java代码性能优化总结
- 读Android Builder设计模式后实战
- android开发的实用工具类
- hdu1429胜利大逃亡(续)【广搜】
- KNN算法
- 解决EL表达式不能在Js文件中使用
- 扒一扒传智播客深藏在背后的内幕