【问题标题】:Format an NSNumber in to superscript or subscript unicode characters将 NSNumber 格式化为上标或下标 unicode 字符
【发布时间】:2015-01-15 15:22:55
【问题描述】:

Unicode 包括上标字符,例如“⁰¹²³⁴⁵⁶⁷⁸⁹”和下标:“₀₁₂₃₄₅₆₇₈₉”。我想将一个数字格式化为上标数字或下标数字——我相信它应该可以在UILabelUITextField 中使用。

具体来说,我不想使用NSAttributedString

谢谢。

【问题讨论】:

    标签: unicode nsstring string-formatting nsnumber


    【解决方案1】:

    我在 NSString 上添加了一个类别,但在使用前请阅读下面的巨大警告

    -(NSString*) asSuperscript
    {
        return [self tranformWithMap:
                @{
                  @"0": @"⁰",
                  @"1": @"¹",
                  @"2": @"²",
                  @"3": @"³",
                  @"4": @"⁴",
                  @"5": @"⁵",
                  @"6": @"⁶",
                  @"7": @"⁷",
                  @"8": @"⁸",
                  @"9": @"⁹"
                  }];
    }
    
    -(NSString*) tranformWithMap: (NSDictionary *) map
    {
        NSString *output = self;
    
        for (NSString *from in map.allKeys)
        {
            output = [output stringByReplacingOccurrencesOfString: from withString: map[from]];
        }
    
        return output;
    }
    

    上面的实现效率低得惊人,但对于短字符串就可以了——嘿,计算机很快,对吧?

    但是,在 iOS 上,我需要这个,系统字体的上标字符看起来很糟糕。特别是“123”的字符不垂直排列与“0456789”,它们都在更高的位置!

    【讨论】:

      猜你喜欢
      • 2012-10-29
      • 2014-10-26
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      • 2017-06-21
      • 1970-01-01
      • 2012-03-19
      • 2015-05-22
      相关资源
      最近更新 更多