0

I am using custom font for non-editable NSTextField, which I created in StoryBoard:

generalDisplay.font = NSFont(name: "DS-Digital Bold", size: 25.0)

Then I am adjusting frame height:

generalDisplay.frame.size.height = 28

The result is not centered vertically:

enter image description here

I've tried to turn off single line mode, but the result is even worse.

enter image description here

If I should subclass it, could you give me an example what methods I have to override?

Pablo
  • 28,133
  • 34
  • 125
  • 215
  • Possible duplicate of [Vertically aligning text in an NSTextField using Swift](https://stackoverflow.com/questions/34379353/vertically-aligning-text-in-an-nstextfield-using-swift) – Willeke Sep 30 '18 at 16:19

2 Answers2

0

You can subclass UITextField and override this method:

override func textRect(forBounds bounds: CGRect) -> CGRect { 
   return bounds.inset(by: UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 10)) 
} 
Andrew21111
  • 868
  • 8
  • 17
0

Looking at the Apple documentation for NSTextfield maybe you could initialize your NSTextField with a NSAttributedString to be able to change the baseline

NSTextfield(labelWithAttributedString: NSAttributedString)
James Woodrow
  • 365
  • 1
  • 14
  • `Use of unresolved identifier 'NSBaselineOffsetAttributeName'`, `Value of type 'NSTextField' has no member 'defaultTextAttributes'` – Pablo Sep 30 '18 at 10:13
  • Just read your comment on the other answer thought this was for iOS swift and confused it with `UITextfield` but maybe this can help since it talks about baseline offsets here as well https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/TextUILayer/Tasks/SetTextAttributes.html – James Woodrow Sep 30 '18 at 10:16
  • After reading the documentation I updated my answer, maybe it would work, I don't really know the context to be able to narrow down the search myself but if it doesn't help hope someone else will have better knowledge of `NSTextfield` – James Woodrow Sep 30 '18 at 10:23
  • `let atrString = NSMutableAttributedString() atrString.append(NSAttributedString(string: display.data, attributes: [NSAttributedStringKey.baselineOffset:15.0])) generalDisplay.attributedStringValue = atrString` doesn't move offset inside text field... – Pablo Sep 30 '18 at 11:19
  • Setting field to single line mode moves to base offset. But I wonder if the offset can be calculated based on font size. – Pablo Sep 30 '18 at 11:36
  • You could probably subclass NSAttributedString so that it would recalculate the offset after setting the font or the font size – James Woodrow Sep 30 '18 at 17:24