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

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

Related

NSImageView won't scale image down if frame style is NSImageFrameNone

I need to display NSImageView on resizable NSWindow. The image displayed in the view should scale down if it's too big to fit image view in the widnow and it should also change its size when resizing the window. I am able to achieve correct behavior using Auto-Layout, and setting imageScaling property of NSImageView to NSImageScaleProportionallyDown. Unfortunately it's not working when my image view's imageFrameStyle property is set to NSImageFrameNone (it works for any other option like NSImageFramePhoto or NSImageFrameGrayBezel). I don't want any frame to be displayed, like with NSImageFrameNone, but disabling the frame breaks autoresizing - it looks like with frame set to NSImageFrameNone image is not scaled down and NSImageView is scaled up to match displayed image size. Anyone have solution for that issue?
It occurs that it's easier than I thought. The problem was with Auto-Layout, not with NSImageView scaling logic. Using InterfaceBuilder with my image view selected I had to change "Content Compression Resistance Priority" to lower value (it's located on Size Inspector tab in Xcode5). That solved my problem and the image is now scaled properly when resizing window.

UIButton stretching image when selected

I have a rectangle UIButton which I set the content mode to UIViewContentModeScaleAspectFit because the image is not always the same size of the button.
[btnPlaylist.imageView setContentMode:UIViewContentModeScaleAspectFit];
The thing is that when I hit the button, the image stretches like it was UIViewContentModeScaleAspectFill, ignoring that it already fits vertical or horizontal size.
What am I missing?
Did you try turning off adjustsImageWhenHighlighted? Perhaps if this property is NO it won't mess with your image.
adjustsImageWhenHighlighted A Boolean value that determines whether
the image changes when the button is highlighted.
#property(nonatomic) BOOL adjustsImageWhenHighlighted
Discussion
If YES, the image is drawn lighter when the button is highlighted. The
default value is YES.
Availability Available in iOS 2.0 and later.

UIButton border along rounded rect

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.

How to change the size of NSImageView control from within program?

The following line of code displays an image on my application window, within an NSImageView control named outputImageView. I used Interface Builder to put the NSImageView control onto the window, initially (then bound it to outputImageView). How do I change the size of the NSImageView control within my program, so that it matches the size of the actual image I am displaying (which may change, periodically)? And what if I wanted to change the anchor point for the image (i.e., centered, left edge, bottom right, or whatever? Thanks in advance!
[outputImageView setImage: outputImage];
Like any other NSView, use -setFrame::
[outputImageView setFrame:NSMakeRect(x, y, w, h)]
The coordinates are specified in the system of the containing view.
If you need to change only the size (leaving the origin alone), you can use -setFrameSize::
[outputImageView setFrameSize:NSMakeSize(w, h)]
For alignment, use NSImageView's -setImageAlignment:, as jshier wrote.
I don't know a way to change an NSImageView's size programattically, but you can use - setImageAlignment: to determine where the image should be positioned in the frame. Also, you may want to look at using IKImageView if you want a more flexible image view.

Assigning a Background image to CGRect Object!

I am new to iphone Programming . I have a rectangle on which i display some data. I want to assign an image to its background. And is it possible that the data would be visible even after the image is assigned . Any idea how i can do it ?
Thanks
You don't say how you are displaying your data, so it isn't possible to respond properly.
But in general, you create simple displays using Interface Builder, and can position a UILabel over a UIImageView, and can set the labels background to be transparent.
If you are drawing using drawRect:, then you probably shouldn't be. And yes, you can draw text over an image doing it this way, too.
CGRect is just a structure representing an abstract rectangle. You do not associate styling information (e.g. color, background image, etc.) to it because this does not make sense.
If you simply want to display an image on a window, drag the PNG image into your project, then you can find your image in Interface Builder, which can be put onto the window.