Search code examples
cocoansstringstring-formattingnsalert

Bolding part of a string in an NSAlert


I'm creating a Mac OS X app and have some issues about formatting strings on an NSAlert. I want the informative text to be formatted as follows :

something something something something

-------something bold in the middle---------

other things other things other things

So far I could only separate the lines using \n statement but I don't know how to make the line in the middle bold and centred. Does anyone have any idea?


Solution

  • You should not do this, and indeed you cannot without subclassing NSAlert, or creating a generic window yourself. The alert can only be given a plain string, which does not include decoration information (you would need an NSAttributedString for that); it will do its own bolding of the text you pass.

    What you can and should do is have a look at "Alerts" in the HIG, and see that there are two strings you can supply to your alert: the "message text", which will be displayed in a bold font, and the "informative text". Make use of those to achieve a result consistent with the platform guidelines and your needs.