ios基本控件之UIButton



UIButton属性

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;

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

在点击按钮是按钮是凹下去,然后弹起才触发起事件,按钮的状态有: 

1. UIControlEventTouchDown      // 按下    

2.    

3. UIControlEventTouchDownRepeat  多次按下   

4. UIControlEventTouchUpInside // 在按及其一定外围内

5.    

6. UIControlEventTouchUpOutside // 外面松  

 

4.adjustsImageWhenDisabled

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

5.adjustsImageWhenHighlighted

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

6.showsTouchWhenHighlighted

button.showsTouchWhenHighlighted=YES;点击时的闪光效果会被前景图片遮住中间部分;

6.contentEdgeInsets

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

 

7.按钮实例

1.有些候我UIButtontitle居左齐,我设置 
btn.textLabel.textAlignment = UITextAlignmentLeft

有作用的,我需要设置 

btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;

但是问题又出,此文字会紧贴边框,我可以设置 

btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);

使文字距离边框保持10个像素的距离。




8.使用遇到的问题:

当我们要给button的titleLabel加一些定制属性的时候,可以用到 NSMutableAttributedString 属性字符串

 NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"我已阅读并了解《电子交易风险揭示书》"]];
        NSRange attributeRange = {0,attributeStr.length};
        NSRange blueStrRange = {7,11};
        NSRange lineRange = {8,9};
        [attributeStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:lineRange];
        [attributeStr addAttribute:NSForegroundColorAttributeName value:PB_COLOR_1 range:blueStrRange];
        [attributeStr addAttribute:NSFontAttributeName value:PB_FONT_23 range:attributeRange];
        [_mRiskButton setAttributedTitle:attributeStr forState:UIControlStateNormal];

button的上的文字居中或者居左不能用button.titleLable.textAlignment = NSTextAlignmentLeft;是无效的

        _mRiskButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

        _mRiskButton.titleLabel.adjustsFontSizeToFitWidth = YES;


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值