Auto-Resizing of NSTextView and/or NSScrollView

*爱你&永不变心* 提交于 2021-02-07 08:21:30

问题


I have an NSTextView inside an NSView (which is being used by a NSPopover, don't know if that is relevant) that I'm trying to resize automatically and programmatically (cf caption).

enter image description here

I have been struggling with a lot of stuff, namely :

  • Looking at NSLayoutManager and usedRectForTextContainer that give me aberrant size values (usedRectForTextContainer : {{0, 0}, {0.001, 28}})
  • Modifying NSScrollView frame, [NSScrollView contentView], [NSScrollView documentView]
  • Getting rid of AutoLayout

I reached the point where I can resize my scollview and my Popover, but I can't get the actual height of the text inside the NSTextView.

Any kind of help would be appreciated.

-(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 ?

}

回答1:


I still have no idea why I can't use [layout usedRectForTextContainer:container], but I managed to get the NSTextView's height by using :

-(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;
}



回答2:


Here is some functional code that I have tested myself for swift. usedRectForTextContainer is not broken it just is set lazily. To set it i call glyphrangefortextcontainer

I found the answer here: 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)

The documentation also suggest that is the the case:



来源:https://stackoverflow.com/questions/19777904/auto-resizing-of-nstextview-and-or-nsscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!