sneakyimp wrote:
I'm wondering a couple of things:
1) Is there some function I can define/override/augment in my UIViewController which could take advantage of this code when the user changes their orientation? I'm guessing I'll have to move/redraw my view when the user rotates their iPhone.
Well I'm not sure what kind of advantage you're thinking about. When I tested that code, I pinned the label to the upper left corner (left and upper struts on in IB), and turned both springs off. That resulted in a label with exactly the same dimensions and relative position in both portrait and landscape regardless of the text or font.
The best (and easiest) rotatable UI is made by adjusting the struts and springs in the IB Size Inspector. It takes a little practice. My graphic ability is quite limited, so the effect of these settings was never obvious to me. But I just use the orientation switch (the upper-right-corner curved arrow in the IB editor window) to see what's going to happen. Anyway the user experience is usually far superior if you can avoid using code to change the layout when the orientation changes.
Btw, if you meant you'd like to use the label to detect the orientation, you should use UIDevice.orientation for that purpose.
2) Is there some way to sniff out the width/height currently available to a given UIView?
For any descendant of UIView, just look at view.bounds.size.width and height (The compiler doesn't seem to like four dots, so break that up into two lines as show in the code). You can normally use the frame instead of the bounds, but never use view.frame.origin if the view has been rotated.
I see you've defined MAX
LABELWIDTH as a constant...I'd prefer to grab it live as my code runs -- unless you think that's a bad idea.
There's no reason it needs to be a constant. I only meant to show where you would limit the width.
Btw, I never asked why you cared about the width of the label, since labels almost always have a transparent background. I generally make the label as wide as possible and limit the width of the text as needed (using the NSString measurement methods at the link I posted). I assume you'd only need to limit the label itself if the background were opaque. None of my business, of course. You asked for an expanding label and now you have one 🙂