iOS Button 設置AttributeString 在不一樣狀態下自適應尺寸心得

描述下場景:button在不一樣的狀態顯示不一樣的title樣式 ide

好比normal 下 font是[UIFont systemFontOfSize:18.0f weight:UIFontWeightRegular顏色是  [UIColor blackColor]ui

select 下 font 是[UIFont systemFontOfSize:18.0f weight:UIFontWeightMedium]顏色是  [UIColor grayColor]google

 

一開始這樣設置:spa

 

 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的尺寸3d

google了下 發現須要設置設置button的highLighted及 UIControlStateSelected | UIControlStateHighlighted狀態 參見:http://www.jianshu.com/p/57b2c41448bf http://rickytan.cn/blog/2015/07/06/uibutton-state/code

修改代碼以下:orm

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];
}
相關文章
相關標籤/搜索