相关疑难解决方法(0)

是否有一种"正确"的方式让NSTextFieldCell绘制垂直居中的文本?

我有NSTableView几个文本列.默认情况下,dataCell这些列是Apple NSTextFieldCell类的一个实例,它可以执行各种奇妙的操作,但它会绘制与单元格顶部对齐的文本,并且我希望文本在单元格中垂直居中.

有一个内部标志NSTextFieldCell可以用来垂直居中文本,它工作得很漂亮.但是,由于它是一个内部标志,它的使用不受Apple的批准,它可能会在未来的版本中消失而不会发出警告.我目前正在使用这个内部标志,因为它简单而有效.Apple显然花了一些时间来实现这个功能,所以我不喜欢重新实现它的想法.

所以; 我的问题是:实现与Apple的NStextFieldCell完全相同的行为的正确方法是什么,但是绘制垂直居中的文本而不是顶部对齐?

为了记录,这是我目前的"解决方案":

@interface NSTextFieldCell (MyCategories)
- (void)setVerticalCentering:(BOOL)centerVertical;
@end

@implementation NSTextFieldCell (MyCategories)
- (void)setVerticalCentering:(BOOL)centerVertical
{
    @try { _cFlags.vCentered = centerVertical ? 1 : 0; }
    @catch(...) { NSLog(@"*** unable to set vertical centering"); }
}
@end
Run Code Online (Sandbox Code Playgroud)

使用如下:

[[myTableColumn dataCell] setVerticalCentering:YES];
Run Code Online (Sandbox Code Playgroud)

customization cocoa vertical-alignment nstextfieldcell

50
推荐指数
3
解决办法
2万
查看次数

垂直对齐CATextLayer中的文本?

我正在开发一个我想在Mac和iOS上使用的CATextLayer.我可以控制图层中文本的垂直对齐方式吗?

在这种特殊情况下,我希望将其垂直居中 - 但有关其他垂直对齐的信息也会引起关注.

编辑:我发现了这个,但我不能让它发挥作用.

iphone macos core-animation text-alignment ios

23
推荐指数
6
解决办法
1万
查看次数

使用子类化在NSSecureTextField中垂直居中文本

我正试图在我的NSTextFields中垂直居中文本,但其中一个是密码,所以它是一个NSSecureTextField.我已经将它的类设置MDVerticallyCenteredSecureTextFieldCell为以下实现:

- (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+10);
}

- (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 …
Run Code Online (Sandbox Code Playgroud)

macos objective-c nstextfield

11
推荐指数
2
解决办法
2086
查看次数

获取NSTextField内容以进行扩展

如何才能使文本比例符合我给出的界限?

cocoa scale nstextfield

8
推荐指数
1
解决办法
5134
查看次数