Rotating UILabel is clipping text - objective-c

I have a UILabel that I am trying to rotate. First I am calling sizeToFit on the label, adding a little padding to the frame and then setting the transform to CGAffineTransformMakeRotation(rotation) (where rotation is a value calculated from a UIRotationGestureRecognizer).
Some of the text is being clipped as the UILabel is rotated (as illustrated in the image linked below). For demonstrative purposes, I made the UILabel's background color red so you can see how the bounds change when it is rotated. How do I prevent this from happening?
http://cl.ly/image/3F0M420R0d14
Thanks.

Related

UIButton inside UIView goes out of bounds

I'm struggling with a problem that probably it's really stupid.
I've created an UIView and inside the UIView I've placed an UIButton and a UISwitch.
The UIView scales it's width based on the screen size keeping from left and right 45px, the UISwitch keeps it's frame size and the UIButton has its frame size changed based on the UIView frame size.
The problem is that the UIButton's text goes out of bounds for no reasons, this is the result I get:
The Blue background is the one of the UIButton and the Yellow background is the one of the UIView
I'm not able to align the text of the UIButton on top, this is what I've tried:
[_privacyButton setTitleEdgeInsets:UIEdgeInsetsMake(0,0,0,0)];
_privacyButton.titleLabel.baselineAdjustment = UIBaselineAdjustmentNone;
And also from the Storyboard I've set the vertical aligment top
Please someone help me, this UIButton is driving me crazy
You need to set constrains for the particular unbutton, i am unable to see you uiswitch, where you place it?.

CATextLayer subpixel antialiasing

My app draws layer-backed labels over a NSImageView.
The image view displays an image, and a tint color over that image.
This ensures that the contrast between the labels and the background image works.
As you can see, subpixel antialiasing is enabled and works correctly.
When you hover over those labels, they animate the frame property (Actually the view containing them).
While animating, the subpixel antialiasing is disabled, and when done enabled again.
This looks incredibly weird.
The layer is never redrawn, and the subpixel antialiasing doesn't have to change.
So I don't see a good reason why it shouldn't be displayed when animating.
I've tried everything I can think of.
Making the NSTextField opaque
Making the CATextLayer opaque
Giving the NSTextField a background-color
Giving the CATextLayer a background-color
Always the same result.
Disabling subpixel antialiasing for the labels is not an option, since it's not well readable on non-retina devices.
EDIT
I forgot that the layer is replaced with a presentationLayer while animating.
This layer probably does not support subpixel antialiasing, which is why it's disabled.
Now the question is if I can replace this presentationLayer with a CATextLayer.
What I also noticed is that setting shouldRasterize to YES enabled subpixel antialiasing also for animation, but only against the background color. So no background-color will bring no subpixel antialiasing.
Is there any way that you can post a piece of sample code? I quickly mocked up an NSWindow, added an NSImageView, added a background-less NSTextField with setWantsLayer: set to YES. In my applicationDidFinishLaunching: I set a new rect on the NSTextField's Animator frame, but I didn't see any pixelation.
The problem is with positioning of the text layer. Let's presume you use left alignment. The text will look good if x and y coordinates of the layer's frame origin are rounded numbers. For example:
CGFloat x = 10.6;
CGFloat y = 10.3;
textLayer.frame = CGRectMake(x, y, width, height); // the text will be blur.
textLayer.frame = CGRectMake(round(x), round(y), width, height); // the text will not be blur.
So, the first problem may be that the coordinates you assign to the layer's frame are not rounded.
The tricky part is that the edge of the layer may still be not aligned with pixels even if you think you passed rounded coordinates. This may happen if the anchorPoint of your layer is (0.5, 0.5), which is the default value. If you set:
textLayer.position = CGPointMake(10.0, 10.0);
you may think it should draw the text sharp. However, position point is in the center of the layer here, and depending on the layer's width and height the left and top edge's coordinates may be fractional numbers.
If you want to make a quick test do this.
Use textLayer.frame = frame instead of using position and anchor point, it will assign the coordinates directly to the frame.
Make sure the numbers you use in the frame are rounded.
Do not mess with rendering mechanism, remove the code that changes shouldRasterize, antialiasing, etc.
If this makes the text sharp, you can start using the anchor point and position and to see how the result changes.

How can I trim a UIImageView to fit an aspect ratio image

I am using a crop tool in my app and I need to modify a UIImageView so that it fits an image exactly after inserting the image in aspect fit mode.
So an image is selected and added to the UIImageView in aspect fit mode. The problem is that this then leaves "blank space" around the image inside the UIImageView that needs trimming. I was wondering how I could then go and resize the holding UIImageView based upon the image inside.
Is this possible?
The Easy way is simply using the following code on your "imageView"
imageView.contentMode = UIViewContentModeScaleAspectFit;
Assuming you want to cut a "zoomed" section of your image to fit fully into your imageView
check your original image width and height
Assuming width is bigger in size then height , scale the image width to the holder width
center the image on your holder , the width will fit perfectly (section2) and the height will simply be cropped follow above and below the holder.
It turns out that a better approach is to use the following idea.
How to get the size of a scaled UIImage in UIImageView?
Instead of trimming the UIImageView, insert the image and then get the dimensions of the image inside the UIImageView, from there you can then resize the UIImageView to match the dimensions of the image inside.

Resizing CAShapeLayer

I have a simple test app on which my rootViewController's UIView contains a bunch of UIView subviews. Each one of those UIView subview is backed by a CAShapeLayer.
I want the composition created by those subviews ( the four shapes that are within the dotted area .. ) to always stay vertically and horizontally centered with respect to my
UIWindow. (the minimium size of the left/right, top/bottom margins will be subject to be changed at runtime at each orientation change )
So for example when i rotate to portrait i will have to
resize and reposition those single shapes so that the whole figure will be mantained centered and each CAShapeLayer sublayer stays sharp ( i want their path to be resized not just a raster resize )
what would be the best technique to resize/move the shapes to always have a centered composition while maintaining path crisp appearance for the shapes?
Ultimately for me it will be good to have an answer to this: how can i shrink the subviews as a whole? i mean their sizes and relative positions?
Thanks
You can use CGPathCreateMutableCopyByTransformingPath() or -[UIBezierPath applyTransform:] to recalculate all points in the path.

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.