nacho4d
nacho4d

Reputation: 45118

NSScrollView with unclipped content view?

Is there a way I can set my scrollview not to clip its contents? (Which is a NSTextView)

I have a subclass of NSScrollView and want its content not to be clipped to its bounds. I have tried overriding:

- (BOOL) wantsDefaultClipping{
    return NO;
}

in MyScrollView and in MytextView without any effect. In the iOS I would simply would do: myuitextView.clipsToBounds=NO; how can I do this in Cocoa?

EDIT

This is an example of what I want to achieve but in the mac The scrollview is white, the scroller will never go outside its bounds but the text does since I did myuitextView.clipsToBounds=NO

See picture here

EDIT2

I wouldn't mind clip my view like @Josh suggested. But the real behaviour I would like to have can be explained with this picture:

normal NSScrollView with NSTextView

Do you see the word *****EDIT***** that has being cut in the very first line? I want the text not to be cut this way, rather I want it to completely appear and I will put a semitransparent image so it looks like it fades off when it's outside the frame.

Q: Why don't I simply put a semitransparent NSImageView on it so it looks like what I want? A: Because 1.Scroller will be faded as well. Even if I correctly place the semitransparent NSImageView so the scroller looks fine, the cursor/caret will be able to go underneath the semitransparent NSImageView again it does not look good.

I would like to be able to control the area is clipped by NSClipView. I think that would solve my problem. Is there any alternative I have? maybe I can control the caret position or scrolling position through NSTextView so caret will never go near the top/bottom frame limits? or any work-around?

Any advice is appreciated.

Upvotes: 3

Views: 2669

Answers (4)

definitelyokay
definitelyokay

Reputation: 387

Now that it's 2016 and we're using vibrant titlebars with full size content views, I'll add my thoughts to how someone might accomplish this. Hopefully, this will help anyone who came here looking for help on this, as it helped me.

This answers the question in regards to scrolling under the titlebar, but you could easily modify this technique to scroll under other things using the insets and caret position.

To get a scroll view (with or without an NSTextView inside of it) to scroll behind a titlebar, you can use:

    // For transparent title.
    window.titlebarAppearsTransparent = true
    window.styleMask = window.styleMask | NSFullSizeContentViewWindowMask
    window.appearance = NSAppearance(named: NSAppearanceNameVibrantLight)

This effectively overlays the titlebar of the NSWindow onto the window's contentView.

To constrain something to the top of the window without knowing the height of the titlebar:

// Make a constraint for SOMEVIEW to the top layout guide of the window:
let topEdgeConstraint = NSLayoutConstraint(
item: SOMEVIEW, attribute: NSLayoutAttribute.Top,
relatedBy: NSLayoutRelation.Equal,
toItem: window.contentLayoutGuide,
attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0)

// Turn the constraint on automatically:    
topEdgeConstraint.active = true

This allows you to constrain the top of an element to the bottom of the titlebar (and or toolbar + any accessory views it may have). This was shown at WWDC in 2015: https://developer.apple.com/videos/play/wwdc2014/220/

To get the scrollview to scroll under the titlebar but show its scrollbars inside the unobscured part of the window, pin it to the top of the content view in IB or via code, which will cause it to be under the titlebar. Then, tell it to automatically update it's insets:

scrollView.automaticallyAdjustsContentInsets = true

Finally, you can subclass your window and handle the cursor/caret position. There is a presumed bug (or developer error on my part) that doesn't make the scrollview always scroll to the cursor/caret when it goes above or below the content insets of the scrollview.

To fix this, you must manually find the caret position and scroll to see it when the selection changes. Forgive my awful code, but it seems to get the job done. This code belongs in an NSWindow subclass, so self is referring to the window.

// MARK: NSTextViewDelegate
func textViewDidChangeSelection(notification: NSNotification) {
    scrollIfCaretIsObscured()
    textView.needsDisplay = true // Prevents a selection rendering glitch from sticking around
}

// MARK: My Scrolling Functions

