在NSTextField中设置文本垂直中心 [英] Set text vertical center in NSTextField

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

问题描述

在一定高度的NSTextField中,如果文字的大小很小,布局就会是这样。

In a certain height NSTextField , the layout would be like this if text's size is small

文本流向顶部,如何使文本中心如下:

the text is flowing to the top ,how to make the text center like this:

>

推荐答案

只是子类 NSTextField 或swizzle it )...并实现...

Just subclass NSTextField (or swizzle it) ... and implement...

- (NSRect) titleRectForBounds:(NSRect)frame {  

  CGFloat stringHeight       = self.attributedStringValue.size.height;
   NSRect titleRect          = [super titleRectForBounds:frame];
          titleRect.origin.y = frame.origin.y + 
                              (frame.size.height - stringHeight) / 2.0;
   return titleRect;
 }
- (void) drawInteriorWithFrame:(NSRect)cFrame inView:(NSView*)cView {
  [super drawInteriorWithFrame:[self titleRectForBounds:cFrame] inView:cView];
 }

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

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