简体   繁体   English

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

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

I have a UIButton which is really just 2 words, wrapped to 2 lines (I set the lines value to "2" in code later). 我有一个UIButton,它实际上只是2个单词,包装成2行(稍后我在代码中将lines的值设置为“ 2”)。 I have been tightening up the default lineSpacing by putting in a negative value for the paragraph lineSpacing (as simplified & shown below). 我一直在通过为段lineSpacing输入一个负值来加强默认的lineSpacing(简化并显示如下)。

As of iOS 10.3, it appears the negative value is now being ignored. 从iOS 10.3开始,看来负值已被忽略。 While I can increase the lineSpacing with a positive value, I can not tighten up the 2 lines anymore. 虽然我可以将lineSpacing增加为正值,但我无法再收紧这两行。

Does anyone else know how to tighten this up in a UIButton? 有谁知道如何在UIButton中加强它吗? (I'm about ready to change that control up... but thought I'd post the question). (我准备改变控制方式,但是以为我会发布问题)。

Much thanks all. 非常感谢大家。

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];

edit: Looks like this is also an issue with UILabel also. 编辑:看起来这也是UILabel的问题。

i can change the line space using the code in iOS 10.2 我可以使用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];

i guess maybe you can change the paragraphSpacing, it works~ 我想也许您可以更改段落间距,它可以工作〜

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

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