I have created the following two helper functions which will return the unicode string for a given number (0-9) in subscript or superscript format:
-(NSString *)superscript:(int)num {
NSDictionary *superscripts = @{@0: @"u2070", @1: @"u00B9", @2: @"u00B2", @3: @"u00B3", @4: @"u2074", @5: @"u2075", @6: @"u2076", @7: @"u2077", @8: @"u2078", @9: @"u2079"};
return superscripts[@(num)];
}
-(NSString *)subscript:(int)num {
NSDictionary *subscripts = @{@0: @"u2080", @1: @"u2081", @2: @"u2082", @3: @"u2083", @4: @"u2084", @5: @"u2085", @6: @"u2086", @7: @"u2087", @8: @"u2088", @9: @"u2089"};
return subscripts[@(num)];
}
Once you have these declared, you can easily call something like this:
NSLog(@"%@/%@", [self superscript:5], [self subscript:6]);
Which would output the following:
?/?
And even a screenshot for ya from my normal UILabel
:
EDIT
Here's a function that will work with any fraction, including 37/100
, for example:
-(NSString *)fraction:(int)numerator denominator:(int)denominator {
NSMutableString *result = [NSMutableString string];
NSString *one = [NSString stringWithFormat:@"%i", numerator];
for (int i = 0; i < one.length; i++) {
[result appendString:[self superscript:[[one substringWithRange:NSMakeRange(i, 1)] intValue]]];
}
[result appendString:@"/"];
NSString *two = [NSString stringWithFormat:@"%i", denominator];
for (int i = 0; i < two.length; i++) {
[result appendString:[self subscript:[[two substringWithRange:NSMakeRange(i, 1)] intValue]]];
}
return result;
}
Calling the following:
NSLog(@"%@", [self fraction:37 denominator:100]);
Logs 3?/???
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…