0

我创建了一个子类,NSTabViewItem以便可以为选项卡视图项指定特定宽度,如本答案所示。这很好用,但是选项卡的标签不是水平居中的,因此文本右侧有额外的填充。如何在标签范围内将文本居中,或者如何将文本正确居中在固定宽度内NSTabViewItem

在此处输入图像描述

4

1 回答 1

1

You can override "drawLabel:inRect:" function and give the appropriate rect for drawing here. Like

 (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];
 }
于 2015-03-26T07:00:03.770 回答