【问题标题】:UIButton with Text Stroke/Outline带有文本笔画/轮廓的 UIButton
【发布时间】:2024-01-16 17:54:02
【问题描述】:

我想绘制一个UIButton,其文本使用轮廓,即笔划。因此,我尝试使用 titleLabel 制作自己的自定义标签并分配给 UIButton.titleLabel ,但这是行不通的。有人提到我可以在UIButton 之上添加UILabel,但我不确定这个解决方案是否有效。

有人可以推荐一个稳定的方法来解决这个问题吗?谢谢。

【问题讨论】:

    标签: iphone ios objective-c xcode uibutton


    【解决方案1】:

    你可以使用

    - (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state
    

    UIButton的方法。

    例如。像这样的属性字符串

    NSAttributedString *attributedText =
    [[NSAttributedString alloc] initWithString:title
                                           attributes:@{NSStrokeWidthAttributeName: [NSNumber numberWithInt:-6],
                           NSStrokeColorAttributeName: titleBorderColor,
                       NSForegroundColorAttributeName: [UIColor whiteColor]}];
    

    【讨论】:

    • 是的,我忘了说:)
    • 太糟糕了,不过它看起来是一个更好的解决方案。我现在似乎与其他人一起失败了。
    【解决方案2】:

    您还可以将视图作为子视图添加到您的按钮。 在此视图中,您可以放置​​标签,或者,如果需要,可以放置更多标签,例如一个带有两个不同格式文本行的按钮。

    【讨论】:

    • 这可能不是最好的解决方案,但我想要 iOS 5 支持,因此无法使用 NSAttributedString 的答案,而且我在子类化 UIButton 时也遇到了麻烦。 UILable 我可以很好地继承它,它对我有用。
    最近更新 更多