UIButton中设置Titl方法包括以下几种:
- (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的值,但是运行出来确没显示文字。
- 正常使用UIButton的时候设置Title是要对应Button的ControlState,因为UIButton继承于UIControl,在设置值得时候需要对象状态,所以一般都会用:
setTitle:(NSString *)title forState:(UIControlState)state 设置 Title;
- setAttributedTitle是iOS6之后的方法,使用起来很简单,没特色说明。如下:
[button setAttributedTitle:[[NSAttributedString alloc]initWithString:@"3333333"] forState:UIControlStateNormal];
-
对应的currentTitle 也就是/normal/highlighted/selected/disabled状态下的title值,属性为readOnly;
-
至于titleLabel是设置的时候为啥不显示。查了官方文档以后才发现,真正的原因再于:(使用UIButton打印titleLabel对象的结果)
<UIButtonLabel: 0x8a75810b; 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, 0, 100, 30)];
或者
UIButton *uibtn = [UIButton buttonWithType:UIButtonTypeCustom];
[uibtn setFrame:CGRectMake(0, 0, 100, 30)];
文章参考:
http://www.cnblogs.com/Yukang1989/p/3716753.html
网友评论