Guest User

Untitled

a guest
Jan 9th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. //: Playground - noun: a place where people can play
  2.  
  3. import UIKit
  4. import CoreGraphics
  5.  
  6. import UIKit
  7.  
  8. let pointSize : CGFloat = 60.0
  9. let systemFontDesc = UIFont.systemFont(ofSize: pointSize,
  10. weight: UIFont.Weight.light).fontDescriptor
  11. let fractionFontDesc = systemFontDesc.addingAttributes(
  12. [
  13. UIFontDescriptor.AttributeName.featureSettings: [
  14. [
  15. UIFontDescriptor.FeatureKey.featureIdentifier: kFractionsType,
  16. UIFontDescriptor.FeatureKey.typeIdentifier: kDiagonalFractionsSelector,
  17. ],
  18. ]
  19. ] )
  20.  
  21. let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
  22.  
  23. label.font = UIFont(descriptor: fractionFontDesc, size:pointSize)
  24. label.text = "12/48" // note just plain numbers and a regular slash
  25.  
  26. enum Fraction: Double {
  27. case Eighth = 0.125
  28. case Quarter = 0.25
  29. case Third = 0.333333333333333
  30. case Half = 0.5
  31. case TwoThirds = 0.666666666666667
  32. case ThreeQuarters = 0.75
  33. }
  34.  
  35. func localizedStringFromFraction(fraction: Fraction) -> String {
  36. switch fraction {
  37. case .Eighth:
  38. return NSLocalizedString("u{215B}", comment: "Fraction - 1/8")
  39. case .Quarter:
  40. return NSLocalizedString("u{00BC}", comment: "Fraction - 1/4")
  41. case .Third:
  42. return NSLocalizedString("u{2153}", comment: "Fraction - 1/3")
  43. case .Half:
  44. return NSLocalizedString("u{00BD}", comment: "Fraction - 1/2")
  45. case .TwoThirds:
  46. return NSLocalizedString("u{2154}", comment: "Fraction - 2/3")
  47. case .ThreeQuarters:
  48. return NSLocalizedString("u{00BE}", comment: "Fraction - 3/4")
  49. }
  50. }
  51.  
  52. let superscriptDigits = Array("⁰¹²³⁴⁵⁶⁷⁸⁹".characters)
  53. let subscriptDigits = Array("₀₁₂₃₄₅₆₇₈₉".characters)
  54.  
  55. func vulgarFractionWithNumerator(numerator: UInt, denominator: UInt) -> String {
  56. let zeroBias = UnicodeScalar("0").value
  57. let supers = "(numerator)".unicodeScalars.map { superscriptDigits[Int($0.value - zeroBias)] }
  58. let subs = "(denominator)".unicodeScalars.map { subscriptDigits[Int($0.value - zeroBias)] }
  59. return String(supers + [ "⁄" ] + subs)
  60. }
  61.  
  62. vulgarFractionWithNumerator(123, denominator: 45678)
  63.  
  64. enum ScriptType {
  65. case Superscript
  66. case Subscript
  67. }
  68.  
  69. func createSuperOrSubscriptDigit(character:Character, type:ScriptType) -> Character {
  70. switch character {
  71. case "0": return type == .Superscript ? "u{2070}" : "u{2080}"
  72. case "1": return type == .Superscript ? "u{00b9}" : "u{2081}"
  73. case "2": return type == .Superscript ? "u{00b2}" : "u{2082}"
  74. case "3": return type == .Superscript ? "u{00b3}" : "u{2083}"
  75. case "4": return type == .Superscript ? "u{2074}" : "u{2084}"
  76. case "5": return type == .Superscript ? "u{2075}" : "u{2085}"
  77. case "6": return type == .Superscript ? "u{2076}" : "u{2086}"
  78. case "7": return type == .Superscript ? "u{2077}" : "u{2087}"
  79. case "8": return type == .Superscript ? "u{2078}" : "u{2088}"
  80. case "9": return type == .Superscript ? "u{2079}" : "u{2089}"
  81. default: return character
  82. }
  83. }
  84.  
  85. func createSuperOrSubscriptDigits(string:String, type:ScriptType) -> String {
  86. return String(string.characters.map() { createSuperOrSubscriptDigit($0, type: type) })
  87. }
  88.  
  89. extension String {
  90. func createSuperscriptDigits() -> String {
  91. return createSuperOrSubscriptDigits(self, type: .Superscript)
  92. }
  93. func createSubscriptDigits() -> String {
  94. return createSuperOrSubscriptDigits(self, type: .Subscript)
  95. }
  96. }
  97.  
  98. func fractionString(numerator:String, denominator:String) -> String {
  99. return numerator.createSuperscriptDigits() + "u{2044}" + denominator.createSubscriptDigits()
  100. }
Add Comment
Please, Sign In to add comment