01-UI基础-05-01-UIButton : UIControl

##继承关系 UIButton

##一、UIButton属性 ###1.1、UIButton状态

enum {
    UIControlStateNormal = 0, // 常规状态显现 
    UIControlStateHighlighted = 1 << 0, // 高亮状态显现 
    UIControlStateDisabled = 1 << 1, // 禁用的状态才会显现
    UIControlStateSelected = 1 << 2, // 选中状态 
    UIControlStateApplication = 0x00FF0000, // 当应用程序标志时 
    UIControlStateReserved = 0xFF000000 //为内部框架预留,可以不管他 
};

###1.2、Uibutton类型

typedef enum {
    UIButtonTypeCustom = 0, // 自定义风格
    UIButtonTypeRoundedRect, // 圆角矩形 
    UIButtonTypeDetailDisclosure, // 蓝色小箭头按钮,主要做详细说明用
    UIButtonTypeInfoLight, // 亮色感叹号
    UIButtonTypeInfoDark, // 暗色感叹号
    UIButtonTypeContactAdd // 十字加号按钮
} UIButtonType;

###1.3、文字、图标等

// 给按钮设置文字时,苹果文档说明,不能使用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; 

###1.4、adjustsImageWhenDisabled

当按钮禁用的情况下,图像的颜色会被画深一点,默认为YES。

###1.5、adjustsImageWhenHighlighted

当按钮高亮的情况下,图像的颜色会被画深一点,默认为YES。

###1.6、showsTouchWhenHighlighted

button.showsTouchWhenHighlighted=YES;点击时的闪光效果会被前景图片遮住中间部分;也就是设置按钮按下会发光

###1.7、contentEdgeInsets

设置按钮的内部内容(包含按钮图片和标题)离按钮边缘上下左右的距离。

UIEdgeInsets insets; // 设置按钮内部图片间距
insets.top = insets.bottom = insets.right = insets.left = 10;
bt.contentEdgeInsets = insets;
bt.titleEdgeInsets = insets; // 标题间距

###1.8、文字显示位置、字体的大小、字体颜色

  • 显示位置
btn.frame = CGRectMake(x, y, width, height);
[btn setTitle: @"search" forState: UIControlStateNormal];

设置按钮上的自体的大小

[btn setFont: [UIFont systemFontSize: 14.0]];    //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法

应该使用

btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
[btn seBackgroundColor: [UIColor blueColor]];

最后将按钮加入到指定视图superView

[superView addSubview: btn];
  • 字体大小、颜色

初始化button

tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)];

这样初始化的button,文字默认颜色是白色的,所有如果背景也是白色的话,是看不到文字

btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;//设置文字位置,现设为居左,默认的是居中
[btn setTitle:@“title”forState:UIControlStateNormal];// 添加文字
  • 对齐方式

有些时候我们想让UIButton的title居左对齐,我们设置

btn.textLabel.textAlignment = UITextAlignmentLeft;// 是没有作用的

正确的设置

btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;

但是此时文字会紧贴到左边框,我们可以设置

btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
// 使文字距离做边框保持10个像素的距离。

设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用

[btn.titleLabel setTextColor:[UIColorblackColor]];
btn.titleLabel.textColor=[UIColor redColor];

正确地做法

[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];

##二、事件 ###2.1、添加事件

这些事件都是基于触摸、基于值、基于编辑。有如下事件会触发。

// 原方法声明
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; 
/**controlEvents事件类型:
1. UIControlEventTouchDown      // 按下    
2. UIControlEventTouchDownRepeat  多次按下   
3. UIControlEventTouchUpInside // 在按钮及其一定外围内松开
4. UIControlEventTouchUpOutside // 按钮外面松开 
*/
// 调用样例
[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];

###2.2、移除事件

// 原方法声明
- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; 
// controlEvents 参数同上
// 调用样例
[btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];

##三、官方文档 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/index.html#//apple_ref/occ/cl/UIButton

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值