【问题标题】:Auto-Resizing of NSTextView and/or NSScrollViewNSTextView 和/或 NSScrollView 的自动调整大小
【发布时间】:2013-11-04 21:52:29
【问题描述】:

我在NSView 中有一个NSTextViewNSPopover 正在使用它,不知道这是否相关),我正在尝试以编程方式自动调整大小(参见标题)。

我一直在为很多事情苦苦挣扎,即:

  • 查看NSLayoutManagerusedRectForTextContainer 给我的尺寸值异常 (usedRectForTextContainer : {{0, 0}, {0.001, 28}})
  • 修改NSScrollView frame[NSScrollView contentView][NSScrollView documentView]
  • 摆脱自动布局

我可以调整 scollview 和 Popover 的大小,但我无法获得 NSTextView 中文本的实际高度。

我们将不胜感激。

-(void)resize
{
    //Get clipView and scrollView
    NSClipView* clip = [popoverTextView superview];
    NSScrollView* scroll = [clip superview];

    //First, have an extra long contentSize and ScrollView to test everything else
    [popover setContentSize:NSMakeSize([popover contentSize].width, 200)];
    [scroll setFrame:(NSRect){
        [scroll frame].origin.x,[scroll frame].origin.y,
        [scroll frame].size.width,155
    }];



    NSLayoutManager* layout = [popoverTextView layoutManager];
    NSTextContainer* container = [popoverTextView textContainer];

    NSRect myRect = [layout usedRectForTextContainer:container]; //Broken
    //Now what ?

}

【问题讨论】:

  • 这种情况是否仅在第一次调用 resize 时发生?我问是因为 usedRectForTextContainer: 在返回矩形之前不做任何布局。这是在小牛队吗?
  • 是的,它在 Mavericks 上,不,它变化很大(第一次调用,给出一个 OK 的结果,然后它的高度缩小到 0.001)。如果您在 Github 上,如果您想尝试 fork 并使用它,该项目是 github.com/bertrand-caron/BCFirstLaunchTutorial
  • 很愿意。谢谢。
  • 有趣的项目

标签: cocoa nstextview nsscrollview


【解决方案1】:

我仍然不知道为什么我不能使用[layout usedRectForTextContainer:container],但我设法通过使用获得了NSTextView 的高度:

-(void)resize
{
    //Get glyph range for boundingRectForGlyphRange:
    NSRange range = [[myTextView layoutManager] glyphRangeForTextContainer:container];

    //Finally get the height
    float textViewHeight = [[myTextView layoutManager] boundingRectForGlyphRange:range
                                       inTextContainer:container].size.height;
}

【讨论】:

  • 您可以使用usedRectForTextContainer:,它没有损坏。问题是它没有布局文本,所以你需要先调用glyphRangeForTextContainer:,才能布局文本。
【解决方案2】:

这是我自己测试过的一些功能代码。 usedRectForTextContainer 没有损坏,它只是懒惰地设置。要设置它,我调用 glyphrangefortextcontainer

我在这里找到了答案:http://stpeterandpaul.ca/tiger/documentation/Cocoa/Conceptual/TextLayout/Tasks/StringHeight.html

    let textStorage = NSTextStorage(string: newValue as! String)
    let textContainer = NSTextContainer(size: NSSize(width: view!.Text.frame.width, height:CGFloat.max))
    let layoutManager = NSLayoutManager()

    layoutManager.addTextContainer(textContainer)
    textStorage.addLayoutManager(layoutManager)

    textContainer.lineFragmentPadding = 0.0
    textContainer.lineBreakMode = NSLineBreakMode.ByWordWrapping
    textStorage.font = NSFont(name: "Times-Roman", size: 12)

    layoutManager.glyphRangeForTextContainer(textContainer)

    let rect = layoutManager.usedRectForTextContainer(textContainer)

    Swift.print(rect)

文档也表明是这种情况:

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-08-18
  • 2012-02-21
  • 1970-01-01
  • 1970-01-01
  • 2020-10-20
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
相关资源
最近更新 更多