NSString representation of fractions using unicode

I am trying to "beautifully" display the shares in the application for the iPhone. I used to use the tedious switch statement leading to hard-coded Unicode characters for vulgar fractions, but I found out about the unicode slash character, which, if I understand it correctly, should mean that I can create a line like this: / p>

[NSString stringWithFormat:@"%i\u2044%i",numerator,denominator]; 

And the "renderer" will automatically print it with a smaller superscript numerator and indexed denominator. However, the code above just gives me a standard 1/2 view. I use drawAtPoint to put a string on the screen. I experimented with decposedStringUsingCanonicalMapping and precomposedStringUsingCanonicalMapping, but to be honest, the documentation has lost me.

Should this work, or is the NSString drawing not handling it?

+6
source share
4 answers

I do not know how the unicode character will have the properties that you describe. AFAIK - the only thing that distinguishes U + 2044 from a regular slash is a little more angular and has little space for either side, so it approaches the surrounding numbers.

Here's a page on using Fraction Slash in HTML, and as you can see, it shows that you just get something like "1/10" if you try to use it yourself. He compensates for this by using the <sup> and <sub> tags in the HTML on the surrounding numbers to get an appropriate display.

In order for you to work in NSString, you will need to figure out how to use add-ons and subscribe to surrounding numbers.

+1
source

I just wanted the simple fractions for recipes to be converted to coarse Unicode fractions.

Here's how you can do it:

 CGFloat quantityValue = 0.25f; NSString *quantity = nil; if (quantityValue == 0.25f) { // 1/4 const unichar quarter = 0xbc; quantity = [NSString stringWithCharacters:&quarter length:1]; } else if (quantityValue == 0.33f) { // 1/3 const unichar third = 0x2153; quantity = [NSString stringWithCharacters:&third length:1]; } else if (quantityValue == 0.5f) { // 1/2 const unichar half = 0xbd; quantity = [NSString stringWithCharacters:&half length:1]; } else if (quantityValue == 0.66f) { // 2/3 const unichar twoThirds = 0x2154; quantity = [NSString stringWithCharacters:&twoThirds length:1]; } else if (quantityValue == 0.75f) { // 3/4 const unichar threeQuarters = 0xbe; quantity = [NSString stringWithCharacters:&threeQuarters length:1]; } NSLog(@"%@", quantity); 
+2
source

There are some Unicode characters included that give the actual appearance of the fractions, but they are limited to 1/2, 1/3 and 1/4, which I think.

If you want this for arbitrary fractions, it seems to me that you need a custom look that attracts the appropriate look; either using positioned subzones or drawRect:

0
source

I know this was a long time ago, but if that helps, there are Unicode superscript characters for all decimal numbers that you can use to display arbitrary fractions in most fonts; see answers to a similar question here - fooobar.com/questions/894145 / ...

Edit:

According to the comments, this decision depends on the font you are using. Amsi Pro (left) and other commercial fonts usually include all the necessary characters for the labels, but the system font (right) does not.

Nice fontSystem font

0
source

Source: https://habr.com/ru/post/894143/


All Articles