ios - 从 iOS 10.3 开始 : UIButton ignoring negative paragraph lineSpacing in NSAttributedString

标签 ios objective-c uibutton nsparagraphstyle ios10.3

我有一个 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/

相关文章:

objective-c - 如何在半个外面的 NSWindow 标题栏上显示图像

iphone - 通过像 iOS 5 中的短信应用程序一样拖动手指来隐藏键盘

ios - 获取单个 NSDateComponents 的 2 个日期之间的确切差异

iOS 按钮文本本地化 - 你必须做整个 xib 文件吗?

javascript - 为什么在 js 取消设置只读标签后,iOS 不显示键盘用于输入?

ios - UIButton 不在两个图像之间切换 Swift 4.2

uiviewcontroller - 在自定义单元格中点击 UIButton 后显示新的 ViewController

ios - 如果您在桌面上阅读过,Facebook Messenger 如何从锁定屏幕清除推送通知?

iPhone - UILabel 同时包含多种字体的文本

iOS - UIPageControl 上的 UIButton 不工作