【问题标题】:iOS change button text to bold when highlighted or selectediOS 在突出显示或选择时将按钮文本更改为粗体
【发布时间】:2014-06-24 09:47:17
【问题描述】:

我需要在突出显示或选择时将标题设置为粗体

这是我用来以编程方式生成按钮的代码:

    UIButton *pagerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [pagerButton setFrame:CGRectMake(offsetX, 10, 30, 30)];

    [pagerButton setTitle:pageNumber forState:UIControlStateNormal];
    [pagerButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];

我找不到任何属性来设置标题的字体或将其更改为粗体 forState:UIControlStateHighlighted

【问题讨论】:

标签: ios iphone objective-c ipad uibutton


【解决方案1】:

除了颜色和阴影之外,您还必须在自己的代码中处理这种情况。然而,加粗文本也可以使用属性文本,所以你可以使用setAttributedTitle:forState:。

【讨论】:

    【解决方案2】:

    当你点击按钮然后在你的按钮点击事件中调用下面的方法

    -(void)setButton:(UIButton *)button title:(NSString *)title fontSize:(CGFloat)fontSize
    {
        [button setTitle:title forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont fontWithName:@"Impact" fontSize]];
    }
    

    【讨论】:

    • 请不要使用大写的变量名。
    【解决方案3】:

    我不确定如何通知按钮的更改状态.. 我确定有办法..

    但是您可以通过这种方式将按钮文本设置为粗体 ----->

    button.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    

    它会将字体设置为粗体,您还可以设置大小...

    但是,如果您使用自定义字体,则需要获取该系列的粗体样式字体.... 希望对您有所帮助..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多