2011-12-24 86 views

Trả lời

18

Bạn có thể phân lớp NSTextFieldCell để làm những gì bạn muốn:

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 

Sau đó, bạn có thể sử dụng thường xuyên NSTextField trong Interface Builder và chỉ định MDVerticallyCenteredTextFieldCell (hoặc bất kỳ thứ gì bạn muốn đặt tên) làm lớp tùy chỉnh cho văn bản f tế bào trường văn bản ield của (chọn textfield, tạm dừng, sau đó nhấp vào textfield lần nữa để chọn các tế bào bên trong lĩnh vực văn bản):

enter image description here

+0

điều này thật tuyệt, ngoại trừ việc nó làm cho văn bản tràn ra ngoài khung văn bản ở bên trái và bên phải. bất kỳ ý tưởng? –

+0

@ simon.d: đúng rồi ... Hmm. Tôi đã sử dụng mã này (mà tôi tin là thích nghi từ một số mẫu mã Apple mà tôi tìm thấy ở đâu đó) trong một thời gian không có vấn đề, mặc dù tôi nhận ra bây giờ nó chỉ với một dòng (không gói) 'NSTextField's. Tôi sẽ xem xét lại nó để xem liệu tôi có thể không làm cho nó hoạt động cho các trường văn bản nhiều dòng ... – NSGod

+0

editWithFrame không được gọi –

2

Nó tốt hơn để sử dụng boundingRectForFont và chức năng ceilf() khi tính toán có thể chiều cao chữ tối đa , bởi vì giải pháp nêu trên làm cho văn bản bị cắt theo đường cơ sở. Vì vậy, các adjustedFrameToVerticallyCenterText: sẽ trông giống như phiên bản này

- (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; 
} 
1

Swift 3.0 (Tạo một lớp con tùy chỉnh cho 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 
} 
Các vấn đề liên quan