Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Workaround Sought for: iOS 6 NSAttributedString Descenders Block Underline

I am using NSAttributedString (and NSMutableAttributeString) for writing to a PDF via UIGraphicsBeginPDFContextToFile. When I output an attributed string with underline attributes, the underline is broken when a letters descender goes over underline.

Here is a sample (screen capture) showing the current output:

sample underline image

And here is the code that builds that sample attributed string:

NSAttributedString* ftype = 
       [[NSMutableAttributedString alloc] 
                    initWithString:@"Dangerous"
                        attributes:@{ 
                                   NSParagraphStyleAttributeName:pstyle,
                                   NSFontAttributeName:[UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:48.0], 
                                   NSForegroundColorAttributeName:[UIColor redColor],
                                   NSUnderlineStyleAttributeName:[NSNumber numberWithInt:NSUnderlineStyleSingle]
                                   }
       ];

My client's and my preference is that the underline be continuous, and ideally shifted below the descender.

Manually drawing the underline is difficult as we would have to computer the text position of the words after layout (sometimes the text is not as simple as the example above).

Does anyone have a fix to put the underline either a) lower or b) make it continuous?

Thanks in advance.

like image 647
LarryN Avatar asked Jul 17 '13 10:07

LarryN


1 Answers

I know this was old and after searching found no answers. The solution I found is in the dictionary you pass in for attributes to NSAttributedString initWithString:attributes: add this

NSExpansionAttributeName : @-0.0001f

This scales the glyph, and it seems even with this small amount the underline doesn't get cut (well it didn't with the 13pt font I was using). Though this doesn't seem like the intended use of NSExpansionAttributeName, but couldn't find another solution.

like image 162
anabi Avatar answered Oct 26 '22 00:10

anabi