Set text vertical center in NSTextField

nstextfield vertical alignment swift
nstextfieldcell

In a certain height NSTextField , the layout would be like this if text's size is small

the text is flowing to the top ,how to make the text center like this:

I'm basically using Alex's answer, with one amendment: We should decrease the height of the titleRect if we increase the y-origin. Otherwise our titleRect will end below the end its superview, possibly hiding any content below the NSTextField.

The .h:

#import <Cocoa/Cocoa.h>

@interface VerticallyCenteredTextFieldCell : NSTextFieldCell

@end

The .m:

#import "VerticallyCenteredTextFieldCell.h"

@implementation VerticallyCenteredTextFieldCell

- (NSRect) titleRectForBounds:(NSRect)frame {

CGFloat stringHeight = self.attributedStringValue.size.height;
NSRect titleRect = [super titleRectForBounds:frame];
CGFloat oldOriginY = frame.origin.y;
titleRect.origin.y = frame.origin.y + (frame.size.height - stringHeight) / 2.0;
titleRect.size.height = titleRect.size.height - (titleRect.origin.y - oldOriginY);
return titleRect;
}

- (void) drawInteriorWithFrame:(NSRect)cFrame inView:(NSView*)cView {
[super drawInteriorWithFrame:[self titleRectForBounds:cFrame] inView:cView];
}

@end

Also, VerticallyCenteredTextFieldCell is a subclass of NSTextFieldCell. If you subclass it, change the class of the NSTextFieldCell in Interface Builder, not the NSTextField (I didn't know this).

vertically align nstextfield text · GitHub, When the text field is being edited or selected, we have to turn off the magic because it screws up. // the configuration of the field editor. We sneak around this by  Code Revisions 1 Stars 5. vertically align nstextfield text. Raw. RSVerticallyCenteredTextFieldCell.swift. class RSVerticallyCenteredTextFieldCell: NSTextFieldCell {. var mIsEditingOrSelecting:Bool = false. override func drawingRect ( forBounds theRect: NSRect) -> NSRect {. //Get the parent's idea of where we should draw.

class CustomTextFieldCell: NSTextFieldCell {

    func adjustedFrame(toVerticallyCenterText rect: NSRect) -> NSRect {
        // super would normally draw text at the top of the cell
        var titleRect = super.titleRect(forBounds: rect)

        let minimumHeight = self.cellSize(forBounds: rect).height
        titleRect.origin.y += (titleRect.height - minimumHeight) / 2
        titleRect.size.height = minimumHeight

        return titleRect
    }

    override func edit(withFrame rect: NSRect, in controlView: NSView, editor textObj: NSText, delegate: Any?, event: NSEvent?) {
        super.edit(withFrame: adjustedFrame(toVerticallyCenterText: rect), in: controlView, editor: textObj, delegate: delegate, event: event)
    }

    override func select(withFrame rect: NSRect, in controlView: NSView, editor textObj: NSText, delegate: Any?, start selStart: Int, length selLength: Int) {
        super.select(withFrame: adjustedFrame(toVerticallyCenterText: rect), in: controlView, editor: textObj, delegate: delegate, start: selStart, length: selLength)
    }

    override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
        super.drawInterior(withFrame: adjustedFrame(toVerticallyCenterText: cellFrame), in: controlView)
    }

    override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
        super.draw(withFrame: cellFrame, in: controlView)
    }
}

Swift port of NSTextFieldCell subclass to vertically align text. Original , // Makes the text in an NSTextFieldCell vertically centered. Works with single line, non-editable cells. // Maybe doesn't work with others. This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site.

Just subclass NSTextFieldCell (or swizzle it) ... and implement...

- (NSRect) titleRectForBounds:(NSRect)frame {  

  CGFloat stringHeight       = self.attributedStringValue.size.height;
   NSRect titleRect          = [super titleRectForBounds:frame];
          titleRect.origin.y = frame.origin.y + 
                              (frame.size.height - stringHeight) / 2.0;
   return titleRect;
 }
- (void) drawInteriorWithFrame:(NSRect)cFrame inView:(NSView*)cView {
  [super drawInteriorWithFrame:[self titleRectForBounds:cFrame] inView:cView];
 }

NSTextField vertical alignment - Apple, NSTextField vertical alignment. range:NSMakeRange (0, [attributedString length])]; then the text is vertically centered and not cut off. Icon Bar Menu Icon Accordion Tabs Vertical Tabs Tab Headers Full Page Tabs Hover Tabs Top Navigation Responsive Topnav Navbar with Icons Search Menu Search Bar Fixed Sidebar Side Navigation Responsive Sidebar Fullscreen Navigation Off-Canvas Menu Hover Sidenav Buttons Sidebar with Icons Horizontal Scroll Menu Vertical Menu Bottom Navigation Responsive Bottom Nav Bottom Border Nav Links Right Aligned Menu Links Centered Menu Link Equal Width Menu Links Fixed Menu Slide Down Bar on Scroll Hide

