在某个高度 NSTextField ,如果文本的大小很小,布局将是这样的
文本流到顶部,如何使文本中心像这样:
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)
}
}
我基本上使用亚历克斯的答案,但有一个修正:如果我们增加 y 原点,我们应该减少 titleRect 的高度。否则,我们的 titleRect 将在其父视图的末尾下方结束,可能会将任何内容隐藏在 NSTextField 下方。
.h:
#import <Cocoa/Cocoa.h>
@interface VerticallyCenteredTextFieldCell : NSTextFieldCell
@end
他们:
#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
此外,VerticallyCenteredTextFieldCell 是 NSTextFieldCell 的子类。如果您将其子类化,请在 Interface Builder 中更改 NSTextFieldCell 的类,而不是 NSTextField(我不知道这一点)。
只是子类NSTextFieldCell
(或混合它)......并实现......
- (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];
}
在 Erik 和 Hejazi 的回答中添加一些内容,您还需要实现selectWithFrame
,这是 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);
}
}
Erik Wegener 的答案的 Swift 版本(也修改为使用多行):
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)
}
}
我的解决方案是基于使用 NSLayoutManager 计算字体高度。这种方式允许使用属性字符串的方法计算高度的更多性能。
- (NSRect)titleRectForBounds:(NSRect)theRect {
NSRect titleFrame = [super titleRectForBounds:theRect];
CGFloat fontHeight = [[[NSLayoutManager alloc] init] defaultLineHeightForFont:self.font];
if (fontHeight < titleFrame.size.height) {
titleFrame.origin.y = theRect.origin.y + (theRect.size.height - fontHeight) * 0.5f;
titleFrame.size.height = fontHeight;
}
return titleFrame;
}
- (void) drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view {
[super drawInteriorWithFrame:[self titleRectForBounds:frame] inView:view];
}