Assigning a Background image to CGRect Object! - objective-c

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.

Related

Completely transparent UIButton with irregular hit area

I know how to use a background image with alpha channel to create a UIButton with an irregular tap area. But with this solution, only the ignore-tap area is transparent; the tap area consists of the opaque.
What I want is a totally transparent UIButton, with an irregular tap area. (It triggers an animation behind the button.)
It seems that some sort of extra UILayer with some hit-testing could work, but I don't quite see how. Suggestions welcome.
Here's my solution. The problem: A UIImageView (call it the base view) to which I want to attach an irregularly-shaped tap area. The area corresponds to something in the image, let's call it a river. It could be a non-connected region.
In the Finder, duplicate the view's image, and make all the non-river pixels transparent, using your favorite graphics app. Call this the mask image.
When the base view is tapped, check to see if the corresponding pixel in the mask image is or is not transparent. (Solution left to reader; examples abound.)
So the mask image is never actually rendered, it is just used as a reference.
Note that if the base view gets resized or moved around (as it does in my app, via animating its constraints) then you have to "move" the mask image too, or in some other way manage the coordinate translation between view and mask. The easiest way to do this is to make the mask image a hidden subview of the base view, with its left, top, leading and trailing anchors constrained to be equal to those of the base view. Then they move around together, and the coord translation between the two is the identity.
I wrapped all this up in a subclass of UIImageView so that it manages its own mask, and calls a delegate if the "river" (or whatever) is tapped.

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

How to change background image of the UIButton in Interface Builder and preserve its shape?

I'm in the process of writing my first Hello World application for iOS, and I don't know even elementary things, so I'm sorry if this question will be stupid. I want to change the background image of button. But when I do this button changes its shape and becomes squared instead of Rect Square (with non-zero corner radius). So the question are there some ways to change background and preserve button's shape either directly by Interface Builder or by manipulating XML-like language in .xib files or whatever other way.
Add a button and set its type property to custom. With this method you could define an image for the button as you would like it to be displayed not just the background within the rounded rect.

Programmatically resizing and Scrolling NSView after application launch

I want to create a simple application which performs some calculations and then draws some images on view. I use NSBezierPath. Then I must resize the view and allow people scroll the finished picture. But i don't know how.If I also try to draw an image on an invisible part of canvas then it becomes invisible or isn't drawn (I couldn't know the future canvas size).
Check out the Apple sample code called BezierPathLab. I think that will get you started. There's lot of other sample code for Quartz 2D drawing too.
Being able to scroll and resize the view should be as simple as putting the view that you will be using to draw inside an NSScrollView.

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.