NSTextField(多行)在填充所有行后水平增长

问题描述:

我有多行NSTexField,用于显示静态文本,每个窗口显示的长度可能不同。此NSTextField具有高度限制,将其限制为两行。NSTextField(多行)在填充所有行后水平增长

我想设置行为,NSTextField只会水平增长,只会填满那两条水平线。 现在,当我将水平压缩阻力设置得更高时,文本字段宽度会变得非常大,以便在一行上显示所有文本。这只能通过使用自动布局约束来实现,还是必须以某种方式计算nstextfield的宽度?

我试着在superview中重载“layout”方法。但它没有按照我的预期工作。

- (void)layout 
{ 
    [self solveLayoutForView:self]; 
} 

- (void)solveLayoutForView:(NSView*)view 
{ 
    for (NSView* subView in [view subviews]) 
    { 
     if (subView.subviews.count) 
      [self solveLayoutForView:subView]; 
     else 
     { 
      NSLayoutConstraint* height = [subView constraintForAttribute:NSLayoutAttributeHeight]; 

      if (height && height.constant > 17 && [subView isKindOfClass:[NSTextField class]]) // seems like multiline nstextfield 
      { 
       NSTextField* textField = (NSTextField*)subView; 

       textField.preferredMaxLayoutWidth = 0; 
       [super layout]; 
       textField.preferredMaxLayoutWidth = NSWidth([textField alignmentRectForFrame:textField.frame]); 
       [super layout]; 
      } 
     } 
    } 
} 

我最终自己计算它。 我在NSTextField的子类中重载了setStringValue。 “PreferGrowingHorizo​​ntally”是一个子类属性,如果我需要跳过此计算(例如:如果我希望它水平增长)

- (void)setStringValue:(NSString *)stringValue 
{ 
    BOOL changed = ![self.stringValue isEqualToString:stringValue] ? YES : NO; 

    [super setStringValue:stringValue]; 

    if (stringValue.length && changed) 
    { 
     if (self.frame.size.height > 17 && self.preferGrowingHorizontally) 
      [self compensateWidth]; 
    } 
} 

- (void)compensateWidth 
{ 
    float requiredHeight = FLT_MAX; 
    int widthCompensation = 0; 

    while (requiredHeight > self.frame.size.height) 
     requiredHeight = [self heightForStringDrawing:self.stringValue Font:self.font Width:self.frame.size.width + widthCompensation++]; 

    if (--widthCompensation) 
    { 
     NSLayoutConstraint* width = [self constraintForAttribute:NSLayoutAttributeWidth]; 
     if (width) 
      width.constant = self.frame.size.width + widthCompensation; 

     NSLog(@"requiredHeight: %f width compensation: %d for textField: %@", requiredHeight, widthCompensation, self.stringValue); 
    } 
} 

- (float)heightForStringDrawing:(NSString*)myString Font:(NSFont*)myFont Width:(float)myWidth 
{ 
    NSTextStorage *textStorage = [[[NSTextStorage alloc] initWithString:myString] autorelease]; 
    NSTextContainer *textContainer = [[[NSTextContainer alloc] initWithContainerSize:NSMakeSize(myWidth, FLT_MAX)] autorelease]; 

    NSLayoutManager *layoutManager = [[[NSLayoutManager alloc] init] autorelease]; 
    [layoutManager addTextContainer:textContainer]; 
    [textStorage addLayoutManager:layoutManager]; 
    [textStorage addAttribute:NSFontAttributeName value:myFont 
         range:NSMakeRange(0, [textStorage length])]; 
    [textContainer setLineFragmentPadding:0.0]; 
    textContainer.lineBreakMode = NSLineBreakByWordWrapping; 

    (void) [layoutManager glyphRangeForTextContainer:textContainer]; 
    return [layoutManager usedRectForTextContainer:textContainer].size.height; 
}