Objective C: How to create border effect around photos - objective-c

Can anyone advise me on how I can create a border around photos (like what you see in the screen shot below)? It seems like there is a thin grey border and a drop shadow at the back.
Appreciate any advise here.

Simple way: wrap the photo into a UIImageView, setup the borderColor, cornerRadius, shadowColor, shadowOffset, shadowPath etc. for the layer of the UIImageView.
Complex way: subclass the UIView and draw the border and shadow by yourself in -drawRect:.

Related

how to draw UIButton with tint color and rounder from single side only

I wanna draw a UIButton filled with a red tint color or some color else presented by rgb and only the upper edges are rounded.
some people answer me to get an image to do that but every time i want to change it i have to bring new image and replace it, I wanna ask can i do it from code without get an image.
There is no direct way. Only way to achieve this to create a segment control and keep only a single segment in that.
See this post for detailed implementation,
Update:
If you want to round only two corners check Round two corners in UIView or Just two rounded corners? as mentioned in rdurand's comment. Since segment control is a subview of UIView, this should work.
You can draw the rounded corners using NSBezierPath s bezierPathWithRoundedRect:xRadius:yRadius: method. And then draw over it with another bezier path. Or you could just define a custom bezier path using moveToPoint: lineToPoint: and curveToPoint:controlPoint1:controlPoint2:
Take a look at this:
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSBezierPath_Class/Reference/Reference.html

Interface Builder UIButton with Image Rounded Corners Gone

Using Interface Builder I have a UIView with a number of UIButtons as subviews. In IB all of the button types are set to Rounded Rect and each button uses an image from the image drop down box. The images are square and the size of the rounded rect UIButton is the same as image. The images are png's and they are from b&w photo's.
The issue is the UIButton rounded corners are gone and everything is square in IB and when running under the simulator.(no hardware yet) I was hoping the button drawRect would crop the image and preserve the rounded rect look. Is there a way to do this? in IB?
Appreciate the help.
Make sure that Clip Subviews are checked in IB. It's in the Attributes Inspector under the View section.

Rounded corners on NSTableView

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.

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.

Setting the background of NSBox to a gradient programmatically without subclassing

I want to set the background of an NSBox to be a gradient. In Interface Builder it is possible to set the background color of an NSBox to selectedMenuColor which is a gradient.
NSBox only has a setFillColor method so how is Interface Builder filling it with a gradient?
How do I programmatically fill an NSBox without subclassing it? It would be trivial to subclass NSBox but the workings of Interface Builder suggest there may be better solution.
selectedMenuColor is a "magic" color that is not displayed as a solid color. Many of these "magic" colors exist in the system.
I have used colorWithPatternImage: for this before. But note that the image you use as the pattern will get tiled, so you will probably have to resize the image to the size of the box.
Probably the closest you could come would be to use an NSColor created with colorWithPatternImage:, then create the gradient you want as an image and load that in. Ugly, but should work. I think subclassing is your best bet.
The selectedMenuColor color is actually a pre-rendered image of a gradient, and not a gradient drawn on the fly, so there is not any way to specify an arbitrary gradient as a background color. Like Ben said, subclassing is probably the way to go.
In xib, select NSBox, then goto effect inspector, check NSBox for Core Animation Layer.
Now
IBOutlet NSBox *box;
[box.setWantsLayer:YES];
[box.layer setBackgroundColor:[[NSColor whiteColor] CGColor]];
or
[box.setWantsLayer:YES];
[box.layer setBackgroundColor:[[NSColor colorWithPatternImage:[NSImage imageNamed:#"white.gif"]] CGColor]];