func scrollIfCaretIsObscured() {
    let rect = caretRectInWindow()
    let y: CGFloat = caretYPositionInWindow() - rect.height
    // Todo: Make this consider the text view's ruler height, if present:
    let tbHeight: CGFloat
    if textView.rulerVisible {
        // Ruler is shown:
        tbHeight = (try! titlebarHeight()) + textViewRulerHeight
    } else {
        // Ruler is hidden
        tbHeight = try! titlebarHeight()
    }
    if y <= tbHeight {
        scrollToCursor()
    }
}

func caretYPositionInWindow() -> CGFloat {
    let caretRectInWin: NSRect = caretRectInWindow()
    let caretYPosInWin: CGFloat = self.contentView!.frame.height - caretRectInWin.origin.y
    return caretYPosInWin
}

func caretRectInWindow() -> CGRect {
    // My own version of something based off of an old, outdated
    // answer on stack overflow.
    // Credit: http://stackoverflow.com/questions/6948914/nspopover-below-caret-in-nstextview
    let caretRect: NSRect = textView.firstRectForCharacterRange(textView.selectedRange(), actualRange: nil)
    let caretRectInWin: NSRect = self.convertRectFromScreen(caretRect)
    return caretRectInWin
}

/// Scrolls to the current caret position inside the text view.
/// - Parameter textView: The specified text view to work with.
func scrollToCursor() {
    let caretRectInScreenCoords = textView.firstRectForCharacterRange(textView.selectedRange(), actualRange: nil)
    let caretRectInWindowCoords = self.convertRectFromScreen(caretRectInScreenCoords)
    let caretRectInTextView = textView.convertRect(caretRectInWindowCoords, fromView: nil)
    textView.scrollRectToVisible(caretRectInTextView)
}

enum WindowErrors: ErrorType {
    case CannotFindTitlebarHeight
}

/// Calculates the combined height of the titlebar and toolbar.
/// Don't try this at home.
func titlebarHeight() throws -> CGFloat {
    // Try the official way first:
    if self.titlebarAccessoryViewControllers.count > 0 {
        let textViewInspectorBar = self.titlebarAccessoryViewControllers[0].view
        if let titlebarAccessoryClipView = textViewInspectorBar.superview {
            if let view = titlebarAccessoryClipView.superview {
                if let titleBarView = view.superview {
                    let titleBarHeight: CGFloat = titleBarView.frame.height
                    return titleBarHeight
                }
            }
        }
    }
    throw WindowErrors.CannotFindTitlebarHeight
}

Hope this helps!

Upvotes: 4

jscs
jscs

Reputation: 64002

You might consider using a translucent layer to achieve this appearance, without actually drawing outside your view. I'm not certain of the rules on iOS, but on the Mac, a view drawing outside its bounds can cause interference with surrounding drawing.

However, you can set the clipping region to be whatever you like inside your scroll view subclass's drawRect: using -[NSBezierPath setClip:]:

- (void)drawRect:(NSRect)dirtyRect {

    [NSGraphicsContext saveGraphicsState];
    [[NSBezierPath bezierPathWithRect:[[self documentView] frame]] setClip];

    //...

    [NSGraphicsContext restoreGraphicsState];
}

It might be possible (since you asked) to use this code in an NSClipView subclass, but there's not much info about that, and I think you may have a hard time making it interact properly with its scroll view. If it were me, I'd try subclassing NSScrollView first.

Upvotes: 0

Jack Nutting
Jack Nutting

Reputation: 606

This is a little hairy. AFAIK, NSViews can't draw outside their own frame. At any rate I've never seen it done, and I was somewhat surprised when I realized that UIView allows it by default. But what you probably want to do here is not manipulate clipping rectangles (doing any such thing inside NSScrollView will probably not do what you want or expect), but instead try to cover up the vertically-truncated text lines with either layers or views that are the same color as the background. Perhaps you could subclass NSClipView and override viewBoundsChanged: and/or viewFrameChanged: in order to notice when the text view is being shifted, and adjust your "shades" accordingly.

Upvotes: 0

user187676
user187676

Reputation:

I would simply try to observe the document view's frame and match the scroll view's frame when the document resizes.

Upvotes: 0

Related Questions