Display fraction number in UILabel

I am working on an application in which I need to show the fraction number on a shortcut.

NSMutableAttributedString * hogan = [[NSMutableAttributedString alloc] initWithString:self.toValue]; [hogan setAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"DINPro-Bold" size:11.0]} range:NSMakeRange(9,6)]; UILabel *fal = [[UILabel alloc] initWithFrame:CGRectMake(100,12, 150.0, 50)]; fal.text = @""; fal.textColor = [UIColor whiteColor]; fal.backgroundColor = [UIColor redColor]; fal.font = [UIFont fontWithName:@"DINPro-Bold" size:18.0]; fal.attributedText=hogan; [self addSubview:fal]; 

and the output of this code is 1/5. I want to show it as follows.

I tried using the attribute string, but it does not work.

Can anybody help me.

Thanks in advance.

0
source share
5 answers

I created the following two helper functions that will return a unicode string for a given number (0-9) in index or superstructure 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 declare them, you can easily call something like this:

 NSLog(@"%@/%@", [self superscript:5], [self subscript:6]); 

To deduce the following:

⁡/₆

And even a screenshot for ya from my regular UILabel :

From the simulator

EDIT

Here is a function that will work with any share, 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; } 

Call the following:

 NSLog(@"%@", [self fraction:37 denominator:100]); 

Magazines ³⁷/₁₀₀ .

+5
source

If you want to use very common fractions, use their standard Unicode characters, as indicated in other answers.

Or, to get an arbitrary fraction, you can program all characters for index indices and index indices from Unicode. There is also a special fractional fraction.

The code below converts the NSString numerator and the NSString denominator to a Β³Β³ / fraction. You can easily change to use NSNumberFormatter .

 // Constants for the different Unicode charcodes, where the nth array // member is for the digit n. NSArray *superscriptDigits = @[@"\u2070", @"\u00b9", @"\u00b2", @"\u00b3", @"\u2074", @"\u2075", @"\u2076", @"\u2077", @"\u2078", @"\u2079"]; NSArray *subscriptDigits = @[@"\u2080", @"\u2081", @"\u2082", @"\u2083", @"\u2084", @"\u2085", @"\u2086", @"\u2087", @"\u2088", @"\u2089"]; NSString *slashCharacter = @"\u2044"; +(NSString *)fractionStringFromDenominator:(NSString *)denominatorString numerator:(NSString *)numeratorString { NSMutableString *fractionString = [NSMutableString new]; for (int i = 0; i < denominatorString.length; i++) { unichar c = [denominatorString characterAtIndex:i]; [fractionString appendFormat:@"%@", [self unicodeForDigitChar:c subscript:NO]]; } [fractionString appendFormat:@"%@", slashCharacter]; for (int i = 0; i < numeratorString.length; i++) { unichar c = [numeratorString characterAtIndex:i]; [fractionString appendFormat:@"%@", [self unicodeForDigitChar:c subscript:YES]]; } return fractionString; } +(NSString *)unicodeForDigitChar:(unichar)c subscript:(bool)subscript { if (c >= '0' && c <= '9') { return subscript ? subscriptDigits[c - '0'] : superscriptDigits[c - '0']; } // Not a standard digit character return @""; } 
+2
source

I don’t know if there is any other solution to have friction inside the β€œnormal” UILabel, but you can create your own.

Create a new class extending from UIView and draw two lines and a line between them.

you can use

 [str drawAtPoint:CGPointMake(x,y)] 

Instead of just writing a set method that takes two numbers or setter for a decimal number, and how do you convert this to friction.

 - (void) setFractionValue: (NSNumber * ) a divider: (NSNumber * ) b { // Draw fraction a/b [[NSString stringWithFormat:@"%@", a] drawAtPoint:CGPointMake(x,y1)] ; [[NSString stringWithFormat:@"%@", b] drawAtPoint:CGPointMake(x,y2)] ; } 

or use something like this :

 - (void) setDecimalValue: (NSNumber *) decimal { // convert decimal to fraction // get a and b from fraction // Draw fraction a/b [[NSString stringWithFormat:@"%@", a] drawAtPoint:CGPointMake(x,y1)] ; [[NSString stringWithFormat:@"%@", b] drawAtPoint:CGPointMake(x,y2)] ; } 

EDIT


Use Case:

  @implementation TestDrawText - (void)drawRect:(CGRect)rect { [super drawRect:rect]; [self drawText:rect.origin.x + 10 yPosition:rect.origin.x + 10 text:@"2"]; [self drowLine:rect.origin.x + 11 yPosition:rect.origin.x + 30]; [self drawText:rect.origin.x + 20 yPosition:rect.origin.x + 20 text:@"5"]; } - (void)drawText:(CGFloat)xPosition yPosition:(CGFloat)yPosition text: (NSString * ) text{ CGPoint point = CGPointMake(xPosition, yPosition); NSDictionary* textFontAttributes = @{NSFontAttributeName: [UIFont fontWithName: @"Helvetica" size: 12], NSForegroundColorAttributeName: UIColor.redColor}; [text drawAtPoint:point withAttributes:textFontAttributes]; } - (void) drowLine:(CGFloat)xPosition yPosition:(CGFloat)yPosition { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); // Draw them with a 2.0 stroke width so they are a bit more visible. CGContextSetLineWidth(context, 2.0f); CGContextMoveToPoint(context, xPosition, yPosition); //start at this point CGContextAddLineToPoint(context, xPosition + 15, yPosition - 15); //draw to this point // and now draw the Path! CGContextStrokePath(context); } 

This is a quick example and you need to customize your wishes, but this is the beginning from which you could go.

+1
source

You can achieve this by setting UILabel's text property to Unicode 1/2:

 myLblName.text = [NSString stringWithFormat:@"%C",0x00bd]; 

Also see below link
NSString: Unicode lookup for add-ons: 1, 2, 3
May be useful for you. Enjoy;)

0
source

Try setting the label text property to Unicode 1/5:

 label.text = [NSString stringWithFormat:@"%C",0x2155]; 
0
source

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


All Articles