How to center a NSTextField in a view

Elfoiros picture Elfoiros · Jun 3, 2012 · Viewed 7k times · Source

i've a NSTextField (label) centered with IB in a view with the text "__DRAG_AND_DROP_YOUR_MEDIA_HERE". In my controller a use NSLocalizedString to translate the text :

English.lproj/Localizable.strings :

"__DRAG_AND_DROP_YOUR_MEDIA_HERE" = "Drag & Drop your media here"

French.lproj/Localizable.strings :

"__DRAG_AND_DROP_YOUR_MEDIA_HERE" = "Déposer vos média ici"

Only the initial text "__DRAG_AND_DROP_YOUR_MEDIA_HERE" is centerred in the view. The translated text is not centerrd (depending on is lenght). All the translated text start at the same absciss x than the orignal text. How can i fix this issue ?

PS: I don't want to play with setFrame, height, width just for a simple point like this. I guess there's another method to do this.

Thanks.

Answer

Ken Thomases picture Ken Thomases · Jun 3, 2012

Set the text field's alignment to NSCenterTextAlignment. You can do this from IB or programmatically using -setAlignment:.