在UITextView的NSAttributedString上设置字体忽略行间距
我试图在iOS 6中设置一个属性string到UITextView。问题是,如果我尝试设置属性string的字体属性,行间距将被忽略。 但是,如果我不设置字体,并使用默认字体,则行间距工作。
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;
任何想法发生了什么?
归属string编程指南:
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模拟器上的视图不显示属性。
我发现你的问题,因为我也与NSAttributedString战斗。 对于我来说, beginEditing
和endEditing
方法就是这样做的,就像在改变一个Attributed String中所说的那样。 除此之外,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;
虽然没有testing这个确切的代码,顺便说一句,但我的外观几乎相同。
编辑:
同时,我testing了它,如果我错了,纠正我, - beginEditing
和- endEditing
调用似乎是相当重要的。
在iOS 6中存在一个错误,当设置字体时会导致行高被忽略。 请参阅NSParagraphStyle行间距忽略的答案和更长的雷达错误分析:UITextView忽略属性string中的最小/最大行高 。
//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];