【问题标题】:How to resize NSTextView according to its content?如何根据其内容调整 NSTextView 的大小?
【发布时间】:2010-04-16 16:26:56
【问题描述】:

我正在尝试在 NSTextView 中设置属性字符串。我想根据它的内容增加它的高度,最初它被设置为一些默认值。

所以我尝试了这个方法:

我在 NSTextView 中设置内容。当我们在 NSTextView 中设置一些内容时,它的大小会自动增加。所以我增加了它的超级视图的高度,即 NSScrollView 到它的高度,但是 NSScrollView 并没有完全调整大小,它在右边显示滚动条。

float xCoordinate = 15.0;

[xContentViewScroller setFrame:NSMakeRect(xCoordinate, 0.0, 560.0, 10.0)];

[[xContentView textStorage] setAttributedString:xContents];

float xContentViewScrollerHeight = [xfContentView frame].size.height + 2;

[xContentViewScroller setFrame:NSMakeRect(xCoordinate, 0.0, 560.0, xContentViewScrollerHeight)]; 

谁能给我一些解决这个问题的方法或方法。通过谷歌搜索,我发现在 UITextView 中有 contentSize 方法可以获取其内容的大小,我试图在 NSTextView 中找到类似的方法但没有成功:(

【问题讨论】:

  • Peter阐述的解决方案here欢呼

标签: cocoa nstextview


【解决方案1】:

根据@Peter Hosey 的回答,这里是 Swift 4.2 中 NSTextView 的扩展:

extension NSTextView {

    var contentSize: CGSize {
        get {
            guard let layoutManager = layoutManager, let textContainer = textContainer else {
                print("textView no layoutManager or textContainer")
                return .zero
            }

            layoutManager.ensureLayout(for: textContainer)
            return layoutManager.usedRect(for: textContainer).size
        }
    }
}

【讨论】:

  • 这对我不起作用。我正在使用 Swift 5.0。除了上面的代码,我还需要做其他事情吗?
  • 很好的答案,但如果您使用自动布局,这只会给您一行文本的高度。使用自动布局时,我会发现如何找到所有行的总高度。 ?
【解决方案2】:
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];

【讨论】:

    【解决方案3】:
    + (float)heightForString:(NSString *)myString font:(NSFont *)myFont andWidth:(float)myWidth andPadding:(float)padding {
         NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:myString];
         NSTextContainer *textContainer = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(myWidth, FLT_MAX)];
         NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
         [layoutManager addTextContainer:textContainer];
         [textStorage addLayoutManager:layoutManager];
         [textStorage addAttribute:NSFontAttributeName value:myFont
                        range:NSMakeRange(0, textStorage.length)];
         textContainer.lineFragmentPadding = padding;
    
         (void) [layoutManager glyphRangeForTextContainer:textContainer];
         return [layoutManager usedRectForTextContainer:textContainer].size.height;
    }
    

    我使用这个参考做了这个功能:Documentation

    例子:

    float width = textView.frame.size.width - 2 * textView.textContainerInset.width;
    float proposedHeight = [Utils heightForString:textView.string font:textView.font andWidth:width
                                       andPadding:textView.textContainer.lineFragmentPadding];
    

    【讨论】:

      猜你喜欢
      • 2020-10-20
      • 1970-01-01
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 2010-09-08
      • 1970-01-01
      相关资源
      最近更新 更多