1

I have an NSTextField where the font used can be changed by the user.

Although the font's point size remains the same, the actual height of the font is much bigger for some fonts. This means if the user changes the font to 'Zapfino' for example, most of the text is cropped. I would like it so the text in the box always looks roughly the same size.

Also the line height seems to change depending on which font is used meaning they don't line up well and sometimes get pushed down and the bottom gets cropped off.

How can I keep the text size and line height looking the same?

Richard Garside
  • 87,839
  • 11
  • 80
  • 93

1 Answers1

1

You need NSTextField which is using single line. You can do it in Attributes inspector by checking Uses Single Line Mode.

How to do it:

Single mode

And now Your text will be like this:

example

Do programatically:

To change NSTextField height programatically by font size or scale the text to fit the bounds example here.

Community
  • 1
  • 1
Justin Boo
  • 10,132
  • 8
  • 50
  • 71
  • This isn't working for me. When I increase the font size the text box stays the same height and the bottom is cropped off. – Richard Garside Feb 15 '12 at 09:29
  • It's now moving the text up and cropping the top off. Are there some settings I'm missing? – Richard Garside Feb 15 '12 at 09:38
  • I'm also not sure this will work if the font is changed using code. I'm using bindings to do this. – Richard Garside Feb 15 '12 at 09:53
  • 1
    If You want to use the same size font all the time then You need to change the height of NSTextField by changing Line Break to Word Wrap (then You can change height to whatever height You want). And If user can change height You can try to change NSTextField height programatically by font size or scale the text to fit the bounds example here: http://stackoverflow.com/questions/2908704/get-nstextfield-contents-to-scale/2911982#2911982. – Justin Boo Feb 15 '12 at 10:56
  • I've changed the height pragmatically using the question you suggested. That has sorted me out. Could you add it to your answer so I can mark it as accepted. It's a bit hidden in the comments. – Richard Garside Feb 24 '12 at 18:38
  • @Richard I updated the answer as You suggested. I'm glad that this solved Your problem. – Justin Boo Feb 24 '12 at 19:50