Vertical text alignment in NSTextField using Swift

I read various options for adjusting vertical alignment on an NSTextField. I want the text displayed in the center and executed programmatically in Swift. Here is what I just saw:

In Swift, I tried to set the following property:

textField.usesSingleLineMode = true 

Any advice on the best way to vertically center the text would be greatly appreciated!

+5
source share
5 answers

This is very difficult to do, as Apple makes it very difficult. I achieved this by subclassing NSTextFieldCell and ovrriding the drawRectForBounds method: for example:

 override func drawingRectForBounds(theRect: NSRect) -> NSRect { let newRect = NSRect(x: 0, y: (theRect.size.height - 22) / 2, width: theRect.size.width, height: 22) return super.drawingRectForBounds(newRect) } 

This is just my way of doing this, I'm sure there are better ways that I don't know yet. And this only works for the standard font size in TextFields (which gives the height of the text is 22). This is why I hard-coded this. They haven't figured out how to get the height in the cell if you change the font.

Result:

enter image description here

+5
source

Try it on the playground, it perfectly centers the text, use it in your projects! Hope this helps!

 import Cocoa let cell = NSTableCellView() cell.frame = NSRect(x: 0, y: 0, width: 100, height: 100) let tf = NSTextField() tf.frame = cell.frame tf.stringValue = "MyTextfield" tf.alignment = .Center let stringHeight: CGFloat = tf.attributedStringValue.size().height let frame = tf.frame var titleRect: NSRect = tf.cell!.titleRectForBounds(frame) titleRect.size.height = stringHeight + ( stringHeight - (tf.font!.ascender + tf.font!.descender ) ) titleRect.origin.y = frame.size.height / 2 - tf.lastBaselineOffsetFromBottom - tf.font!.xHeight / 2 tf.frame = titleRect cell.addSubview(tf) 
+3
source

The accepted answer works fine and here is the Swift3 version.

 class VerticallyAlignedTextFieldCell: NSTextFieldCell { override func drawingRect(forBounds rect: NSRect) -> NSRect { let newRect = NSRect(x: 0, y: (rect.size.height - 22) / 2, width: rect.size.width, height: 22) return super.drawingRect(forBounds: newRect) } } 
+1
source

I added an NSTextField inside an NSView and focused it.

Another solution (in the iOS project) was to create a UILabel and let it adjust its size (sizeToFit ()) and inject it into the UIView again.

I personally don't like the calculations in the previous answers, and the second iOS solution works for all sizes and line numbers.

0
source

I also ran into the problem of vertical alignment using NSTextField. My requirement included creating a single line string inside an NSTextField. Additionally, the text box must be resized, implying that we have changed the font size in the text box with a change in font size. In this scenario, we ran into vertical alignment problems - incorrect alignment was difficult to understand / understand in the forward direction.

What ultimately worked:

So, in my scenario, simple, disable "Single Line Mode" in the interface builder
for the text field the problem is solved.

0
source

Source: https://habr.com/ru/post/1238752/


All Articles