Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

ios - Display fraction number in UILabel

I am working on the app in which I have to show fraction number on label.

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 like this ?.

I have tried with attribute string but it doesn't work.

Can anyone please help me.

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

From the simulator

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?/???.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...