Setting font on NSAttributedString on UITextView disregards line spacing Setting font on NSAttributedString on UITextView disregards line spacing objective-c objective-c

Setting font on NSAttributedString on UITextView disregards line spacing


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.


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.


//For proper line spacingNSString *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];