UIButton border along rounded rect - objective-c

I have a button and its type is UIButtonTypeRoundedRect, but its border does not go along the rounded corner. How can I curve the border.

Check out this blog post I recently wrote: UI Polish in An Instant
You're on the right track accessing the button's layer property, but there is more you need to do (for example, to get rounded corners, add deleteButton1.layer.cornerRadius = 10), and more you can do, all without extra images.

Images are the recommended method for creating custom buttons. Apple's built-in buttons are basically only there for prototyping.
You could also create a subclass of UIButton and then override the methods for drawInRect and provide custom drawing code, probably using CoreGraphics. However, it is still much cleaner in code and more efficient at runtime to just use images.

Related

How to set an image to a NSButton to be resized along with the button?

I have an image (3width x 15height, the first and last pixels are rounded on the corners), and I want to create a button (the height of the button is always the same, 15px, but the width changes) that uses this image no matter what size it is, kinda like UIEdgeInsets on iOS.
I've tried to use [[button cell] setImageScaling:NSImageScaleAxesIndependently]; but the image gets distorted and loses quality.
How can I do this on OSX? Thanks!
You could probably create a custom subclass of NSButtonCell and use the Application Kit's NSDrawThreePartImage() function in the cell's drawInteriorWithFrame:inView: method.
For more info on how to use NSDrawThreePartImage(), see Cocoa Drawing Guide: Drawing Resizable Textures Using Images.
If it's possible to set this in graphical interface > Attribute Inspection > Button section Background

Too many UIViews causes lag

I am creating an app for practice that is a simple drawing app. The user drags his/her finger along the screen and it colors in a 100px x 100px square.
I currently achieve this by creating a new colored UIView where the user taps, and that is working. But, after a little time coloring in, there is substantial lag, which I believe is down to there being too many UIViews as a subview of the main view.
How can I, and others who similarly create UIViews on dragging a finger reduce the lag to none at all, no matter how many UIViews there are. I also think that perhaps this is an impossible task, so how else can someone like me color a cube of the size stated above in the main view on a finger dragged along the screen?
I know that this may seem like a specific question, but I believe that it could help others understand how to reduce lag if there are a very large amount of UIViews where a less performance reducing option is available.
One approach is to draw each square into an image and display that image, rather than keeping around an UIView for each square.
If your drawing is simple enough, though, you can use OpenGL to do this, which is much faster. You should look at Apple's GL Paint Sample Code which shows how to do this in OpenGL.
If your drawing is too complex for OpenGL, you could create, for example, a CGBitmapContext, and draw each square into that context when the user drags their finger. Whenever you draw a new square into that bitmap, you can turn the bitmap into an image (via CGBitmapConxtextCreateImage) and display that image an a UIImageView.
There are two things that come to my mind:
1- Use Instruments tool to check if you are leaking any memory
2- If you are just coloring the views than instead of creating images for each of them, either set the background color property of UIView or override the drawRect method to do custom drawing
I think what you are looking for is the drawRect: method of UIView. You could create your custom UIView (you propably have that already) and override the drawRect method and do your drawing there! You will have to save your drawings in an array or another container and call the setNeedsDisplay method whenever the array content is changed.

What is this Toolbar-Style Cocoa UI Element?

I've seen this toolbar-like UI element in a couple of apps, and I'd like to know if there's a standard Cocoa object for such an element.
Xcode:
ColorSchemer:
CornerStone:
If a standard Cocoa implementation does not exist, can anyone suggest a good open-source library?
In the second example, the buttons are what Interface Builder calls "Gradient Buttons" which have that appearance without needing a background image. The buttons are just butted up next to one another.
In the other examples, the "toolbar" is just an ordinary NSView with a custom image background, with standard NSButton objects as subviews. The buttons have their border disabled so all you see is the icon.
To get the "inset" look for the icons, the button images are most likely template images. To make an image a template image, it must consist of black and clear colors only, with an alpha channel. You then call [image setTemplate:YES] on the NSImage object.
Apple supplies some pre-canned template images which you can access by using the +imageNamed: method of NSImage.
In the case of Xcode, it's a private class called IDENavigatorFilterControlBar, with this inheritance hierarchy:
IDENavigatorFilterControlBar
IDEFilterControlBar
DVTBorderedView
DVTAutoLayoutView
NSView
DVT is the prefix used by the private Developer Tools framework.
Not sure if this is what you want, but have you tried Matt Gemmell's MGScopeBar?

Interesting Buttons

Is there a way, without having to create images of my own, to make rounded rectangle buttons look more appealing? For example, providing them with shading, or making them look three dimensional.
Have a look at this article I wrote recently: UI Polish In An Instant
It talks about using layer properties to add visual interest to your UI with relative ease.
If you want to do so, then consider creating a UIView with all your customizations enabling it for user interaction. It might help!
Using Core Animation functions you can make them a bit more exciting. First import the QuartzCore library. Then you can access properties of the view.layer.
button.layer.cornerRadius changes the rounded corners
button.layer.strokeWidth changes the border thickness
button.layer.strokeColor changes the border colour
button.layer.shadowColor use this and other shadow properties to add a nice soft drop shadow
There's a whole bunch of properties you can tweak if you check the CALayer documentation, and these work for any UIView or subclass, not just buttons.
To add a gradient, check out the CAGradientLayer. You can create one of these and append it as a sublayer to your button.layer.
There's also CAShapeLayer to add an arbitrary polytonal shape to your button, all drawn using code without any images.

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.