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

标签 ios objective-c uibutton uilabel nsattributedstring

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

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/

相关文章:

iOS leftSpacingConstraint 相对于屏幕高度

ios - swift : How to update UILabel from another class X: UIButton

ios - 如何让高级 Controller 加载 View Controller

ios - UIActivityViewController 的自定义 header

iphone - 相机应用程序支持方向吗?

objective-c - UIImage :resizableImageWithCapInsets 创建的崩溃释放图像

ios - isKindOfClass 抓取标签

xcode - swift Xcode 6 : How do I use multiple functions in one statement?

ios - CNCopyCurrentNetworkInfo与iOS 13

ios - IOS 中的 viewDidLoad 与 ViewWillAppear