Why is UILabel's text blurry on iPad if width is not even? - objective-c

Following phenomenon: my text is "Search". I create a UILabel of SmallSystemFontSize and call sizeToFit:.
The result is 39 units wide and the text looks kind of blurry.
If I adjust the width to 40 it looks perfect.
I read that the text gets blurry if you hit sub pixels, meaning the width would be something like 39.5, but it seems it has to be even.
Can somebody confirm or even explain what is going on ?

In my case, having set shouldRasterize = YES on the CGLayer of the UILabel's superview was the culprit. Removing that line made the text nice and crisp.

UIView items are positioned by their center which for a size that is odd is on a half pixel, 19.5 for a width of 39.. This alignment causes pixel averaging that causes the fuzziness.
One way is to make it an even width.
Another is to place it by the center at an even point use:
#property(nonatomic) CGPoint center
Example, for a desired position of label; at (10, 10, 39, 19) one could use:
label.center = CGPointMake(50, 20);

Related

Add horizontal padding if the original image is less than the width specified

I don't think this is possible out of the box but wanted to make sure.
We'd like to do the following.
Take any image input and force the output width to be a fixed size. If the width is less than the output width, we'd like to center the image and add horizontal padding to the image but not add vertical padding.
For example
Original image is 700px x 400px
Final output size of 1000px width x 400px. This would include 150px padding left and 150px padding right (no top / bottom padding).
I know that we can upscale the image (scale=both) or set the canvas scale (but that adds top / bottom padding) or we could add padding to the image but none are really what we want.
Thanks for any help
Response to Nathanael
Your comments are exactly correct.
I expected http://z.zr.io/ri/red-leaf.jpg?width=1000&scale=canvas&bgcolor=gray to work exactly as it does
Yes, our problem is that the image heights are not known beforehand, but it's good to know that this works with a known height
I think it would be great if there was a command for scale=padwidth that would work with variable heights. Or a setting for padwidth=true and padheight=false that could be used in conjunction with scale=canvas.
So, let's say that you're given an 800x600px image, and you apply ?width=1000&scale=canvas. You were expecting that this would produce a 1000x600px image, but instead it produced a 1000x750px image, right?
http://z.zr.io/ri/red-leaf.jpg?width=1000&scale=canvas&bgcolor=gray
If you specify the height explicitly, the padding goes away - but you may not know the image height beforehand, correct?
http://z.zr.io/ri/red-leaf.jpg?width=1000&scale=canvas&bgcolor=gray&height=600
What would be the least surprising behavior - maintaining aspect ratio, or only adding the minimum padding required? How would you expect this to behave, or be exposed as a command?

Objective C - Adjust height of image in image view automatically after original size

I am developing an app that will show flags for countries some places, but after looking at flags I realized that the flags format was different for almost every country. Therefor I would like the height of the image view to automatically adjust it self to the width i set. Example:
Standard width for all flags: 100 px
USA : Height: 50px
UK: Height 56 px
Russia: Height 34px
I have no idea how to solve this, thanks for help! The best would be if this could be done automatically - without me needing to create example arrays or something for every flag to adjust size.
This is a ratio problem. Suppose your English flag is 120x80px. You want it to be 100px wide. How tall will it be? You have to scale the width and height by the same ratio.
First of all, calculate the ratio between the desired width of the flag and its actual width:
CGFloat ratio = 100.0f / 120.0f;
That gives us a ratio of 0.83. Now we can calculate the display height by multiplying the actual height by the ratio:
CGFloat height = 80.0f * 0.83;
The display height is 66.4px.
Here's the neat thing: UIImageView does this for you. If you specify the width of the image view and set its content mode to UIViewContentModeScaleAspectFit it does the hard work automatically.
See this question:
How to scale a UIImageView proportionally?
You could set the size of the image view from the size of the image as follows:
UIImage* flagImage = [UIImage imageNamed:"flagimage.png"];
CGRect flagImageRect = flagImageView.frame;
flagImageRect.size = flagImage.size;
flagImageView.frame = flagImageRect;
flagImageView.image = flagImage;
I guess you would do the above in a loop where you are setting the flag images for all your image views. You could also take the opportunity to scale your images (if desired).

Position Subviews Relative to Screen Estate

