Setting a font to NSAttributedString in a UITextView ignores line spacing

I am trying to set the attribute string in a UITextView in iOS 6. The problem is that if I try to set the font property in the attribute string, the line spacing will be ignored. However, if I do not install 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 is going on?

+60
objective-c ios6
Oct 02 '12 at 16:12
source share
4 answers

Programming Guide with String Attributes:

 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 using the addAttribute: method in my own application, but it didn't seem to work on iOS 6 Simulator:

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

The log appears to display correctly added attributes, but the view on the iOS simulator does not display with attributes.

+93
Nov 09
source share

I found your question because I also fought with NSAttributedString. For me, the beginEditing and endEditing did the trick, as indicated in Changing the attribute string . In addition, lineSpacing is set using setLineSpacing in Style.

So you can try changing the code:

 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; 

I have not tested this exact code, although, by the way, but mine looks almost the same.

EDIT:

Meanwhile, I tested it, and correct me if I am mistaken, the calls - beginEditing and - endEditing seem quite important.

+20
Apr 28 '15 at 11:38
source share

There was an error in iOS 6 that caused the line height to be ignored when setting the font. See Response to Row Space NSParagraphStyle is ignored and longer error analysis in Radar: UITextView ignores min / max line Height in attribute string .

+3
Jul 10 '13 at 15:32
source share
 //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]; 
+1
Mar 10 '17 at 6:54
source share



All Articles