如何在另一个上显示分数文本编号?

How to display fraction text number over another?

我需要在我的应用程序中显示分数,但找不到好的方法?

应该看起来像这样

(概念验证...不需要字体):

还有其他类似的帖子,但它们在 ObJ C 中,我在 swift

中找不到可靠的解决方案

我不得不在应用程序中做类似的事情。我在普通分数和相关的 unicode 字符之间创建了一个映射,如下所示:

enum Fraction: Double {
    case Eighth = 0.125
    case Quarter = 0.25
    case Third = 0.333333333333333
    case Half = 0.5
    case TwoThirds = 0.666666666666667
    case ThreeQuarters = 0.75
}

func localizedStringFromFraction(fraction: Fraction) -> String {
    switch fraction {
    case .Eighth:
        return NSLocalizedString("\u{215B}", comment: "Fraction - 1/8")
    case .Quarter:
        return NSLocalizedString("\u{00BC}", comment: "Fraction - 1/4")
    case .Third:
        return NSLocalizedString("\u{2153}", comment: "Fraction - 1/3")
    case .Half:
        return NSLocalizedString("\u{00BD}", comment: "Fraction - 1/2")
    case .TwoThirds:
        return NSLocalizedString("\u{2154}", comment: "Fraction - 2/3")
    case .ThreeQuarters:
        return NSLocalizedString("\u{00BE}", comment: "Fraction - 3/4")
    }
}

如果您需要支持更多的分数,您可以找到映射here

这是一个 Swift 任意分数的解法:

enum ScriptType {
  case Superscript
  case Subscript
}

func createSuperOrSubscriptDigit(character:Character, type:ScriptType) -> Character {
  switch character {
  case "0": return type == .Superscript ? "\u{2070}" : "\u{2080}"
  case "1": return type == .Superscript ? "\u{00b9}" : "\u{2081}"
  case "2": return type == .Superscript ? "\u{00b2}" : "\u{2082}"
  case "3": return type == .Superscript ? "\u{00b3}" : "\u{2083}"
  case "4": return type == .Superscript ? "\u{2074}" : "\u{2084}"
  case "5": return type == .Superscript ? "\u{2075}" : "\u{2085}"
  case "6": return type == .Superscript ? "\u{2076}" : "\u{2086}"
  case "7": return type == .Superscript ? "\u{2077}" : "\u{2087}"
  case "8": return type == .Superscript ? "\u{2078}" : "\u{2088}"
  case "9": return type == .Superscript ? "\u{2079}" : "\u{2089}"
  default: return character
  }
}

func createSuperOrSubscriptDigits(string:String, type:ScriptType) -> String {
  return String(string.characters.map() { createSuperOrSubscriptDigit([=10=], type: type) })
}

extension String {
  func createSuperscriptDigits() -> String {
    return createSuperOrSubscriptDigits(self, type: .Superscript)
  }
  func createSubscriptDigits() -> String {
    return createSuperOrSubscriptDigits(self, type: .Subscript)
  }
}

func fractionString(numerator:String, denominator:String) -> String {
  return numerator.createSuperscriptDigits() + "\u{2044}" + denominator.createSubscriptDigits()
}

@KennethBruno 的回答有效,但我觉得它有点不雅。这是我的版本:

let superscriptDigits = Array("⁰¹²³⁴⁵⁶⁷⁸⁹".characters)
let subscriptDigits = Array("₀₁₂₃₄₅₆₇₈₉".characters)

func vulgarFractionWithNumerator(numerator: UInt, denominator: UInt) -> String {
    let zeroBias = UnicodeScalar("0").value
    let supers = "\(numerator)".unicodeScalars.map { superscriptDigits[Int([=10=].value - zeroBias)] }
    let subs = "\(denominator)".unicodeScalars.map { subscriptDigits[Int([=10=].value - zeroBias)] }
    return String(supers + [ "⁄" ] + subs)
}

vulgarFractionWithNumerator(123, denominator: 45678)

结果:

"¹²³⁄₄₅₆₇₈"

这只是来自 WWDC 2015 的介绍新系统字体视频中的 Apple 示例代码,该视频被放入 playground 并使用 UILabel 使用字体功能呈现纯文本分数。 [更新 Swift 4]

//: Playground - noun: a place where people can play

import UIKit
import CoreGraphics

let pointSize : CGFloat = 60.0
let systemFontDesc = UIFont.systemFont(ofSize: pointSize,
                                       weight: UIFont.Weight.light).fontDescriptor
let fractionFontDesc = systemFontDesc.addingAttributes(
    [
        UIFontDescriptor.AttributeName.featureSettings: [
            [
                UIFontDescriptor.FeatureKey.featureIdentifier: kFractionsType,
                UIFontDescriptor.FeatureKey.typeIdentifier: kDiagonalFractionsSelector,
                ],
        ]
    ] )

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 100))

label.font = UIFont(descriptor: fractionFontDesc, size:pointSize)
label.text = "12/48" // note just plain numbers and a regular slash

只要在操场上点击眼睛,你就会看到美丽的分数。

Introducing the New System Fonts (WWDC 2015 at 20:24)