NSTextField Vertical alignment
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.
How do I vertically center UITextField Text?
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
In swift use:
textField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
Is there a right way to have NSTextFieldCell draw vertically centered text?
The other answers didn't work for multiple lines. Therefore I initially continued using the undocumented cFlags.vCentered
property, but that caused my app to be rejected from the app store. I ended up using a modified version of Matt Bell's solution that works for multiple lines, word wrapping, and a truncated last line:
-(void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSAttributedString *attrString = self.attributedStringValue;
/* if your values can be attributed strings, make them white when selected */
if (self.isHighlighted && self.backgroundStyle==NSBackgroundStyleDark) {
NSMutableAttributedString *whiteString = attrString.mutableCopy;
[whiteString addAttribute: NSForegroundColorAttributeName
value: [NSColor whiteColor]
range: NSMakeRange(0, whiteString.length) ];
attrString = whiteString;
}
[attrString drawWithRect: [self titleRectForBounds:cellFrame]
options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin];
}
- (NSRect)titleRectForBounds:(NSRect)theRect {
/* get the standard text content rectangle */
NSRect titleFrame = [super titleRectForBounds:theRect];
/* find out how big the rendered text will be */
NSAttributedString *attrString = self.attributedStringValue;
NSRect textRect = [attrString boundingRectWithSize: titleFrame.size
options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin ];
/* If the height of the rendered text is less then the available height,
* we modify the titleRect to center the text vertically */
if (textRect.size.height < titleFrame.size.height) {
titleFrame.origin.y = theRect.origin.y + (theRect.size.height - textRect.size.height) / 2.0;
titleFrame.size.height = textRect.size.height;
}
return titleFrame;
}
(This code assumes ARC; add an autorelease after attrString.mutableCopy if you use manual memory management)
vertically aligning text in NSTableView row
This is a simple code solution that shows a subclass you can use to middle align a TextFieldCell.
the header
#import <Cocoa/Cocoa.h>
@interface MiddleAlignedTextFieldCell : NSTextFieldCell {
}
@end
the code
@implementation MiddleAlignedTextFieldCell
- (NSRect)titleRectForBounds:(NSRect)theRect {
NSRect titleFrame = [super titleRectForBounds:theRect];
NSSize titleSize = [[self attributedStringValue] size];
titleFrame.origin.y = theRect.origin.y - .5 + (theRect.size.height - titleSize.height) / 2.0;
return titleFrame;
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSRect titleRect = [self titleRectForBounds:cellFrame];
[[self attributedStringValue] drawInRect:titleRect];
}
@end
This blog entry shows an alternative solution that also works well.
Related Topics
What's the Difference Between Using or Not Using the 'Where' Clause with Generics
How to Change Navigation Bar & Back Button Colour iOS 15
Arkit - Viewport Size VS Real Screen Resolution
Parameters After Opening Bracket
How to Make the Memberwise Initialiser Public, by Default, for Structs in Swift
Swift's JSONdecoder with Multiple Date Formats in a JSON String
Differencebetween Http Parameters and Http Headers
How to Hide Labels in iOS-Charts
How to Retrieve Alamofire Response Header for a Request
Binary Operator Cannot Be Applied to Operands of Type Int and Int? Swift 3
Curl Through Nstask Not Terminating If a Pipe Is Present
Swiftui - Wait Until Firestore Getdocuments() Is Finished Before Moving On
Spacer Not Working with Form Inside a VStack
Change Uibarbuttonitem from Uisearchbar