iOS UIButton setTitle与setAttributedTitle
来源:互联网 发布:找一个程序员女朋友 编辑:程序博客网 时间:2024/05/21 21:29
今天遇到个问题,在此记录下来,也记录下现在的解决办法:
我需要在同一个UIButton上根据条件先后设置不带属性的title,即用setTitle来设置标题,后来条件变化需要显示带属性的标题(例如下划线,或者字体多种颜色等等)使用setAttributedTitle,后来条件再次改变需要显示不带属性的标题,在此我重新使用setTitle来重新设置。
[stateBtn setTitle:@"456" forState:UIControlStateNormal];//-------1 NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:@"123"];[attriString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4, 2)]; [stateBtn setAttributedTitle:attriString forState:UIControlStateNormal];//---------2 attriString = nil; [stateBtn setTitle:@"789" forState:UIControlStateNormal];//----------3在标记1处,按钮成功显示“456”;标记2处,按钮成功显示“123”,但是标记3时,按钮并不能切换到“789”;
这是为什么呢?
我查阅了相关文档,加上自己的推测,当对同一个按钮同时使用setTitle与setAttributedTitle,按钮优先选择使用setAttributedTitle,所以导致在3处无法切换回。
最后我的处理办法:将所有在setAttributedTitle后的setTitle,替换为setAttributedTitle,一切又恢复正常!
0 0
- iOS UIButton setTitle与setAttributedTitle
- UIButton setTitle
- iOS 中UIButton的 settitle 和 titlelabel的使用情况
- iOS修改UIButton setTitle字体颜色和调整字体位置
- iOS 中UIButton的 settitle 和 titlelabel的使用误区
- iOS 中UIButton的 settitle 和 titlelabel的使用误区
- UIButton设置内容setTitle
- UIButton的setTitle,system,custom
- objective-c中UIButton setTitle不起作用
- UIButton.titleLabel.text和[UIButton setTitle:],UIButton.imageView.image和[UIButton setImage]的区别
- iOS之UI随笔-UIButton与UIImageView
- iOS initWithFrame:frame] 与 [UIButton buttonWithType] 对比
- iOS UIButton的setBackgroundImage与setImage区别
- iOS UIButton的基本设置与切圆角
- 【IOS】UIButton
- iOS-UIButton
- ios UIButton
- IOS UIButton
- 计算机状态通知
- 使用PHP获取汉字的拼音(全部与首字母)
- 背包问题九讲 v1.0
- 数据库第五章作业
- Android-SDK 升级下载技巧
- iOS UIButton setTitle与setAttributedTitle
- Android圆形图片自定义控件
- MySQL56打印sql语句
- Tomcat 设置内存大小
- JavaScript中typeof
- MyBatis+Spring 事务管理
- android Activity之间递 bundle对象传递
- HIVE使用小计
- 《剑指Offer》学习笔记--面试题16:反转链表