【问题标题】:How do I vertically center align text in a NSTextField?如何在 NSTextField 中垂直居中对齐文本?
【发布时间】:2012-01-27 07:57:41
【问题描述】:

我有一个 NSTextField,我想将其中的文本垂直居中对齐。基本上我需要How do I vertically center UITextField Text?的 NSTextField 答案

有人指点一下吗?谢谢!

【问题讨论】:

标签: objective-c xcode macos


【解决方案1】:

你可以继承 NSTextFieldCell 来做你想做的事:

MDVerticallyCenteredTextFieldCell.h:

#import <Cocoa/Cocoa.h>

@interface MDVerticallyCenteredTextFieldCell : NSTextFieldCell {

}

@end

MDVerticallyCenteredTextFieldCell.m:

#import "MDVerticallyCenteredTextFieldCell.h"

@implementation MDVerticallyCenteredTextFieldCell

- (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)frame {
    // super would normally draw text at the top of the cell
    NSInteger offset = floor((NSHeight(frame) - 
           ([[self font] ascender] - [[self font] descender])) / 2);
    return NSInsetRect(frame, 0.0, offset);
}

- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView
         editor:(NSText *)editor delegate:(id)delegate event:(NSEvent *)event {
    [super editWithFrame:[self adjustedFrameToVerticallyCenterText:aRect]
          inView:controlView editor:editor delegate:delegate event:event];
}

- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView
                 editor:(NSText *)editor delegate:(id)delegate 
                  start:(NSInteger)start length:(NSInteger)length {

    [super selectWithFrame:[self adjustedFrameToVerticallyCenterText:aRect]
                    inView:controlView editor:editor delegate:delegate
                     start:start length:length];
}

- (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view {
    [super drawInteriorWithFrame:
       [self adjustedFrameToVerticallyCenterText:frame] inView:view];
}

@end

然后您可以在 Interface Builder 中使用常规 NSTextField,并指定 MDVerticallyCenteredTextFieldCell(或任何您想命名的名称)作为文本字段的文本字段单元格的自定义类(选择文本字段,暂停,然后单击再次选择文本字段以选择文本字段内的单元格):

【讨论】:

  • 这很好,只是它会导致文本溢出到左侧和右侧的文本框框架之外。有什么想法吗?
  • @simon.d:对,你是……嗯。我一直在使用这段代码(我相信它改编自我在某处找到的一些 Apple 示例代码)一段时间没有问题,尽管我现在意识到它只有单行(非包装)NSTextFields。我会再看看它,看看我是否不能让它也适用于多行文本字段......
【解决方案2】:

Swift 3.0 版本(为 NSTextFieldCell 创建自定义子类):

override func drawingRect(forBounds rect: NSRect) -> NSRect {
    var newRect = super.drawingRect(forBounds: rect)
    let textSize = self.cellSize(forBounds: rect)
    let heightDelta = newRect.size.height - textSize.height
    if heightDelta > 0 {
        newRect.size.height -= heightDelta
        newRect.origin.y += (heightDelta / 2)
    }
    return newRect
}

【讨论】:

  • @JFS 这对我有用。但它不起作用 SecureTextFielCell
【解决方案3】:

在计算可能的最大字体高度时最好使用boundingRectForFont和函数ceilf(),因为上述解决方案会导致文本在基线下被截断。所以adjustedFrameToVerticallyCenterText: 看起来像这样

- (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)rect {
    CGFloat fontSize = self.font.boundingRectForFont.size.height;
    NSInteger offset = floor((NSHeight(rect) - ceilf(fontSize))/2);
    NSRect centeredRect = NSInsetRect(rect, 0, offset);
    return centeredRect;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-06
    • 2012-07-31
    • 2013-06-06
    • 2013-05-25
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    相关资源
    最近更新 更多