Search code examples
iosuitextviewnsattributedstring

Can I adjust the baseline in an NSAttributedString without increasing the line height?


In the San Francisco font, brackets are a bit low if they surround numbers. An example from the app I'm making, using an NSAttributedString:

example

I'd like to increase the baseline for the brackets by a pixel or two, but when I do that using the NSBaselineOffsetAttributeName attribute, the line height is increased by two pixels as well. The text is inside a UITextView, and I really wouldn't like the line height to change. Is there anything I can do?


Solution

  • A possible way to do it is to force the maximum line height with NSParagraphAttributeName.

    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setMaximumLineHeight:maxHeightWanted];
    
    NSDictionary *parenthesisAttributes = @{NSParagraphStyleAttributeName:style, 
                                            NSBaselineOffsetAttributeName:@(baselineWanted), 
                                            NSFontAttributeName:sameFontWithBiggerPointSize};