6

I am trying to "nicely" display fractions in my iPhone application. Previously I have been using a tedious switch statement leading to hardcoded unicode characters for the vulgar fractions, but I have learnt about the unicode fraction slash character which, if I am understanding it correctly, should mean that I can create a string as follows:

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

And the "renderer" will automatically print it with a smaller, superscriped numerator and subscripted denominator. However, the above code just gives me the standard 1/2 appearance. I am using drawAtPoint to put the string on the screen. I have experimented with decomposedStringUsingCanonicalMapping and precomposedStringUsingCanonicalMapping but to be honest the documentation lost me.

Should this be working or does NSString drawing not cope with this?

jrturton
  • 118,105
  • 32
  • 252
  • 268

4 Answers4

2

I happened to only want simple fractions for recipes to be converted to Unicode vulgar fractions.

Here is 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);
Cameron Lowell Palmer
  • 21,528
  • 7
  • 125
  • 126
  • You must suffer with lots of "misses" due to floating-point rounding errors. You need to perform the comparison with an *epsilon* value. – Droppy Jun 16 '15 at 09:44
  • That's an excellent point. I more specifically was demonstrating the display of unicode fractions, but rounding errors will be an issue. – Cameron Lowell Palmer Jun 17 '15 at 11:37
1

I'm not aware of any way for a unicode character to have the properties you describe. AFAIK the only thing that distinguishes U+2044 from a regular slash is it's a bit more angled and has little-to-no space on either side, therefore making it nestle up a lot closer to the surrounding numbers.

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

In order for you to get this to work in NSString you're going to have to figure out some way to apply superscripting and subscripting to the surrounding numbers yourself.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • I was working from the "Fraction Slash" section of [wikipedia](http://en.wikipedia.org/wiki/Mapping_of_Unicode_characters) which gave me hope that it would work. – jrturton Aug 02 '11 at 21:32
  • 1
    It appears that, at least on the desktop, the Cocoa text rendering system follows fraction synthesizing information provided in fonts, but does not synthesize fractions if the font itself does not support it. For example, if you type 1⁄2 using Apple Chancery it displays as a true fraction, but in Helvetica it doesn't. I don't know if the text rendering system in Cocoa Touch supports this at all. – Lily Ballard Aug 02 '11 at 22:44
  • `sub` and `sup` can be set for `NSAttributedString`. – Sulthan Jun 17 '15 at 08:57
0

I know this was a long time ago, but if it helps, there are superscript Unicode characters for all decimal numbers you can use to display arbitrary fractions in most fonts; see answers on a similar question here - https://stackoverflow.com/a/30860163/4522315

Edit:

As per comments, this solution depends on the font you're using. Amsi Pro (left) and other commercial fonts tend to include all the required symbols for superscripts, but the system font (right) does not.

Nice font System font

Community
  • 1
  • 1
Corwin Newall
  • 508
  • 8
  • 18
0

There are some included Unicode chars that do give actual fraction appearances, but they're limited to a 1/2, a 1/3 and a 1/4 I think.

If you want this for arbitrary fractions, seems to me like you need a custom view that draws the appropriate look; either through using positioned subviews or drawRect:.

Benjamin Mayo
  • 6,649
  • 2
  • 26
  • 25