有没有“权利"?让 NSTextFieldCell 绘制垂直居中文本的方法? [英] Is there a "right" way to have NSTextFieldCell draw vertically centered text?

查看:25
本文介绍了有没有“权利"?让 NSTextFieldCell 绘制垂直居中文本的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an NSTableView with several text columns. By default, the dataCell for these columns is an instance of Apple's NSTextFieldCell class, which does all kinds of wonderful things, but it draws text aligned with the top of the cell, and I want the text to be vertically centered in the cell.

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

There is an internal flag in NSTextFieldCell that can be used to vertically center the text, and it works beautifully. However, since it is an internal flag, its use is not sanctioned by Apple and it could simply disappear without warning in a future release. I am currently using this internal flag because it is simple and effective. Apple has obviously spent some time implementing the feature, so I dislike the idea of re-implementing it.

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

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

For the record, here is my current "solution":

@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

用法如下:

[[myTableColumn dataCell] setVerticalCentering:YES];

推荐答案

其他答案不适用于多行.因此,我最初继续使用未记录的 cFlags.vCentered 属性,但这导致我的应用被应用商店拒绝.我最终使用了 Matt Bell 解决方案的修改版本,该解决方案适用于多行、自动换行和截断的最后一行:

The other answers didn't work for multiple lines. Therefore I initially continued using the undocumented cFlags.vCentered property, but that caused my app to be rejected from the app store. I ended up using a modified version of Matt Bell's solution that works for multiple lines, word wrapping, and a truncated last line:

-(void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    NSAttributedString *attrString = self.attributedStringValue;

    /* if your values can be attributed strings, make them white when selected */
    if (self.isHighlighted && self.backgroundStyle==NSBackgroundStyleDark) {
        NSMutableAttributedString *whiteString = attrString.mutableCopy;
        [whiteString addAttribute: NSForegroundColorAttributeName
                            value: [NSColor whiteColor]
                            range: NSMakeRange(0, whiteString.length) ];
        attrString = whiteString;
    }

    [attrString drawWithRect: [self titleRectForBounds:cellFrame] 
                     options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin];
}

- (NSRect)titleRectForBounds:(NSRect)theRect {
    /* get the standard text content rectangle */
    NSRect titleFrame = [super titleRectForBounds:theRect];

    /* find out how big the rendered text will be */
    NSAttributedString *attrString = self.attributedStringValue;
    NSRect textRect = [attrString boundingRectWithSize: titleFrame.size
                                               options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin ];

    /* If the height of the rendered text is less then the available height,
     * we modify the titleRect to center the text vertically */
    if (textRect.size.height < titleFrame.size.height) {
        titleFrame.origin.y = theRect.origin.y + (theRect.size.height - textRect.size.height) / 2.0;
        titleFrame.size.height = textRect.size.height;
    }
    return titleFrame;
}

(此代码假定为 ARC;如果您使用手动内存管理,则在 attrString.mutableCopy 之后添加一个自动释放)

(This code assumes ARC; add an autorelease after attrString.mutableCopy if you use manual memory management)

这篇关于有没有“权利"?让 NSTextFieldCell 绘制垂直居中文本的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