UIButton

2016-05-21  本文已影响273人  Barry_小闪

深度定制 UIButton 按钮

补充

[button sizeToFit ] 
[button setContentHorizontalAlignment: UIControlContentHorizontalAlignmentLeft];
  button.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
button3.enabled = NO;
[button3 setTitle:@"点不了" forState:UIControlStateDisabled];
[self.clearRecentHistoryButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 15, 0, 0)];
  [self.clearRecentHistoryButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
 button.adjustsImageWhenHighlighted = NO;
在XIB中的话,取消勾选的 "highlight adjusts image"

目录

给按钮添加事件(核心方法)

[button addTarget:self action:@selector(onclicked:) forControlEvents:UIControlEventTouchDown];

当指定的事件发生的时候,响应消息的对象会去调用指定的消息

UIControlEventTouchUpInside   按下松开
UIControlEventTouchDown  按下   
UIControlEventTouchUpInside:当手指按下按钮并且松开的一瞬间,self去调用onclicked:方法
UIControlEventTouchDown:当手指按下按钮的瞬间,self去调用on clicked:方法

系统按钮

创建按钮
UIButton * button2 = [UIButton buttonWithType:UIButtonTypeContactAdd];
UIButtonTypeDetailDisclosure,  系统详情按钮
UIButtonTypeInfoLight,  系统详情按钮
UIButtonTypeInfoDark,  系统详情按钮
UIButtonTypeContactAdd,  系统添加按钮
UIButtonTypeRoundedRect = UIButtonTypeSystem

文字按钮

[button3 setTitle:@"来点我啊" forState:UIControlStateNormal];
UIControlStateNormal    正常状态 ,如果只设置了正常状态下的文字,那么其他的状态下文字和正常状态下文字一样
UIControlStateHighlighted  高亮状态
UIControlStateDisabled   不可点击状态
UIControlStateSelected   选中状态
[button3 setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[button3 setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
  button3.titleLabel.font = [UIFont systemFontOfSize:25];

图片按钮

[button setBackgroundImage:[UIImage imageNamed:@"map.png"] forState:UIControlStateNormal];  
[button4 setImage:[UIImage imageNamed:@"map.png"] forState:UIControlStateNormal];
[button4 setImage:[UIImage imageNamed:@"button_up.png"] forState:UIControlStateHighlighted];
[button4 setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];

图片和文字同时存在

[button5 setTitle:@"hello" forState:UIControlStateNormal];
[button5 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button5 setImage:[UIImage imageNamed:@"player_down_1"] forState:UIControlStateNormal];

定制按钮(图片和文字的位置)

创建继承UIButton的类
@interface CustomButton : UIButton

//imageViewH:labelH = 4 : 1

//重新计算按钮中label的位置信息
//contentRect:当前按钮的frame(真正有用的是size)
//返回值:label新的frame
- (CGRect)titleRectForContentRect:(CGRect)contentRect{
    
    CGFloat W = contentRect.size.width;
    CGFloat X = 0;
    CGFloat H = contentRect.size.height/5.0f;
    CGFloat Y = contentRect.size.height/5.0f * 4;

    return CGRectMake(X, Y, W, H);
    
}

//重新计算imageView的位置信息
//contentRect:当前按钮的frame(真正有用的是size)
//返回值:imageView新的frame
- (CGRect)imageRectForContentRect:(CGRect)contentRect{

    CGFloat W = contentRect.size.width;
    CGFloat X = 0;
    CGFloat H = contentRect.size.height/5.0f * 4;
    CGFloat Y = 0;
    
    return CGRectMake(X, Y, W, H);
    
}

获取按钮不同状态下的文字或者图片

//获取某个状态下的文字或者图片
button titleForState:<#(UIControlState)#>
button imageForState:<#(UIControlState)#>

//当前状态下的文字或者图片
[button currentTitle];
[button currentImage];

上一篇 下一篇

猜你喜欢

热点阅读