How to align an 90 deg rotated label perfectly with the pixels? - iphone

When rotating a label, the text is rendered very ugly. Is there an convenient way to get the label aligned with the pixels? Maybe there is some way of rounding the transform of the view, so that it isn't misaligned?

If you use CGAffineTransformRotate or a similar function you won't see any artifacts in the text because the label is drawn first and then rotated in its entirety.
If you are seeing artifacts using a transform, then the most likely source is a transparent label background distorting the appearance of the text. If the background is variable, such as pin stripe or a picture, then rotating the text will put different pixels adjacent to the text which can muddle its outline and apparent color.

Related

Swift iOS UI Advice

I am looking for some general advice and maybe some example code of what I am trying to accomplish if anyone knows of any for an iOS swift project. I would like to either:
A) Make the background, of the blue view, gray and only show a certain percent of the blue area.
OR
B) Overlay the gray area on top of the blue view and just keep making gray area bigger.
What I am trying to do is simulate battery power and show a battery.
I've considered using a progress bar and doing option A, but the blue area is NOT a solid color. Its actually an image. I've tried using an image for the progress bar, but the image needs to keep its dimensions. (Ex: If progress shows 20% it needs to show only 20% of the image or "blue area", but if you use an image as the progress bar it just shrinks the image and still shows 100% of it instead of just the 20% I need to show).
You can easily write a custom self-drawing UIView that will behave in exactly the way you describe. In other words, you tell your UIView a percentage, and it redraws itself with the blue on the left and the gray on the right. You can even draw the darker gray stroke outline shown in your drawings. All easily accomplished in code.
I like being able to lay things out visually and take advantage of autolayout. Here's how I would do this (in a nib/storyboard):
Place a UIView on your canvas and give it the gray background. Give it whatever autolayout constraints are appropriate for you.
Place a UIView inside the one from #1 and give it the blue background. Anchor it's left, top, and bottom to the gray parent view and give it whatever width (doesn't matter).
Add an outlet to that width constraint you made in #2.
Now all you have to do is modify the "constant" property of that width constraint to give you the desired "progress". So if your gray view is 100 wide and you want to present "20%" progress, then just do "yourWidthConstraint.constant = 20".

Core graphics color blending

I have a line of text which I want to display behind a CALayer, and when behind, then the text color should appear in a different color in the overlapping area.
The color of the text is black, and the text color should appear as white on the red layer when they overlap (they may both move around, so this needs to be continuously updated).
How may this be accomplished?
This can’t be done with any blend mode. You’ll need to render the text into the foreground layer in white by hand.
One trick you could try is to keep the white text always visible, but clipped by the layer that moves around using a second layer that also moves, set as the text’s mask.

Cocos2D changes the colours in the last row of pixles in a sprite?

I have never seen this issue before, its very strange. Just wondering if anyone else has come across this too.
I have added a sprite to my game, its supposed to be the top left corner of a box to put text on to. I want to make it scalable without loosing anything so i have it broken up into sections. In the image above the one on top is the image itself, and then the one on the bottom is the image when its being drawn in the iPhone simulator.
Any idea why the last column of pixels on the right are altered? I have not scaled the image at all.
I don't know about Cocos2D, but in general terms what you've done here is to draw the image at a position that isn't an exact multiple of one pixel.
Consequently even without scaling you have resampled the image across a grid that doesn't coincide with the original image data, causing all pixels to be a bit off. It's just the right-hand edge is the most obvious case, since the resampling leaves you with a partial transparency here. But look at eg the two rows of purple pixels in the border: they're not the same, because your vertical alignment is off as well, causing a small amount of colour from the grey border below it to bleed into the lower row of purple.
Ok I actually figured it out this time. Cocos2D adds a bit of antialiasing to CCTextures. To stop it from doing this you need to call this:
[[mySprite texture] setAliasTexParameters];
to turn it back on you call this:
[[mySprite texture] setAntiAliasTexParameters];

Why does a PNG image sometimes get blurry depending on its position in a view

I have noticed that when placing PNG images into a view using IB and/or animating those images to various positions around a view, the image can sometimes get a slight blur.
In most cases I can remedy the blur by adding .5 of a pixel to the images position.
[lbLiteButton.layer setPosition:CGPointMake(140.5,159.5)];
Sometimes I have to adjust both x and y like above. Sometimes I only have to adjust x or y.
I remember reading somewhere that this has to do with the size of the image and how core animation works and something to do with half pixels... but I cant find the article anywhere!?
The problem with the ".5 pixel" solution is that its different for every PNG image depending on size, so you can't reuse custom animation because you have to customise it for each different image.
Is there a way to ensure that no matter where I place or animate my image, I won't get any blurred positions?
Does anyone have any information on this?
Thank You!
The position property of a view's layer is based on its anchorPoint property. By default, that is (0.5, 0.5), meaning that the anchor point of the layer is at its center. If your view (and its layer) are an odd number of pixels wide or high, setting an integral value for the position will cause the view's origin to be non-integral, leading to the blurriness you see.
To work around this, you could figure out an integral version of your position by taking the desired center position of the view, subtracting half of the view's width, rounding that value, then adding half of the view's width, and repeating for the height. You could also set the anchorPoint for your view's layer to (0,0) and position the view based on its origin.
There's a chance that this might also be related to a misalignment of a superview. To diagnose this, you could use the Core Animation instrument in Instruments, and select the Color Misaligned Images option. It should color any views or layers that are non-pixel-aligned in your application.
I had a similar experience with blurred text in a label, and it was caused by the superview of my labels having a subpixel offset. So even though the location within that view was integral, when adjusted to its parent's coordinates it had a half pixel or so offset, causing the blur.
If you're getting this only sometimes, though, that might not be the case. Is your superview moving around, or positioned strangely? I'd say the best thing to do is to figure out the precise circumstances under which this is happening.
IB has a bug where sometimes (not often) just moving elements around will render them fuzzy - you see it most often with UILabels and UIImageViews (although that's probably just what is most apparent). I'm sure it has to do with the points mentioned above in some way, but the fix is often to set the location (x,y) coordinates for the element to 0,0, and then back to the original values. This usually resolves the issue (again, this is in IB).

UIImageView renders image differently to original

The image on the right is the one that I produced in photoshop. I then stripped all text and put it in an image view, as soon as I did that there was a change in colour and the vertical line lost it sharpness. Has anyone else run into a similar problem? What do I do?
alt text http://grab.by/1DuZ
Are the dimensions correct? Is the position of the image an integer? If these cases antialiasing will slightly blur your image.
One thing to be careful of is that if your image is an odd number of pixels in either dimension then centering it onscreen will cause it to be misaligned. Imagine if you had a 1x1 image (just one pixel) and tried to center it perfectly onscreen. It can't be done because the screen is an even number of pixels wide and high. This is why it's best to always use even dimensioned images whenever possible.