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];
    }

     

  • 相关阅读:
    百度mp3地址解密码
    VB 在EXE后附加信息
    截屏函数
    Base64和StrToByte
    The Android ION memory allocator, DMABUF is mentioned as well
    DDC EDID 介绍
    Memory management for graphic processors TTM的由来
    科普 写display driver的必看 How video card works [2D的四种主要操作]
    GEM vs TTM
    DMABUF 背景介绍文章 Sharing buffers between devices
  • 原文地址:https://www.cnblogs.com/programmer-blog/p/6504678.html
Copyright © 2011-2022 走看看