Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextField Vertical alignment

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..!!

like image 620
iUser Avatar asked Apr 18 '12 07:04

iUser


People also ask

How do you vertically align in TD?

HTML | <td> valign Attribute The HTML <td> valign Attribute is used to specify the vertical alignment of text content in a cell. Attribute Value: top: It sets the content to top-align. middle: It sets the content to middle-align.

What is vertically alignment?

Vertical alignment can be defined as the alignment of a pipe in the vertical direction with respect to the proposed plan. Pipe alignment should not vary more than 2 inches in the vertical direction and not more than 6 inches in the horizontal direction.

Where is vertical alignment located?

In the Page Setup dialog box, choose the Layout tab. In the Page section, select the Vertical alignment drop-down arrow and choose an alignment. In the Preview section, select the Apply to drop-down arrow and choose Selected text. Select OK to apply the alignment to the selected text.


2 Answers

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.

like image 126
sosborn Avatar answered Oct 22 '22 04:10

sosborn


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).

like image 37
Kai Huppmann Avatar answered Oct 22 '22 04:10

Kai Huppmann