zoukankan      html  css  js  c++  java
  • iOS UIButton setTitle与setAttributedTitle

    今天遇到一个问题,查了好久,终于解决。

    我需要根据不同的条件给uibutton赋不同的值,由于字体要求有不同颜色变化,所以我选择了一个条件下用setTitle,另一个条件下用setAttributedTitle,但问题来了:当条件转换时,需要从setAttributedTitle切换到setTitle时,并没有达到我想要的切换。下面附上代码:

          NSString *unReceiptStr = @"";
            if (model.uNreceiptCoun==0) {
                unReceiptStr = @"全部已回执";
               
               [self.receiptBtn setTitle:unReceiptStr forState:UIControlStateNormal];/****1
              
            }else{
                unReceiptStr = [NSString stringWithFormat:@"%d人未回执",model.uNreceiptCoun];
                NSString *unCounStr = [NSString stringWithFormat:@"%d",model.uNreceiptCoun];
                NSMutableAttributedString *unReceiptString = [WYTools changStrColorWithTotalStr:unReceiptStr start:0 changeStr:unCounStr withColor:Color(255, 108, 83)];
                [self.receiptBtn setAttributedTitle:unReceiptString forState:UIControlStateNormal];/****2
            }

    当model.uNreceiptCoun!=0时正常显示,但当model.uNreceiptCoun==0,并不能显示全部已回执。

    我查阅了相关文档,加上自己的推测,当对同一个按钮同时使用setTitle与setAttributedTitle,按钮优先选择使用setAttributedTitle,所以导致在model.uNreceiptCoun==0处无法切换回。

    最后我的处理办法:将所有在setAttributedTitle后的setTitle,替换为setAttributedTitle,一切又恢复正常!

  • 相关阅读:
    Oracle通过表名获取所有列名类型(转)
    shell截取文件中的关键字
    VC6.0快捷键 与 Visual Assist X 快捷键(转载)
    mktime()函数使用
    OCI函数简单解析(转载)
    剑指offer16-合并两个排序的链表
    剑指offer17-树的子结构
    剑指offer30-连续子数组的最大和
    剑指offer-整数中1出现的次数
    剑指offer32-把数组排成最小的数
  • 原文地址:https://www.cnblogs.com/wangyingblock/p/6179347.html
Copyright © 2011-2022 走看看