Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UItextview will disregard line height when NSFontAttributeName is set

Well, Ive searched in several places and although some people allegedly have found fixes it doesn't seem to apply to my case.

I'm trying to procedurally set the line height of a few UItextviews like this :

 UITextView *lab = [LocalTexts objectAtIndex:j];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineHeightMultiple = 50.0f;
    paragraphStyle.maximumLineHeight = 50.0f;
    paragraphStyle.minimumLineHeight = 50.0f;

    NSString *string = lab.text;
    NSDictionary *ats = @{
    NSFontAttributeName : [UIFont fontWithName:@"DIN Medium" size:16.0f],
    NSParagraphStyleAttributeName : paragraphStyle, 
    };

    lab.attributedText = [[NSAttributedString alloc] initWithString:string attributes:ats];

Strange thing is that if I disable the NSFontAttributeName assignment, the line height will work, also, if I set the Paragraph style to have a certain paragraph height, that always works too, so the NSParagraphStyleAttribute IS NOT being fully ignored. I dont know if it is a bug or I'm actually doing something wrong.

I tried implementing it as pure CORE TEXT, but it is a bit too complex for the current scope of the project.

Hope someone can point me in the right direction. Thanks.

like image 335
Bernardo Del Castillo Suarez Avatar asked Nov 13 '22 19:11

Bernardo Del Castillo Suarez


1 Answers

This is a known bug in NSHTMLWriter which is used by UITextView to convert your attributed string to HTML: http://www.cocoanetics.com/2012/12/radar-uitextview-ignores-minimummaximum-line-height-in-attributed-string/

You can use UITextView replacement we have in DTCoreText to render this text correctly: https://github.com/Cocoanetics/DTCoreText

like image 58
Cocoanetics Avatar answered Jan 04 '23 02:01

Cocoanetics