How to resize NSTextView according to its content?

前端 未结 4 2209
南方客
南方客 2020-12-14 09:11

I am trying to set an attributed string within NSTextView. I want to increase its height based on its content, initially it is set to some default value.

So I tried

4条回答
  •  不知归路
    2020-12-14 09:31

    NSTextView *textView = [[NSTextView alloc] init];
    textView.font = [NSFont systemFontOfSize:[NSFont systemFontSize]];
    textView.string = @"Lorem ipsum";
    
    [textView.layoutManager ensureLayoutForTextContainer:textView.textContainer];
    
    textView.frame = [textView.layoutManager usedRectForTextContainer:textView.textContainer];
    

提交回复
热议问题