繁体   English   中英

从iOS 10.3开始:UIButton忽略NSAttributedString中的负段lineSpacing

[英]As of iOS 10.3: UIButton ignoring negative paragraph lineSpacing in NSAttributedString

我有一个UIButton,它实际上只是2个单词,包装成2行(稍后我在代码中将lines的值设置为“ 2”)。 我一直在通过为段lineSpacing输入一个负值来加强默认的lineSpacing(简化并显示如下)。

从iOS 10.3开始,看来负值已被忽略。 虽然我可以将lineSpacing增加为正值,但我无法再收紧这两行。

有谁知道如何在UIButton中加强它吗? (我准备改变控制方式,但是以为我会发布问题)。

非常感谢大家。

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = -15.0f;
paragraphStyle.alignment = NSTextAlignmentLeft;

NSDictionary * attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                             [UIColor whiteColor], NSStrokeColorAttributeName,
                             [UIColor greenColor], NSForegroundColorAttributeName,
                             @(-2.0), NSStrokeWidthAttributeName,
                             paragraphStyle, NSParagraphStyleAttributeName,
                             nil];
NSMutableAttributedString *buttonTitle = [[NSMutableAttributedString alloc] initWithString:str attributes:attributes];

self.theButton.titleLabel.numberOfLines = 2;
[self.theButton setAttributedTitle:buttonTitle forState:UIControlStateNormal];

编辑:看起来这也是UILabel的问题。

我可以使用iOS 10.2中的代码更改行空间

NSMutableParagraphStyle *paraStyle = [NSMutableParagraphStyle defaultParagraphStyle].mutableCopy;
paraStyle.alignment = NSTextAlignmentCenter;
paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
paraStyle.paragraphSpacing = -8;

[self setAttributedTitle:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"test button\n normal", titleString] attributes:@{NSParagraphStyleAttributeName:paraStyle}] forState:UIControlStateNormal];
[self setAttributedTitle:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"test button\n selected", titleString] attributes:@{NSParagraphStyleAttributeName:paraStyle}] forState:UIControlStateSelected];

我想也许您可以更改段落间距,它可以工作〜

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM