75

我正在尝试将属性字符串设置为 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;

知道发生了什么吗?

4

5 回答 5

107

属性字符串编程指南:

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 模拟器上的视图未显示属性。

于 2012-11-09T04:13:23.860 回答
26

我找到了你的问题,因为我也在和 NSAttributedString 打架。对我来说,beginEditingandendEditing方法起到了作用,就像在改变属性字符串中所说的那样。除此之外,lineSpacingsetLineSpacing在段落样式中设置。

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

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;

顺便说一句,虽然没有测试这个确切的代码,但我的看起来几乎一样。

编辑:

同时,我已经对其进行了测试,如果我错了,请纠正我,- beginEditingand- endEditing调用似乎非常重要。

于 2015-04-28T11:38:11.503 回答
4
//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];
于 2017-03-10T06:54:43.347 回答
3

iOS 6 中存在一个错误,导致设置字体时忽略行高。请参阅 Radar 中对NSParagraphStyle 行间距忽略和更长的错误分析的回答: UITextView Ignores Minimum/Maximum Line Height in Attributed String

于 2013-07-10T15:32:22.333 回答
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];
              }];
于 2018-07-17T08:41:14.063 回答