Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //: Playground - noun: a place where people can play
- import UIKit
- import CoreGraphics
- import UIKit
- 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
- 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")
- }
- }
- 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)
- 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()
- }
Add Comment
Please, Sign In to add comment