Arch
Arch

Reputation: 127

How to display fraction text number over another?

I need to display a fraction in my app but cannot find a good way to do it?

Should looks something like this

enter image description here(proof of concept... don't need the font):

There is other posts similar to this but they are in ObJ C, I can not find a reliable solution in swift

Upvotes: 2

Views: 3654

Answers (5)

David W. Keith
David W. Keith

Reputation: 2254

I had the same issue and wrote FractionFormatter, and extension of NumberFormatter, to solve this issue.

let fractionFormatter = FractionFormatter()
fractionFormatter.string(from: 1.5) // "1 ½"

Upvotes: 0

Glenn Howes
Glenn Howes

Reputation: 5597

This is just Apple sample code from the Introducing the New System Fonts video from WWDC 2015 put into a playground and using a UILabel to render plain text fractions using font features. [Updated for 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

Just tap on the eye in the playground and you will see a beautiful fraction.

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

Upvotes: 10

user887210
user887210

Reputation:

Here is a Swift solution for arbitrary fractions:

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

Upvotes: 0

rob mayoff
rob mayoff

Reputation: 385500

@KennethBruno's answer works but I find it a little inelegant. Here's my version:

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)

Result:

"¹²³⁄₄₅₆₇₈"

Upvotes: 2

tebs1200
tebs1200

Reputation: 1195

I had to do something similar in an App. I created a mapping between common fractions and the related unicode characters like so:

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")
    }
}

If you need support for more fractions, you can find the mapping here.

Upvotes: 5

Related Questions