I'd like to display multiple small UIViews as Subviews relative to the screen estate. This should work across different screen sizes (iPad, iPhone)/portrait/landscape modes.
Each subview to display has two NSNumber objects with an unsigned int ranging from -100 (min) to 100 (max) which needs to be mapped to the correct x and y coordinates for positioning.
What's the best way to translate those values (-100...100) to use them for positioning UIViews on the screen?
How do I position them in a relative rather then an absolute way, so that the code works across screen rotation and screen sizes?
Ok, so if I understand correctly you want a -100 in the x direction to map to the left most point on the screen, 100 in the x to map to the right most point on the screen, -100 in the y direction to map to the lowest point on the screen, and 100 in the y to map to the highest point on screen (or maybe you want the y inverted from what I have so that it agrees with the screen coordinate system in which y becomes bigger the lower on the screen you get?).
And we also want to account for rotation.
As far as I understand it, asking UIScreen for its height and width:
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;
but this does not account for rotation. The only other way I am aware of that is pretty straightforward would be to ask a UIView covering the screen for its width and height (most simply, you could make your viewcontroller's view cover the whole screen).
If you had a UIView that perfectly covered the whole screen (let's call it myView), you could try:
CGFloat width = myView.frame.size.width;
CGFloat height = myView.frame.size.height;
these should adjust for orientation by themselves (from my experience, it should definitely work if you get the height and width in viewDidAppear:animated: or anything after. also the UIView needs to either be the UIViewControllers view property or a subview of this view. if not, you'll have to implement didRotateFromInterfaceOrientation: or find some other way to tell your view about any rotations). Once we have the 'width' and 'height' of the screen, we can convert from your int's to screen position. Try something like:
(CGPoint)convertX:(NSNumber *)x andY:(NSNumber *)y intoPoint
{
pointX = ([x intValue] + 100.0)*width/200.0;
pointY = (-[y intValue] + 100.0)*height/200.0; // remove the - sign at the front of the expression for y to grow as you move down the screen
return CGPointMake(pointX, pointY);
}
to convert from -100 to 100 in x and y to their respective points on the screen.
If you're working with a range of +/-100, then you may want to use the underlying CALayers to position your views. The nice part about CALayers, is that their anchor points are mapped to a device-agnostic grid that ranges from 0.0 to +1.0 on a Cartesian plane.

Draw rotated text to parent coordinate system

I have a UIView, which I'm drawing manually in the 'drawRect'-Function.
It is basically a coordinate system, which has 'Values' on the Y-Axis and 'Time' on the 'X-Axis'.
Due to space issues, I want the Timestamps to be vertical, instead of horizontal.
For this purpose, I use:
CGContextSaveGState(ctx); //Saves the current graphic context state
CGContextRotateCTM(ctx, M_PI_2); //Rotates the context by 90° clockwise
strPos = CGContextConvertPointToUserSpace(ctx, strPos); //SHOULD convert to Usercoordinates
[str drawAtPoint:strPos withFont:fnt]; //Draws the text to the rotated CTM
CGContextRestoreGState(ctx); //Restores the CTM to the previous state.
ctx (CGContextRef), strPos (CGPoint) and str (NSString) are variables, that have been initialized properly and correctly for 'horizontal text', with a width of the text height.
While this code works flawlessly on the iPhone 3, it gives me a complete mess on the iPhone 4 (Retina), because the CGContextConvertPointToUserSpace function produces completely different results, even though the coordinate system of the iPhone is supposed to remain the same.
I also tried using CGAffineTransform, but only with the same results.
To summarize my question: How do I draw a text to a calculated position in the parent coordinate system (0, 0 being top left)?
After studying the Apple docs regarding Quartz 2D once more, I came to realize, that the rotation by Pi/2 moves all my writing off screen to the left.
I can make the writing appear in a vertical line by translating the CTM by +height.
I'll keep trying, but would still be happy to get an answer.
Edit: Thanks to lawicko's heads-up I was able to fix the problem. See Answer for details.
I would like to thank lawicko for pointing this out.
During my tests I made two mistakes...but he is of course correct. Using CGContextShowTextAtPoint is the most simple solution, since it doesn't require the rotation of the entire CTM.
Again, THANK you.
Now, for the actual answer to my question.
To draw a rotated text at position x/y, the following code works for me.
CGAffineTransform rot = CGAffineTransformMakeRotation(M_PI_2); //Creates the rotation
CGContextSelectFont(ctx, "TrebuchetMS", 10, kCGEncodingMacRoman); //Selects the font
CGContextSetTextMatrix(ctx, CGAffineTransformScale(rot, 1, -1)); //Mirrors the rotated text, so it will be displayed correctly.
CGContextShowTextAtPoint(ctx, strPos.x, strPos.y, TS, 5); //Draws the text
ctx is the CGContext, strPos the desired position in the parent coordinate system, TS a char array.
Again, thank you lawicko.
I probably would've searched forever if not for your suggestion.
Maybe this answer will help someone else, who comes across the same problem.

UIImageView - Fixed width & Flexible height?

CGRect imageLocation = CGRectMake(startingXCoordinate, startingYCoordinate + detailedTitleLabel.frame.size.height + marginY, 280, 300);
self.detailedImageView.frame = imageLocation;
This is the code I've got so far. You can see the width is 280 and the height is 300 for now... In the interface builder, I also set this image view as 'Aspect Fit' to keep the ratio.
The problem here is... for some bigger or smaller images, they're located in the centre of the UIImageView and it naturally creates some extra top & bottom margins. How can I make a perfectly fitting UIImageView not ruining the ratio with 280 width?
Thank you in advance
Did you try aspect fill instead of aspect fit? That should have solved your issue, I think.