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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With