zoukankan      html  css  js  c++  java
  • iOS Button 设置AttributeString 在不同状态下自适应尺寸心得

    描述下场景: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];
    }

     

  • 相关阅读:
    AFNetwork 作用和用法详解
    ios 常见错误记录
    UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
    AutoLayout
    矩阵的法式
    极小多项式
    对角化
    线性映射
    线性方程组的解
    特征值和特征向量
  • 原文地址:https://www.cnblogs.com/programmer-blog/p/6504678.html
Copyright © 2011-2022 走看看