views:

158

answers:

2

Hi, I want to implement dialog borders that scale to the size I require the dialog to be. Perhaps there is a better more conventional name for this sort of thing. If there is, if someone would edit the title, that'd be great.

Anyhow, I'd like to do this so I can have dialogs of any size without the visual artifacts that come with scaling border art to small, large, or wacky unproportional dimentions. I have a few ideas on how this is done, but am not sure which is better for iphone. I have a few questions.

1) Should I make a containing view object that basically overloads its drawRect method and draws the images where they should be at their appropriate scale when the method is called, or should I main a containing view object that simply contains 8 UIImageViews? I suspect the latter approach won't work if I need to actively scale the resulting dialog class like in an animation.

1b) If overloading drawRect is the way to go, does someone have some sample code or a link to an example that demonstrates drawing an image directly from drawRect()?

2) Is it generally better to create

a) a 3 x 3 image where the segments are in their appropriate 1x1 grid of the image? If so, is it simple to draw from a portion of this image onto my target view in drawRect (if the former assumption is correct that I should use drawRect)?

b) The pieces separately in 8 different files?

UPDATE: To clarify, the idea is to take any customized border art and be able to stretch the 2nd, 4th, 6th, and 8th cell (in a 3x3-cell grid) to form a border of any size with just those assets. Stretching just a plain image would result in distortion of the corners, so I'd like to stretch those even numbered cells as needed and tack on the corners so there is no distortion. I'd seen this done before so thought it might be a standard thing and have a standard naming to it other than what I called it.

Anyhow, I was advised that adding 8 UIImageViews to a container would not be as efficient as drawing the UIImages on the fly in drawRect so took that approach using CGContextDrawImage() after applying the necessary transformations to the context to translate and scale the Y. Because this function draws from the bottom left corner of an image but onto a top-left origined UIView, the image is upside down without the Y axis invert. I noticed the suggestion to use UIImage functions like drawAtPoint works as well and similarly but for the invert since UIImage draws in the same orientation as UIViews. I will continue my implementation with the former and see how it goes, but one other question.

Would someone happen to know which of these approaches is more efficeint, faster, etc?

+2  A: 

I'm not sure I follow, but here's my best shot at an answer...

Using drawRect: or adding individual UIImageViews to a parent view is entirely up to you. UIImageView gives you a bit of encapsulated functionality for free, but otherwise they are the same as far as appearances go.

If you do want to go the drawRect route, you just need to use UIImage's drawAtPoint: method. Do the math for where you want it to be, and draw it. You can calculate your points based on the parent view's dimensions.

As far as scaling, it's impossible to resize these images without scaling them, so I'd plan ahead and make your originals as large or larger than you ever expect to display them.

Hope that helps a little?

Cheers

DougW
Thanks, this does help. I will go into more detail by updating the main question.
Joey
No problem. You may also want to consider creating a single image that can stretch in both dimensions. This is what I do for convo bubbles in my application. That way, all you have to do is make your view a UIImageView, create a stretchable image with stretchableImageWithLeftCapWidth:topCapHeight: and set it as the image property. Then you can resize away, and use it like any other UIView.
DougW
Wow, that was a great suggestion and exactly what I was looking for. I hadn't realized designating caps for stretching UIImages was already provided. Thanks!
Joey
No problem. The question was a little confusing, but I guess if you'd known how to word it, you would have already known the answer :)
DougW
+1  A: 

If you want a border on a dialog box, assuming the box is a UIView (or subclass), then set the layer's border properties and let the system draw the border for you.

#import <QuartzCore/QuartzCore.h> 
// ... 

view.layer.borderWidth = 2;
view.layer.borderColor = [UIColor whiteColor].CGColor; 
view.layer.cornerRadius = 0;  // 0=square corners, >0 for rounded
progrmr
Ah, yeah I get what he's asking now. Yeah this is what you want. I got thrown off by the "8 UIImageView" stuff.
DougW
I'm not sure this is what he wants, he might be talking about speech balloons like in comics: http://en.wikipedia.org/wiki/Speech_balloon
progrmr
yes what I meant was something that could essentially be used for speech balloons, not a default border property. I'll update the main question to clarify.
Joey
Ah okay. Well if anyone reading this wants solid, normal borders, this is definitely the way to go.
DougW