Tim
Tim

Reputation: 5054

NSTextField vertically misaligns text when using non-default typeface

I am having trouble making text fields look acceptable when using different typefaces.

See this screenshot taken from a test app I made to demonstrate the problem. It consists of a single XIB, with no code in the delegate or anywhere else. This is on OSX Mavericks with Xcode 5.1.1 but I haven't tried on other versions.

Test app screenshot

The default system font looks fine in the top text field, as you'd expect. Compare this to the one below: same exact size and shape, all I did differently was change the typeface in Interface Builder. Text is pushed downwards, and descending letters (lowercase pqjg) are clipped. The Menlo example below is also pushed downwards, though not quite as badly.

How do I fix this?

Upvotes: 2

Views: 806

Answers (2)

l'L'l
l'L'l

Reputation: 47284

You can resolve this a couple of different ways:

• Enable "Uses Single Line Mode" on the Text Field Panel. When I first encountered this issue it wasn't obvious that this would automatically align the text vertically within an NSTextField.

or...

• Simply adjust the size of the NSTextField using the adjustment boxes that appear after you click on it. You can also adjust them through the "Control" properties.


XCode Text Properties

Upvotes: 2

Abhi Beckert
Abhi Beckert

Reputation: 33379

A quick test shows Lucida Grande and Helvetica require 17 pixels, and Menlo requires 19 pixels:

playground

Note that this excludes the border and shadows, so you need your text view to be significantly larger than that to guarantee it will fit.

Interface Builder has been specifically designed for Lucida Grande, it knows it can get away with being too small because that font doesn't use all the space it has available. Doesn't work well with other fonts.

Upvotes: 2

Related Questions