I'm trying to rotate a UILabel by 45 degrees. I'm setting the transform property to CGAffineTransformMakeRotation(M_PI * 0.25) but when I do so the UILabel just disappears. If I change 0.25 to 0.26, I can see a glimpse of the UILabel (see below)
UILabel before rotation:
Code: self.myLabel.transform = CGAffineTransformMakeRotation(M_PI * 0.26);
If I missed any information that might be helpful, please let me know!
Update
It seems that the frame must be set before applying the rotation.
Your code should work fine, so something else is going on that you didn't post code for. I'd recommend checking the autoresizing mask as well as anywhere else that may be manipulating the transform. From the appearance and your description, it almost seems like it is being rotated along the wrong axis via a 3D rotation causing you to view the label side on where it has a zero width/thickness, like looking at the edge of a paper head on.
Another solution to the problem may be with embedding your UILabel within another view and using the outer view to position the label on your screen.
Another post had mentioned the problem that I'm seeing with distortion or disappearing of the text in a UILabel. Their solution was to set all the outer springs on the UILabel. This worked for me, but then my label wasn't positioned properly on screen, especially during device rotation. So I thought embedding my UILabel in a container view would let me use the container view to position my label with just the top and right spring set while still allowing proper rotation of the UILabel within with the CGAffineTransformMakeRotation.
Hope this helps out someone else who is also having this problem.
Related
I'm trying to create an image viewer that behaves exactly like the Apples Photo App. But for now I'm only interested in a single image behavior.
I've placed an UIImageView inside UIScrollView and managed to handle pretty much everything beside one thing, and that is if I zoom-in in a portion of image(for example a portrait image) and then rotate the device in landscape the portion appears to be rotated over the upper left corner instead of center.
Let me try to illustrate that. This is what I have now:
This is what I would want to happen:
Basically I believe I know how I could do it but it seems to involve a bit more coding than it probably deserves. Maybe I'm missing some simple property that I could just set to get this desired behavior.
Any hint, anyone?
self.imageView.center = self.scrollView.center;
do set [imgView setContentMode:UIViewContentModeScaleAspectFit];
I've a scroll view which has several UIViews that shows information to the user. This scrollview supports only horizontal scrolling and when user starts scrolling from left to right or vise versa, the center view should be bigger and the previous and next views should be smaller (as shown in the below view).
I'm able to do this by changing the frames of all the views that appear on the scrollviews. But the animation is not smooth. I just want to resize the views while scrolling according to their position. Is there anyway to do this? Any help greatly appreciated.
Well you could look at these links
UIScrollview make the current Image Larger
Or I can provide a better solution to you with iCarousel. Please refer to it and I think this might help you , and its pretty easy to integrate too :)
This might be what you are looking for CGAffineTransformScale. You need to set your UIViews transformation property, for example
CGAffineTransform transformRatio = CGAffineTransformScale(CGAffineTransformIdentity, factorX, factorY);
myView.transform = transformRatio;
factorX and factorY are the values by which to scale your views width and height.
When you view starts scrolling in and out you need to set the corresponding factors.
I've got a problem.
I've an UIImageView and i do some rotation to it with CGAffineTransformMakeRotation().
Not problem with that.
But when i do a rotation with my UIViewController (portrait to landscape and/or inverse).
My UIImageView is modified strangely.
Its frame become change from :CGRectMake(50.f, 50.f, 150.f, 150.f)
to a frame like :CGRectMake(50.f, 123.2f, 1004.56f, 1530.456f)
I realize that changes happens when I do UIViewController rotation when i've done a rotation on my UIImageView not equal to 0, 90, 180, or 270
I think it's because the UIViewController change the rect of the UIImageView but how inhibit
that comportement
Unfortunately my view move/change visually.
That's my problem and why I'm talking about that here...
I spoke about the frame property because I notice it changed strangely (and randomly) and even if I changed it after to my original frame. The view doesn't seem 'listen' to it.
I hope I am well.
Anyone for a solution ?
If you set a view's transform property to anything but the identity matrix (which you must have done in order to rotate it), the frame property becomes undefined and should be ignored. This is noted in the documentation. Is it just the frame property that goes wrong or is the view moving/changing visually in some unexpected way as well?
It's not clear from your description, but it sounds as if your UIImageView is a subview of the view owned by your UIViewController. Unless you have a reason to not manage it this way, I'd suggest setting the UIImageView struts and springs (i.e., its autoresizingMask) to surface the desired behavior and then allow the parent view to reposition/resize the UIImageView.
I have a UIView in which I need to draw text in drawRect:
- (void)drawRect:(CGRect)rect {
...
[#"some text" drawAtPoint:somePoint withFont:someFont];
...
}
Because the text requires special formatting, I cannot just use a UILabel.
It looks fine until I rotate the device. Then the size of my custom UIView changes (in the parent view's layoutSubviews method), and the text becomes stretched in one direction and squished in the other.
When I replace my view with a UILabel, the text always looks great, even when the bounds of the view changes.
How can I get my view to exhibit the same behavior as UILabel?
Some things I have looked into, but have yet to have success with:
Set the view's layer's needsDisplayOnBoundsChange to YES.
Set the view's contentStretch to CGRectZero.
Call setNeedsDisplay in my view's layoutSubviews.
Maybe I'm not doing one of these things right. Has anyone else run into this?
Update: As recommended in James Huddleston's answer, I set the contentMode property of the view to UIViewContentModeRedraw, which got me part of the way there. The text now appears correct at the conclusion of the animation. However, at the start of the animation the text gets squished/stretched to fit the end dimensions and then gets unsquished/unstretched over the course of the animation. This is not the case with UILabel.
Try setting the contentMode property of your view to UIViewContentModeRedraw.
This seems to work OK:
self.contentMode = UIViewContentModeRedraw;
self.contentStretch = CGRectMake(1, 1, 0.5, 0.5);
And then ensure that the bottom-right pixel is the background color. To do that, I put one pixel of padding around the contents of the view. It seems like UILabel doesn't have the one pixel border restriction, so it must be doing something different. But as far as I can tell, this has the same effect.
Use a UIWebView
Sounds a bit overkill but it seems the recommended way to get formatted text that's more complicated than a UILabel can cope with.
There is some source code created by Kevin Ballard called FontLabel. (code.google.com)
The good thing about this, it subclasses UILabel and you can use your own Font ;)
//EDIT: ok ok, as told I will update my answer to recommend subclassing UILabel "to get all the UILabel goodness" ^^
I've learned that the best way to get graceful rotation is to set the auto rotation mask on the view that you want resize or move. This works fine if you're using SDK views like UILabel, but if you have your own custom view that uses the drawRect method it doesn't rotate as gracefully. In fact the only thing that happens is that it stretches whatever you drew in drawRect.
I've tried redrawing both before and after the rotation, but it doesn't give me that smooth rotation.
I looked at a UITextField auto rotating (flexible width) in slow motion and it follows the edge perfectly during the rotation. That is what I want my view to do, so how do I do that? My views jump to the right position either before or after the rotation.
The following line will make your UIView stretch the middle pixel only. If this is not your desired behavior I suggest you read the documentation for contentStretch to learn how to manipulate the values of the CGRect.
[self setContentStretch:CGRectMake(0.5, 0.5, 0.0, 0.0)];
I would guess that the UITextField you're looking at has at least three subviews, one displaying the left cap of the field's border, one displaying the right cap, and one displaying the middle, with autoresizing masks of "flexible right margin", "flexible left margin", and "flexible width", respectively. If you set up your custom view something like that, and make sure its autoresizesSubviews property is set to YES, then you should get the same smooth resize that the text field does.