gpt4 book ai didi

ios - 我将如何创建一个包含两行文本且第一行文本大于第二行的 UIButton?

转载 作者:可可西里 更新时间:2023-11-01 05:35:57 26 4
gpt4 key购买 nike

基本上,我想要一个看起来像这样的按钮作为最终结果:

enter image description here

按钮横跨两行,最上面一行字体较大。在这张图片中,顶部是 38pt,底部是 24pt。

我知道这将是 NSAttributedString 的一项任务,但我无法弄清楚如何正确地完成它。我的 Storyboard 中有一个 UIButton,文本设置为属性(换行符设置为自动换行),然后是 viewDidLoad 中的导出,我执行以下操作:

UIFont *font = [UIFont systemFontOfSize:39.0];
UIFont *font2= [UIFont systemFontOfSize:17.0];

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"625 WPM"];
[string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];
[string addAttribute:NSFontAttributeName value:font2 range:NSMakeRange(string.length - 2, 2)];

self.button.titleLabel.attributedText = string;

基本上我给所有的字体都很大,然后对于最后几个字母我把它变小了。但它看起来很大胆。

我做错了什么?

最佳答案

UIButtontitleLabel 无法像独立的 UILabel 那样访问。您需要使用 UIButtonsetAttributedTitle:forState: 方法。

编辑:(添加回答行距问题)

在这个例子中,属性是NSParagraphStyleAttributeName,值是pStyle

NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
pStyle.lineSpacing = 8;

return @{
NSParagraphStyleAttributeName : pStyle
};

关于ios - 我将如何创建一个包含两行文本且第一行文本大于第二行的 UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19551856/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com