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

Sno*_*man 72 objective-c ios6

我正在尝试将属性字符串设置为iOS 6中的UITextView.问题是,如果我尝试在属性字符串上设置font属性,则会忽略行间距.但是,如果我没有设置字体,并使用默认字体,则行间距有效.

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;
Run Code Online (Sandbox Code Playgroud)

知道发生了什么事吗?

小智 103

归因字符串编程指南:

UIFont *font = [UIFont fontWithName:@"Palatino-Roman" size:14.0];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font
                                forKey:NSFontAttributeName];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"strigil" attributes:attrsDictionary];
Run Code Online (Sandbox Code Playgroud)

更新:我尝试addAttribute:在我自己的应用程序中使用方法,但它似乎无法在iOS 6模拟器上工作:

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

日志似乎显示正确添加的属性,但iOS模拟器上的视图未显示属性.

  • 我在尝试添加属性时遇到了同样的问题.我尝试了你的答案,甚至没有用.任何建议?我认为如果我们正在谈论IOS,你应该编辑你对UIFont而不是NSFont的答案. (6认同)
  • 我不知道为什么人们会投票给这个答案.它不能解决实际问题. (6认同)
  • 它也不适合我,在这方面有任何帮助 (2认同)
  • 这个答案有点旧了所以使用:UIFont*font = [UIFont fontWithName:@"Palatino-Roman"尺寸:14.0]来修复 (2认同)

Jan*_*ash 24

我找到了你的问题因为我还在与NSAttributedString战斗.对我来说,beginEditingendEditing方法一样,如更改属性字符串中所述.除此之外,lineSpacing在paragraphStyle上设置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;
Run Code Online (Sandbox Code Playgroud)

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

编辑:

与此同时,我已经对它进行了测试,如果我错了,请纠正我,- 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];
Run Code Online (Sandbox Code Playgroud)