iOS 你不知道的新鲜事iOS 开发成长中心

Mac App NSTextField文本垂直居中

2018-11-12  本文已影响0人  LYSNote
#import <Cocoa/Cocoa.h>
@interface BaseTextFieldCell : NSTextFieldCell

@end
#import "BaseTextFieldCell.h"
@implementation BaseTextFieldCell
- (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)frame 
{
    // super would normally draw text at the top of the cell
    NSInteger offset = floor((NSHeight(frame)/2 - ([[self font] ascender] + [[self font] descender])));
    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
上一篇 下一篇

猜你喜欢

热点阅读