如何显示分数文本数量?

时间:2016-02-05 01:03:27

标签: swift fractions

我需要在我的应用中显示一小部分,但找不到一个好方法吗?

应该看起来像这样

enter image description here(概念证明......不需要字体):

还有其他类似的帖子,但它们都在ObJ C中,我无法在swift中找到可靠的解决方案

4 个答案:

答案 0 :(得分:8)

这只是将来自WWDC 2015的新系统字体介绍视频中的Apple示例代码放入游乐场并使用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)

答案 1 :(得分:4)

我必须在App中做类似的事情。我在公共分数和相关的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

答案 2 :(得分:0)

以下是任意分数的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($0, 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()
}

答案 3 :(得分:0)

@ 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($0.value - zeroBias)] }
    let subs = "\(denominator)".unicodeScalars.map { subscriptDigits[Int($0.value - zeroBias)] }
    return String(supers + [ "⁄" ] + subs)
}

vulgarFractionWithNumerator(123, denominator: 45678)

结果:

  

“¹²³/₄₅₆₇₈”