垂直对齐NSTableView行中的文本 [英] vertically aligning text in NSTableView row

查看:273
本文介绍了垂直对齐NSTableView行中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题与NSTableView。当我增加表中行的高度时,其中的文本在行的顶部对齐,但是我想将它对齐垂直居中!



任何人都可以建议我任何方式做。



谢谢,



Miraaj

解决方案

这是一个简单的代码解决方案,显示一个子类,

  #import< Cocoa /可可。 


@interface MiddleAlignedTextFieldCell:NSTextFieldCell {

}

@end
pre>

代码

  @implementation MiddleAlignedTextFieldCell 

- (NSRect)titleRectForBounds:(NSRect)theRect {
NSRect titleFrame = [super titleRectForBounds:theRect];
NSSize titleSize = [[self attributStringValue] size];
titleFrame.origin.y = theRect.origin.y - .5 +(theRect.size.height - titleSize.height)/ 2.0;
return titleFrame;
}

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSRect titleRect = [self titleRectForBounds:cellFrame];
[[self attributStringValue] drawInRect:titleRect];
}

@end

此博客条目显示了一个替代解决方案,也可以很好地工作。


I have a small problem with NSTableView. When I am increasing height of a row in table, the text in it is aligned at top of row but I want to align it vertically centered!

Can anyone suggest me any way to do it ??

Thanks,

Miraaj

解决方案

This is a simple code solution that shows a subclass you can use to middle align a TextFieldCell.

the header

#import <Cocoa/Cocoa.h>


@interface MiddleAlignedTextFieldCell : NSTextFieldCell {

}

@end

the code

@implementation MiddleAlignedTextFieldCell

- (NSRect)titleRectForBounds:(NSRect)theRect {
    NSRect titleFrame = [super titleRectForBounds:theRect];
    NSSize titleSize = [[self attributedStringValue] size];
    titleFrame.origin.y = theRect.origin.y - .5 + (theRect.size.height - titleSize.height) / 2.0;
    return titleFrame;
}

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    NSRect titleRect = [self titleRectForBounds:cellFrame];
    [[self attributedStringValue] drawInRect:titleRect];
}

@end

This blog entry shows an alternative solution that also works well.

这篇关于垂直对齐NSTableView行中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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