Setting font on NSAttributedString on UITextView disregards line spacing

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

I'm trying to set an attributed string to a UITextView in iOS 6. The problem is, if I attempt to set the font property on the attributed string, the line spacing is ignored. However, if I don't set the font, and the default font is used, then line spacing works.

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; 

Any idea what's going on?

回答1:

Attributed String Programming Guide:

UIFont *font = [UIFont fontWithName:@"Palatino-Roman" size:14.0]; NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font                                 forKey:NSFontAttributeName]; NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"strigil" attributes:attrsDictionary]; 

Update: I tried to use addAttribute: method in my own app, but it seemed to be not working on the iOS 6 Simulator:

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

The log seems to show correctly added attributes, but the view on iOS simulator was not display with attributes.



回答2:

I found your question because I was also fighting with NSAttributedString. For me, the beginEditing and endEditing methods did the trick, like stated in Changing an Attributed String. Apart from that, the lineSpacing is set with setLineSpacing on the paragraphStyle.

So you might want to try changing your code to:

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; 

Didn't test this exact code though, btw, but mine looks nearly the same.

EDIT:

Meanwhile, I've tested it, and, correct me if I'm wrong, the - beginEditing and - endEditing calls seem to be of quite an importance.



回答3:

There was a bug in iOS 6, that causes line height to be ignored when font is set. See answer to NSParagraphStyle line spacing ignored and longer bug analysis at Radar: UITextView Ignores Minimum/Maximum Line Height in Attributed String.



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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!