我有一个 UIButton,它实际上只有 2 个单词,分为 2 行(稍后我在代码中将行值设置为“2”)。我一直在通过为段落 lineSpacing 设置负值来收紧默认 lineSpacing(如下图所示)。
从 iOS 10.3 开始,负值似乎被忽略了。虽然我可以用正值增加 lineSpacing,但我不能再收紧 2 行了。
还有其他人知道如何在 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];
我想也许你可以改变 paragraphSpacing,它有效~
关于ios - 从 iOS 10.3 开始 : UIButton ignoring negative paragraph lineSpacing in NSAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43237493/