在UITextView上设置NSAttributedString的字体会忽略行间距。

76
我正试图在iOS 6中将一个属性字符串设置给UITextView。问题是,如果我尝试在属性字符串上设置字体属性,行间距会被忽略。但是,如果我不设置字体,而使用默认字体,则行间距正常工作。
NSString *string = @" Hello \n world";
attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];

paragraphStyle.minimumLineHeight = 50;
// setting the font below makes line spacing become ignored
[attrString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(0, string.length)];
[attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];

mainTextView.attributedText = attrString;

有什么想法是怎么回事吗?


3
你打算接受某个答案吗,或者说没有任何答案对你有所帮助? :) - Jan Nash
是的,我认为是这样的。请看下面我的回答 :) - Jan Nash
嘿,如果有答案对您有帮助,那么接受一个答案仍然是很酷的。 - Jan Nash
5个回答

111

属性字符串编程指南:

UIFont *font = [UIFont fontWithName:@"Palatino-Roman" size:14.0];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font
                                forKey:NSFontAttributeName];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"strigil" attributes:attrsDictionary];
更新:我尝试在自己的应用中使用addAttribute:方法,但在iOS 6模拟器上似乎不起作用:

NSLog(@"%@", textView.attributedText);

日志显示已正确添加属性,但在iOS模拟器上显示的视图没有属性。

6
当我尝试添加属性时,我遇到了同样的问题。我尝试了你的答案,甚至还是不行。有什么建议吗?如果我们在讨论IOS的话,我认为你应该将答案编辑为UIFont而不是NSFont。 - iosMentalist
6
我不知道为什么人们正在投赞成票。它并没有解决实际问题。 - Snowman
2
这个答案略有点旧,所以请使用:UIFont * font = [UIFont fontWithName: @"Palatino-Roman" size:14.0] 来修复。 - sam_smith
当然还是不行。似乎添加甚至设置属性都对属性字符串没有影响。 - Micrified
对不起,但这仍然不是一个答案。 - Jan Nash

26

我发现你的问题,因为我也在与 NSAttributedString 打交道。对我而言,像更改属性字符串中所述那样,beginEditingendEditing 方法很管用。除此之外,行间距是通过在段落样式上使用 setLineSpacing 来设置的。

因此,你可能想尝试将你的代码更改为:

NSString *string = @" Hello \n world";
attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];

[paragraphStyle setLineSpacing:20]  // Or whatever (positive) value you like...    
[attrSting beginEditing];

[attrString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(0, string.length)];
[attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];

[attrString endEditing];

mainTextView.attributedText = attrString;

顺便说一句,我没测试过这段代码,但我的看起来几乎一样。

编辑:

与此同时,我已经测试了它,如果我错了,请纠正我,-beginEditing- endEditing 调用似乎非常重要。


5
//For proper line spacing

NSString *text1 = @"Hello";
NSString *text2 = @"\nWorld";
UIFont *text1Font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:10];
NSMutableAttributedString *attributedString1 =
[[NSMutableAttributedString alloc] initWithString:text1 attributes:@{ NSFontAttributeName : text1Font }];
NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle1 setAlignment:NSTextAlignmentCenter];
[paragraphStyle1 setLineSpacing:4];
[attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [attributedString1 length])];

UIFont *text2Font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:16];
NSMutableAttributedString *attributedString2 =
[[NSMutableAttributedString alloc] initWithString:text2 attributes:@{NSFontAttributeName : text2Font }];
NSMutableParagraphStyle *paragraphStyle2 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle2 setLineSpacing:4];
[paragraphStyle2 setAlignment:NSTextAlignmentCenter];
[attributedString2 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle2 range:NSMakeRange(0, [attributedString2 length])];

[attributedString1 appendAttributedString:attributedString2];

3

1
您可以使用此示例,并像这样更改其实现方式:
[self enumerateAttribute:NSParagraphStyleAttributeName
                 inRange:NSMakeRange(0, self.length)
                 options:0
              usingBlock:^(id  _Nullable value, NSRange range, BOOL * _Nonnull stop) {
                  NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

                  //add your specific settings for paragraph
                  //...
                  //...

                  [self removeAttribute:NSParagraphStyleAttributeName range:range];
                  [self addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
              }];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接