cocoaunderlinensfont

Create the underlined font in Cocoa


ALL,

According to this NSFont has only method to retrieve the underline font parameters.

Is there a way to set the font to be underlined?

Thank you.


Solution

  • Is there a way to set the font to be underlined?

    No, but that's just because you're barking up the wrong tree.

    A "font" can't be underlined, (at least, I don't think), but a string can be.

    Look at NSAttributedString... You create a dictionary of "attributes", which you attach to the NSString you want to draw... "Obliqueness", (means Italic or not), "Centered"... all that stuff. So it's like this:

    [textAttrDict setObject:[NSNumber numberWithInt:NSUnderlineStyleSingle] forKey:NSUnderlineStyleAttributeName];
    [textStr drawInRect:textRect withAttributes:textAttrDict];
    

    Search for the NSUnderlineStyleSingle in the headers to find the exact enum that you want...