UIButton属性
2015-09-06 20:55
393 查看
1.UIButton状态:
UIControlStateNormal // 正常状态
UIControlStateHighlighted // 高亮状态
UIControlStateDisabled // 禁用状态
UIControlStateSelected // 选中状态
UIControlStateApplication //
UIControlStateReserved // 保留状态
2.Uibutton类型:
UIButtonTypeCustom //自定义类型
添加图片: 灰色背景颜色:
UIButtonTypeRoundedRect //圆角类型
UIButtonTypeDetailDisclosure //细节展示按钮
UIButtonTypeInfoLight //浅色背景的信息按钮
UIButtonTypeInfoDark //暗色背景的信息按钮
UIButtonTypeContactAdd // 添加按钮
3.UIButton常用属性
给按钮设置文字时,苹果文档说明,不能使用label对象设置文字的颜色或者阴影颜色,相反必须使用setTitleColor:forState: and setTitleShadowColor:forState:这两个方法才能修改这些值。
设置按钮中其他属性依次类推。。。。
//设置对应状态的标题内容default is nil. title is assumed to be single line
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
//设置对应状态的标题颜色
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
//设置对应状态的标题阴影颜色
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;
//设置对应状态的按钮的图片
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
//设置对应状态的按钮背景图片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
添加事件
- (void)addTarget:(id)target
action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
这些事件都是基于触摸、基于值、基于编辑。有如下事件会触发。
在点击按钮是按钮是凹下去,然后弹起才触发起事件,按钮的状态有:
UIControlEventTouchDown // 按下
UIControlEventTouchDownRepeat 多次按下
UIControlEventTouchUpInside // 在按钮及其一定外围内松开
UIControlEventTouchUpOutside // 按钮外面松开
4.adjustsImageWhenDisabled
当按钮禁用的情况下,图像的颜色会被画深一点,默认为YES。
5.adjustsImageWhenHighlighted
当按钮高亮的情况下,图像的颜色会被画深一点,默认为YES。
6.showsTouchWhenHighlighted
button.showsTouchWhenHighlighted=YES;点击时的闪光效果会被前景图片遮住中间部分;
6.contentEdgeInsets
设置按钮的内部内容(包含按钮图片和标题)离按钮边缘上下左右的距离。
7.按钮实例
1.有些时候我们想让UIButton的title居左对齐,我们设置
btn.textLabel.textAlignment = UITextAlignmentLeft
是没有作用的,我们需要设置
btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;
但是问题又出来,此时文字会紧贴到左边框,我们可以设置
btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
使文字距离左边框保持10个像素的距离。
UIControlStateNormal // 正常状态
UIControlStateHighlighted // 高亮状态
UIControlStateDisabled // 禁用状态
UIControlStateSelected // 选中状态
UIControlStateApplication //
UIControlStateReserved // 保留状态
2.Uibutton类型:
UIButtonTypeCustom //自定义类型
添加图片: 灰色背景颜色:
UIButtonTypeRoundedRect //圆角类型
UIButtonTypeDetailDisclosure //细节展示按钮
UIButtonTypeInfoLight //浅色背景的信息按钮
UIButtonTypeInfoDark //暗色背景的信息按钮
UIButtonTypeContactAdd // 添加按钮
3.UIButton常用属性
给按钮设置文字时,苹果文档说明,不能使用label对象设置文字的颜色或者阴影颜色,相反必须使用setTitleColor:forState: and setTitleShadowColor:forState:这两个方法才能修改这些值。
设置按钮中其他属性依次类推。。。。
//设置对应状态的标题内容default is nil. title is assumed to be single line
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
//设置对应状态的标题颜色
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
//设置对应状态的标题阴影颜色
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;
//设置对应状态的按钮的图片
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
//设置对应状态的按钮背景图片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
添加事件
- (void)addTarget:(id)target
action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
这些事件都是基于触摸、基于值、基于编辑。有如下事件会触发。
在点击按钮是按钮是凹下去,然后弹起才触发起事件,按钮的状态有:
UIControlEventTouchDown // 按下
UIControlEventTouchDownRepeat 多次按下
UIControlEventTouchUpInside // 在按钮及其一定外围内松开
UIControlEventTouchUpOutside // 按钮外面松开
4.adjustsImageWhenDisabled
当按钮禁用的情况下,图像的颜色会被画深一点,默认为YES。
5.adjustsImageWhenHighlighted
当按钮高亮的情况下,图像的颜色会被画深一点,默认为YES。
6.showsTouchWhenHighlighted
button.showsTouchWhenHighlighted=YES;点击时的闪光效果会被前景图片遮住中间部分;
6.contentEdgeInsets
设置按钮的内部内容(包含按钮图片和标题)离按钮边缘上下左右的距离。
7.按钮实例
1.有些时候我们想让UIButton的title居左对齐,我们设置
btn.textLabel.textAlignment = UITextAlignmentLeft
是没有作用的,我们需要设置
btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;
但是问题又出来,此时文字会紧贴到左边框,我们可以设置
btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
使文字距离左边框保持10个像素的距离。
相关文章推荐
- UIAlertView
- UIActivityIndicatorView
- zoj 2091 Mean of Subsequence(奇怪的贪心)
- 黑马程序员之GUI
- iOS——UISlider的使用方法详细
- Ubuntu安装Torque教程
- 关于 UITableViewCell 的重用,需注意:
- [HDOJ5288]OO's Sequence
- Thread + Handle 解决子线程更新UI
- UI课程04 事件处理
- UI课程01 UIView及其子类
- StringBuilder与StringBuffer的区别(转)
- 数据库连接池Druid
- LightOJ 1427 Substring Frequency (II)(AC自动机裸)
- UI课程02 UILabel,UITextField,UIButton
- iOS——UISegmentedControl 的用法
- UML建模之时序图(Sequence Diagram)
- 一起了解一下 UITextView 的使用
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 菜鸟启航:UILabel基本操作