Rounded corners on NSTableView - objective-c

I have a custom view subclass similar to NSBox that draws a rounded box background. The problem is that if I place a view like an NSTableView in the box view, it does not clip to the rounded corners. Is there any way to round the corners of NSTableView and its parent scroll view?

I haven't tried this with a table view but have with other controls.
In a subclass of NSTableView (or whatever view/control you want to clip)
Override drawRect:
Create an NSBezierPath with the shape you want (probably appendBezierPathWithRoundedRect:xRadius:yRadius: just remember to use the view's bounds as the size)
Send the path the addClip message to add that shape to the view's clipping path
Call super's drawRect:
If the table view has a header you may need to clip the top corners by subclassing NSTableHeaderView. And if you have scrollbars you may have to do the same thing to them except only clip certain corners. Hopefully you don't have scrollbars because I doubt that would look right. Basically you want to clip the view/control that draws that part, clipping the parent will not cause subviews to be clipped.
If you look at Apple's Welcome to Xcode window they get away with it by drawing a custom header at the top and a text block at the bottom so they don't have to round the table view itself. If you can do something like that I would.

Related

OS X chat app with resizable bubbles

I'm building chat application for Mac OS, similar to iMessage. I wonder, how can I implement resizable text views in bubbles. I mean, when I resize chat window bubbles with text will resize to. Any ideas, links will be very useful. Thank you for help)
For text resizing, you use auto layout. If you have an NSScrollView containing MYBubbleViews containing NSTextViews, you can add NSLayoutConstraints using the leftAnchor and rightAnchor properties of the scroll view's content view and those of the bubble view, and add constraints between all edges of the text view and bubble view. Then pin the bubble views to the top/prev view.
Also make sure you set the NSTextView to wrap. The width of the intrinsic size of the text view will be set so that it fills the width and the intrinsic height will be set to fit the whole text.
I previously thought it was about drawing the bubbles, so I first gave this answer:
If you look at Messages.app, you'll see that they're not circular bubbles. They are basically composed of several shapes overlaid on each other. A rectangle with rounded corners, plus a bezier path of the tip.
So you should be able to take a NSTextView for the text, make it a subview of a custom view that draws a rounded rectangle and the tip in its drawRect method, and then use auto layout constraints to make your bubble view resize with the text view and the text view to the window width.
You could probably also have the bubble view host a CALayer with fill and rounded corners, plus one with an image for the tip (or aCAShapeLayer for the tip), but drawRect is the easier approach.

Adding a UIView as a subview to UIImageView

I am working on creating a color picker for "iOS". I am using this project, since it does what i want: "This"
I want to create a moveable circle (UIView) on top of the palette (UIImageView).
What I want to do is, while users move the circle, take the touch point and call the method getPixelColorAtLocation(); and change the background color of the circle to the color on current point. (Seen on most of the color palette/wheels)
The method getPixelColorAtLocation() is available on a child view. I created a circle with UIView on parent view, the problem is I cant call to getPixelColorAtLocation() from parent view.
My question is, Is there anyway to add a UIView as a subView to UIImageView. If I cant, what choices do I have to achieve what I want?
Yes you can do this.
[myImageView addSubview:sampleView];
An image view is just a subclass of UIView, so you can add subviews to it however you'd like. If there's a reason why you can't do so, then you can always make it a subview of the image view's superview, move it to the front, and then use convertPoint:toView: to convert to the image view's coordinate system, then get the pixel color.

Manually scrolling a UIView contentOffset

I'm trying to scroll the contents of a UIView using the contentOffset which so far I achieve handling touchesBegan: and touchesMoves: methods of the view, however I get a jumpy effect as I'm changing the bounds of the UIView manually (for example: current position +/- the change of the last position of the finger and the new position)
Is there any easy way to achieve this without manually changing the bounds of the UIView?
IMPORTANT NOTES: I'm not using a UIScrollView because the mentioned UIView has plenty of draggable subviews so if I use a UIScrollView I can't drag because the UIViewScroll scrolling event executes over the subviews events.
Animating the layer of the mentioned UIView causes the contents to move out of control, like if the point of reference had changed somehow.
However I'm always open to suggestions.
I would rather make a UIView container clipping subviews, add the content view which we will drag as a subview and change it's frame. I think it shouldn't have jumpy effect as it must be the same as animating the view frame change.

Resizing CAShapeLayer

I have a simple test app on which my rootViewController's UIView contains a bunch of UIView subviews. Each one of those UIView subview is backed by a CAShapeLayer.
I want the composition created by those subviews ( the four shapes that are within the dotted area .. ) to always stay vertically and horizontally centered with respect to my
UIWindow. (the minimium size of the left/right, top/bottom margins will be subject to be changed at runtime at each orientation change )
So for example when i rotate to portrait i will have to
resize and reposition those single shapes so that the whole figure will be mantained centered and each CAShapeLayer sublayer stays sharp ( i want their path to be resized not just a raster resize )
what would be the best technique to resize/move the shapes to always have a centered composition while maintaining path crisp appearance for the shapes?
Ultimately for me it will be good to have an answer to this: how can i shrink the subviews as a whole? i mean their sizes and relative positions?
Thanks
You can use CGPathCreateMutableCopyByTransformingPath() or -[UIBezierPath applyTransform:] to recalculate all points in the path.

Howto draw a rect on screen. NSOpenGLContext vs transparent NSWindow + custom NSView

I'm reading pixels from an area of the main screen via NSOpenGLContext. Now I would like to draw a rect around that area to indicate where it actually is.
How would I do this?
My first thought was the "Cocoa way": create a transparent fullscreen NSWindow and a custom NSView to draw the rectangle path. But that feels a bit too complicated. Isn't it possible to draw directly on the NSOpenGLContext?
If you want to draw over elements not inside your application the floating window is the only correct way. There’s really no complication except mapping positions properly, which is easy to do with the coordinate-space conversions available on NSView and NSWindow.