是否有“右”方法有NSTextFieldCell绘制垂直居中的文本吗? [英] Is there a "right" way to have NSTextFieldCell draw vertically centered text?

查看:776
本文介绍了是否有“右”方法有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.

在<$ c $中有一个内部标志c> NSTextFieldCell ,可用于垂直居中的文本,它的作品漂亮。然而,由于它是一个内部标志,它的使用不被苹果认可,它可以简单地在未来的版本中没有警告消失。我目前使用这个内部标志,因为它是简单和有效。苹果显然花了一些时间实现这个功能,所以我不喜欢重新实现它的想法。

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.

我的问题是这样:正确的方式来实现的行为完全像苹果的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 属性,但是这导致我的应用程序从应用商店拒绝。我最后使用一个修改版本的马特贝尔的解决方案,适用于多行,字包装和截断的最后一行:

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,则添加autorelease手动内存管理)

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

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

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