更改UIButton的titleLabel,其标题类型为Storyboard / Xib文件中的Attributed

时间:2017-03-07 06:44:15

标签: nsattributedstring

问题: 我使用下面的代码来更改标题并在运行时设置UIButton的下划线。

[btnTemp setTitle:@"Test" forState:UIControlStateNormal];

我遇到了以下问题:

  1. 下划线未显示
  2. 标题不变
  3. 标题颜色显示为默认黑色。
  4. 为了克服这个问题,我在下面的代码中实现了:

    NSDictionary *attribs = @{NSForegroundColorAttributeName: _btnTemp.titleLabel.textColor,
    NSFontAttributeName: _btnTemp.titleLabel.font,
    NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)
                                  };
    NSAttributedString *strAttributed = [[NSAttributedString alloc] initWithString:@"Test" attributes:attribs];
    
    [btnTemp setAttributedTitle:strAttributed forState:UIControlStateNormal];
    

0 个答案:

没有答案