Adding a little to Erik's and Hejazi's answer, you also need to implement selectWithFrame, here is the solution in Swift 2.2:

class VerticallyCenteredTextFieldCell: NSTextFieldCell {
    override func titleRectForBounds(rect: NSRect) -> NSRect {
        var titleRect = super.titleRectForBounds(rect)

        let minimumHeight = self.cellSizeForBounds(rect).height
        titleRect.origin.y += (titleRect.height - minimumHeight) / 2
        titleRect.size.height = minimumHeight

        return titleRect
    }

    override func drawInteriorWithFrame(cellFrame: NSRect, inView controlView: NSView) {
        super.drawInteriorWithFrame(titleRectForBounds(cellFrame), inView: controlView)
    }

    override func selectWithFrame(aRect: NSRect, inView controlView: NSView, editor textObj: NSText, delegate anObject: AnyObject?, start selStart: Int, length selLength: Int) {
        super.selectWithFrame(titleRectForBounds(aRect), inView: controlView, editor: textObj, delegate: anObject, start: selStart, length: selLength);
    }
}

I will pay 50 dollars to anyone who can Align a NSTextField to have , I will pay 50 dollars to anyone who can Align a NSTextField to have Top Aligned Text in Cocoa in Snow Leopard. That's it first And have the "This" be on exactly the same line vertical position. NSTextView *NewTextView = [[NSTextView alloc] init]; [NewTextView I'm proud I did what I set out to do and didn't give up. Select the “Layout” tab and then click the arrow next to “Vertical Alignment” in the “Page” section. A selection of vertical alignment options will appear. Go ahead and click “Center” (or choose another option that better suits your requirements). Your text will now reflect the selected vertical alignment option.

Swift version of Erik Wegener's answer (also modified to work with multiple lines too):

class VerticallyCenteredTextFieldCell : NSTextFieldCell {
    override func titleRect(forBounds rect: NSRect) -> NSRect {
        var titleRect = super.titleRect(forBounds: rect)

        let minimumHeight = self.cellSize(forBounds: rect).height
        titleRect.origin.y += (titleRect.height - minimumHeight) / 2
        titleRect.size.height = minimumHeight

        return titleRect
    }

    override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
        super.drawInterior(withFrame: titleRect(forBounds: cellFrame), in: controlView)
    }
}

Let iOS macOS Chinese font achieve visual vertical centering , If the TextBox size is large and font size is small the Text in TextBox appear There is nothing built into OS X to vertically align a NSTextField  Open the Word document in which you want to vertically align the text. Go to the Layout tab (or Page Layout, depending on the version of Word). In the Page Setup group, select the Page Setup dialog launcher (which is located in the lower-right corner of the group). In the Page Setup dialog box, choose the Layout tab.

How to change TextBox vertical aliment to center., Try using a regular NSTextField, with your NSSecureTextFieldCell subclass inside. I had the same problem and this combination worked. How can I vertically center text in a table cell? I've serached all the Word help and table help resources, but cannot find any directions for vertically centering text in a cell, i.e., so the text is in the middle of the available cell height.

Vertically Centre Text in NSSecureTextField with subclassing, What's the right way to put some text vertically centered in a and add constraints Align Center X and Align Center Y to the NSTextField object. Align Text Vertically Center with CSS vertical-align Property. To align text vertically center, you can use CSS property vertical-align with center as value. You also need to use display:table-cell property of CSS to make text vertically center. Add some width and height to the div element and align text horizontally center also.

What's the right way to put some text vertically centered in a , Now i want vertical alignment of my text and it should be adjustable You can also adjust font size at this point to find a font which fits the new  Vertical center means to set your textview text into vertically middle of activity screen. Developer can set text alignment using xml file but some times there are special needs so you can also set text alignment using programming file. So here is the complete step by step tutorial for Set TextView Vertically center in android programmatically .

Comments
  • Possible duplicate of stackoverflow.com/questions/1235219/…, take a look at it!
  • @Vervious Thanks, it works, but if the text in NSTextField is selected, it back to top-aligned again.
  • @Veelian Any luck with this? I get the same thing where when the user enters text it is top-aligned and then when they click on say another field then it jumps back to centered
  • @Vervious Sorry for not good solution for it.
  • For the record, these are instance methods of NSTextFieldCell, not NSTextField. I put a category on NSTextFieldCell and implemented these methods and it worked quite well.
  • Swizzle it?? Are you kidding me? That's just plain unethical to suggest something like that to solve a problem like this.
  • This code is not working well if the textfield has multiple lines. I have mine with 3 and the last line is simply not showing. I am on 10.10.
  • @Yar This answer isn't, but it contains a link to a solution that requires the use of method swizzling.
  • Helped me a lot, thanks! One suggestion: Decrease the height of the titleRect if you increase the y-origin, otherwise titleRect will extend beyond its superview.
  • You mean you also need to implement selectWithFrame?
  • When you enter the TF to edit it, the text switches back to the top