UIButton中
2015-12-13 15:49
363 查看
IButton中设置Titl方法包括以下几种:
在定义UIButton的时候,经常会使用titleLabel.text设置UIButton的值,但是Run出来确啥都没显示,不起作用啊!!!,这是怎么会事?难道是API的bug??
1.其实不是,正常使用UIButton的时候设置Title是要对应Button的ControlState,因为UIButton继承于UIControl,在设置值得时候需要对象状态,所以一般都会用
setTitle:(NSString *)title forState:(UIControlState)state 设置 Title;
2.setAttributedTitle是iOS6之后的方法,使用起来很简单,没特色说明。实例如下:
3.对应的currentTitle 也就是/normal/highlighted/selected/disabled状态下的title值,属性为readOnly;
4.至于titleLabel是设置的时候为啥不显示,比较神奇。查了官方文档以后才发现,真正的原因再于:(以下是我使用UIButton打印titleLabel对象的结果)
看到这里你明白了吗?
默认UIButton的titleLable是没设置frame的,而且hidden=YES;只要你设置这2个值就可以正常显示,
无论你采用何种方式生产UIButton:
- (void)setTitle:(NSString *)title forState:(UIControlState)state; - (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state @property(nonatomic,readonly,retain) NSString *currentTitle; @property(nonatomic,readonly,retain) UILabel *titleLabel;
在定义UIButton的时候,经常会使用titleLabel.text设置UIButton的值,但是Run出来确啥都没显示,不起作用啊!!!,这是怎么会事?难道是API的bug??
1.其实不是,正常使用UIButton的时候设置Title是要对应Button的ControlState,因为UIButton继承于UIControl,在设置值得时候需要对象状态,所以一般都会用
setTitle:(NSString *)title forState:(UIControlState)state 设置 Title;
2.setAttributedTitle是iOS6之后的方法,使用起来很简单,没特色说明。实例如下:
[uibutton setAttributedTitle:[[NSAttributedString alloc]initWithString:@"3333333"] forState:UIControlStateNormal];
3.对应的currentTitle 也就是/normal/highlighted/selected/disabled状态下的title值,属性为readOnly;
4.至于titleLabel是设置的时候为啥不显示,比较神奇。查了官方文档以后才发现,真正的原因再于:(以下是我使用UIButton打印titleLabel对象的结果)
po uibutton.titleLabel <UIButtonLabel: 0x7575800; frame = (0 0; 0 0); text = '11111111'; clipsToBounds = YES; hidden = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7572980>>
看到这里你明白了吗?
默认UIButton的titleLable是没设置frame的,而且hidden=YES;只要你设置这2个值就可以正常显示,
无论你采用何种方式生产UIButton:
UIButton *uibtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 100, 100, 30)]; UIButton *uibtn = [UIButton buttonWithType:UIButtonTypeCustom]; [uibtn setFrame:CGRectMake(0, 100, 100, 30)];
相关文章推荐
- iOS UI 02 UIimageview 和模态切换
- iOS UI 02 UIimageview 和模态切换
- go build命令
- iOS UI 01 课堂笔记 -设计模式
- iOS UI 01 课堂笔记 -设计模式
- IOS UI 01 课堂笔记 -label
- IOS UI 01 课堂笔记 -label
- 使用yuicompressor-maven-plugin插件压缩JS和CSS静态资源
- UILabel: 如何可以更改除底部外的所有边框?
- Android 异步更新UI----handler+thread
- WebKit之CSSValue的继承关系
- JSP利用过滤器解决request中文乱码问题
- JavaSe String,StringBuffer,StringBuilder 简要区别(随记一)
- iOS环信3.0集成 (二)UI文件集成
- iOS多线程开发——NSOperation/NSOperationQueue浅析
- 使用Fluent API进行属性映射【Code-First系列】
- leetcode -- Permutation Sequence -- 重点
- APUE学习记录——进程环境
- Company BDui 面试 笔试 : 从10个已按从大到小排列好的数组中,求top 15个最大的数
- 62 You have enabled backup optimization in RMAN. You issue the following RMAN command to configure a