【问题标题】:Display fraction number in UILabel在 UILabel 中显示分数
【发布时间】:2015-06-16 05:20:11
【问题描述】:

我正在开发必须在标签上显示分数的应用程序。

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];

此代码的输出为 1/5。我想这样展示⅕。

我尝试过使用属性字符串,但它不起作用。

谁能帮帮我。

提前致谢。

【问题讨论】:

  • 它不起作用是什么意思?你能分享一下你试过的代码吗?

标签: ios objective-c iphone fractions


【解决方案1】:

我创建了以下两个辅助函数,它们将以下标或上标格式返回给定数字 (0-9) 的 unicode 字符串:

-(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)];
}

一旦你声明了这些,你可以很容易地调用这样的东西:

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

这将输出以下内容:

⁵/₆

甚至是我普通UILabel的截图:

编辑

这是一个适用于任何分数的函数,包括37/100,例如:

-(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;
}

调用以下代码:

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

记录³⁷/₁₀₀

【讨论】:

  • 哦,来吧……他已经给了你答案……你至少不能试着自己解决这个问题吗?
  • @Bhumika 查看我的编辑。你只需要遍历数字。
【解决方案2】:

如果您只想使用非常常见的分数,请使用它们的标准 Unicode 字符,如其他答案中所述。

或者,要获得任意分数,您可能希望从 Unicode 硬编码上标和下标小数的所有字符。还有一个特殊的斜线。

下面的代码会将NSString 分子和NSString 分母转换为像¹³⁄₅ 这样的分数。可以轻松修改为使用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 @"";
}

【讨论】:

    【解决方案3】:

    尝试将标签的文本属性设置为 Unicode 的 1/5:

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

    【讨论】:

    • 但是我的值每次都是动态的。那么它将如何工作,因为你知道 1/2 unicode 是 0x00bd。但是如果值是动态的会怎样
    【解决方案4】:

    我不知道是否有任何其他解决方案可以在“正常”UILabel 中产生摩擦,但您可以创建自己的。

    创建从 UIView 扩展的新类,并在其间绘制两个字符串和线。

    你可以使用

    [str drawAtPoint:CGPointMake(x,y)]
    

    您只需编写一个采用两个数字的 set 方法或十进制数的 setter,然后将其转换为摩擦。

    - (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)] ;
    }
    

    或使用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)] ;
    
    }
    

    编辑


    用例:

     @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);
    }
    

    这是一个简单的示例,需要根据您的意愿进行配置,但您可以从这里开始。

    【讨论】:

    • 嗨,Marko,我试图按照你的方式行事。我创建了一个 UIView 子类,其中有 2 个字符串表示分子和分母以及一个飞溅。我需要重新定义 drawRect 函数吗?如果是这样,我该怎么做?谢谢
    【解决方案5】:

    您可以通过将UILabel's text 属性设置为 1/2 的 Unicode 来实现:

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

    另见以下链接
    NSString: Looking for Unicode for superscript: 1, 2, 3
    可能对你有帮助。 享受;)

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多