首页 > 代码库 > iOS Button 设置AttributeString 在不同状态下自适应尺寸心得

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

 

 

 

 

<style></style><style></style><style></style><style></style><style></style>

iOS Button 设置AttributeString 在不同状态下自适应尺寸心得