码迷,mamicode.com
首页 > 移动开发 > 详细

iOS Button 设置AttributeString 在不同状态下自适应尺寸心得

时间:2017-03-05 11:36:18      阅读:599      评论:0      收藏:0      [点我收藏+]

标签:action   button   name   cte   str   hup   show   不同的   点击   

描述下场景:button在不同的状态显示不同的title样式

比如normal 下 font是[UIFont systemFontOfSize:18.0f weight:UIFontWeightRegular颜色是  [UIColor blackColor]

select 下 font 是[UIFont systemFontOfSize:18.0f weight:UIFontWeightMedium]颜色是  [UIColor grayColor]

 

一开始这样设置:

 

 NSAttributedString *normal_title = [[NSAttributedString alloc] initWithString:@"点评 20320323" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18.0f weight:UIFontWeightRegular],NSForegroundColorAttributeName:unselect_color}];
    NSAttributedString *select_title = [[NSAttributedString alloc] initWithString:@"点评 20320323" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18.0f weight:UIFontWeightMedium],NSForegroundColorAttributeName:select_color}];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.backgroundColor = [UIColor redColor];
    button1.frame = CGRectMake(10.0, 400.0, 100.0, 20.0f);
    [button1 setAttributedTitle:normal_title forState:UIControlStateNormal];
    [button1 setAttributedTitle:select_title forState:UIControlStateSelected];
    [button1 sizeToFit];
    [button1 addTarget:self action:@selector(showSelect:) forControlEvents:UIControlEventTouchUpInside];


- (IBAction)showSelect:(id)sender{
    if ([sender isKindOfClass:[UIButton class]]) {
        UIButton *button = (UIButton *)sender;
        button.selected = !button.selected;
        [button sizeToFit];
}

设置后发现 点击后无法改变button的尺寸

google了下 发现需要设置设置button的highLighted及 UIControlStateSelected | UIControlStateHighlighted状态 参见:http://www.jianshu.com/p/57b2c41448bf http://rickytan.cn/blog/2015/07/06/uibutton-state/

修改代码如下:

UIColor *select_color = [UIColor blackColor];
    UIColor *unselect_color = [UIColor grayColor];
    NSAttributedString *normal_title = [[NSAttributedString alloc] initWithString:@"点评 20320323" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18.0f weight:UIFontWeightRegular],NSForegroundColorAttributeName:unselect_color}];
    NSAttributedString *select_title = [[NSAttributedString alloc] initWithString:@"点评 20320323" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18.0f weight:UIFontWeightMedium],NSForegroundColorAttributeName:select_color}];
    
   
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.backgroundColor = [UIColor redColor];
    button1.frame = CGRectMake(10.0, 400.0, 100.0, 20.0f);
    [button1 setAttributedTitle:normal_title forState:UIControlStateNormal];
    [button1 setAttributedTitle:select_title forState:UIControlStateSelected];
    
    [button1 setAttributedTitle:normal_title forState: UIControlStateHighlighted];
    [button1 setAttributedTitle:select_title forState:UIControlStateSelected | UIControlStateHighlighted];
    [button1 sizeToFit];
    [button1 addTarget:self action:@selector(showSelect:) forControlEvents:UIControlEventTouchUpInside];


- (IBAction)showSelect:(id)sender{
    if ([sender isKindOfClass:[UIButton class]]) {
        UIButton *button = (UIButton *)sender;
        button.selected = !button.selected;
        [button sizeToFit];
}

 

 

 

 

iOS Button 设置AttributeString 在不同状态下自适应尺寸心得

标签:action   button   name   cte   str   hup   show   不同的   点击   

原文地址:http://www.cnblogs.com/programmer-blog/p/6504678.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!