Question

I have a special requirement in my IOS app, I want to flip every characters in a string, the following picture is the input and final result I want.

enter image description here

Currently I have tried with UIFont, I use setTransform method, but this applies to the context which flip the entire string. Then I try to use NSAttributedString since it can apply special effect to a given range in the text. I go through all the attributes in this page under Constants section, but it seems there is no such attribute.

Is that possible to create context for each character in a string? How can I manage the spaces between characters if this is true?

Was it helpful?

Solution

You could reverse the string and then flip it.

I.e.

ABCD -> DCBA -> the flipped image you want.

Just use the transform like you did before but reverse the string first.

OTHER TIPS

Ok, it's a bit hard...

First, split the string into parts that should be upside-down, and parts that should be normal. E.g. you may want to split "Hello world!" and "world" should be upside down. So you have "Hello ", "world", "!". You can always make it so that the first string is normal: E.g. if you want to have "Hello" upside-down and the rest of the string normal, the list would be "", "Hello", " world!".

For each of the strings create a UILabel. Call -sizeToFit on each of them. Place them next to each other. Set the transform property for each UILabel that should be upside down (i.e. for every second UILabel) to the appropriate value.

You may wrap all of the UILabels so created into a container view, or even create a UIView subclass that hides all the implementation details (recommended).

Done.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top