NSTableView 行中的垂直对齐文本
我对 NSTableView 有一个小问题。当我增加表中一行的高度时,其中的文本在行顶部对齐,但我想将其垂直居中对齐!
谁能建议我有什么方法可以做到这一点?
谢谢,
米拉杰
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个简单的代码解决方案,显示了可用于居中对齐 TextFieldCell 的子类。
标头
代码
此博客条目展示了一种也有效的替代解决方案。
This is a simple code solution that shows a subclass you can use to middle align a TextFieldCell.
the header
the code
This blog entry shows an alternative solution that also works well.
下面是基于上面答案构建的代码的 Swift 版本:
然后我在 NSTableView 中设置 tableView heightOfRow 的高度:
将 NSTextFieldCell 的类设置为 VerticallyCenteredTextField:
和 TableViewCell 的高度
< img src="https://i.sstatic.net/mV0Be.png" alt="在此处输入图像描述">
感谢 Bryan 的帮助。
Here is the Swift version of the code building on the answer above:
Then I set the height of the tableView heightOfRow in the NSTableView:
Set the class of the NSTextFieldCell to be VerticallyCenteredTextField:
and the height of the TableViewCell
Thanks Bryan for your help.
即使这是一个非常古老的问题,有一个公认的答案,这里有一个替代解决方案:
进入 IB,选择位于 NSTableCellView 中的 NSTextField 并添加一个新的“在容器中垂直居中”约束。您还应该添加水平约束(可能应该将前导/尾随空格设置为 0 或任何适合您需要的值)。
在 NSOutlineView 中使用它并且效果非常好。在我的例子中,性能不是问题,因为我没有很多单元格,但我希望它不会比手动计算大小差。
Even if this is a very old question with an accepted answer, here's an alternate solution:
Go into the IB, select your NSTextField sitting in a NSTableCellView and add a new "Center Vertically in Container" constraint. You should also add horizontal constraints (which should probably be leading/trailing space set to 0 or whatever suits your needs).
Used this in an NSOutlineView and worked like a charm. Performance wasn't an issue in my case, as I didn't have many cells, but I would expect it's not worse than manually calculating sizes.
@iphaaw 的答案针对 Swift 4 进行了更新(请注意,为了清楚起见,我还在类名末尾添加了“Cell”,这也需要与 Interface Builder 中的类名相匹配):
@iphaaw's answer updated for Swift 4 (note I also added "Cell" on the end of the class name for clarity, which also needs to match the class name in Interface Builder):
对于某些属性字符串(例如带有上标的字符串),仅绘制属性字符串可能会产生奇怪的结果。
我建议在drawInterior中调用super...
Just drawing the attributed string can yield strange results for some attributed strings (with superscript for example).
I’d suggest calling super in drawInterior…