iOS Button 设置AttributeString 在不同状态下自适应尺寸心得
Posted _嵌入式开发_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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]; }
!-- p.p1>!-- p.p1>!-- p.p1>!-- p.p1>!-- p.p1>
以上是关于iOS Button 设置AttributeString 在不同状态下自适应尺寸心得的主要内容,如果未能解决你的问题,请参考以下文章
ios 添加到cell 上的button点击无效!扩大button的点击区域(黑魔法)
ios上的 button和input-button为什么不水平居中的