19

I am creating cocoa app in which i created NSTextField programmatically like this,

NSView *superView = [[NSView alloc] initWithFrame:NSMakeRect(0, 300, 1400, 500)];
NSTextField *myTextField = [[NSTextField alloc] initWithFrame:NSMakeRect(180, 100, 1000, 300)];
[myTextField setStringValue:myText];
[myTextField setEditable:NO];
[myTextField setBezeled:NO];
[myTextField setDrawsBackground:NO];
[myTextField setSelectable:NO];
[myTextField setFont:[NSFont fontWithName:@"Futura" size:22]];
[superView addSubview:myTextField];
[superView setAutoresizesSubviews:YES];
[myTextField setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[self.window.contentView addSubview:superView];  

Now i want vertical alignment of my text and it should be adjustable according to text length.
Anyone have any suggestions? Please share your suggestion :)

Many Thanks..!!

Keith Smiley
  • 61,481
  • 12
  • 97
  • 110
iUser
  • 1,075
  • 3
  • 20
  • 49

4 Answers4

14

What you want is possible, but you'll have to make a subclass of NSTextFieldCell, and then use that subclass in your NSTextField. The key methods you want override are drawingRectForBounds:, selectWithFrame:, and editWithFrame:

Here is a blog post from the fantastic Daniel Jalkut about this, he even includes a downloadable version ready to go. The post is fairly old but it should still work fine.

sosborn
  • 14,676
  • 2
  • 42
  • 46
  • Thanks sosborn..!! Let me try this.. I'll get back to you if any problem in this. :) – iUser Apr 18 '12 at 08:21
  • Ok, I have `RSVerticallyCenteredTextFieldCell.h` and `RSVerticallyCenteredTextFieldCell.m` class but i don't know how to use it, with my `myTextField`. – iUser Apr 18 '12 at 08:50
  • Take a look at the NSControl (From which NSTextField inherits). It has a method called setCellClass:. The Control and Cell Programming Guide doc from Apple is also good to read. – sosborn Apr 18 '12 at 08:57
1

For adjusting the text field to string size you have to calculate the text size with NSString's sizeWithFont: constrainedToSize: lineBreakMode: before and than adjust the text field's size with setting the frame.

For vertical alignment look at this question (and the answers).

Community
  • 1
  • 1
Kai Huppmann
  • 10,705
  • 6
  • 47
  • 78
0

Here is a Swift version of the solution linked to by sosborn above:

open class VerticallyCenteredTextFieldCell: NSTextFieldCell {
    
    fileprivate var isEditingOrSelecting : Bool = false
    
    open override func drawingRect(forBounds rect: NSRect) -> NSRect {
        let rect = super.drawingRect(forBounds: rect)
        
        if !isEditingOrSelecting {
            let size = cellSize(forBounds: rect)
            return NSRect(x: rect.minX, y: rect.minY + (rect.height - size.height) / 2, width: rect.width, height: size.height)
        }
        
        return rect
    }
    
    open override func select(withFrame rect: NSRect, in controlView: NSView, editor textObj: NSText, delegate: Any?, start selStart: Int, length selLength: Int) {
        let aRect = self.drawingRect(forBounds: rect)
        isEditingOrSelecting = true
        super.select(withFrame: aRect, in: controlView, editor: textObj, delegate: delegate, start: selStart, length: selLength)
        isEditingOrSelecting = false
    }
    
    open override func edit(withFrame rect: NSRect, in controlView: NSView, editor textObj: NSText, delegate: Any?, event: NSEvent?) {
        let aRect = self.drawingRect(forBounds: rect)
        isEditingOrSelecting = true
        super.edit(withFrame: aRect, in: controlView, editor: textObj, delegate: delegate, event: event)
        isEditingOrSelecting = false
    }
    
}
PKCLsoft
  • 1,359
  • 2
  • 26
  • 35
-1

override ViewWillDraw and position your NSTextField in the center of its parent view at this point. This works with live resizing as well. You can also adjust font size at this point to find a font which fits the new size.

Radu Simionescu
  • 4,518
  • 1
  • 35
  • 34
  • explain your downvote and note that this was answered in 2015. This is similar to the accepted answer, just that instead of making the custom drawing in the NSTextFieldCell, you do it in the NSTextField... – Radu Simionescu Jul 03 '17 at 14:28