我创建了NSTabViewItem的子类,以便可以为选项卡视图项指定特定的宽度,如this answer所示。效果很好,但是选项卡的标签未水平居中,因此文本右侧有多余的填充。如何在标签范围内居中放置文本,或者如何在固定宽度NSTabViewItem内正确居中放置文本?

最佳答案

您可以覆盖“ drawLabel:inRect:”函数,并在此处提供用于绘制的适当矩形。喜欢

 (void)drawLabel:(BOOL)shouldTruncateLabel
       inRect:(NSRect)labelRect{
   //find your label size
   NSDictionary* attr = [NSDictionary dictionaryWithObjectsAndKeys:
                      self.tabView.font,NSFontAttributeName,
                      nil];
     NSSize labelSize = [self.label sizeWithAttributes:attr];
   //modify your labelRect here.....
    labelRect.origin.x += floor((labelRect.size.width - labelSize.width)/2)
     labelRect.size.width = labelSize.width;
   //call super
    [super drawWithFrame:shouldTruncateLabel  inRect:labelRect];
 }

关于macos - NSTabViewItem中的水平居中标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29269374/

10-09 01:48