SwiftyMarkdown+iOS.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // SwiftyMarkdown+macOS.swift
  3. // SwiftyMarkdown
  4. //
  5. // Created by Simon Fairbairn on 17/12/2019.
  6. // Copyright © 2019 Voyage Travel Apps. All rights reserved.
  7. //
  8. import Foundation
  9. #if !os(macOS)
  10. import UIKit
  11. extension SwiftyMarkdown {
  12. func font( for line : SwiftyLine, characterOverride : CharacterStyle? = nil ) -> UIFont {
  13. let textStyle : UIFont.TextStyle
  14. var fontName : String?
  15. var fontSize : CGFloat?
  16. var globalBold = false
  17. var globalItalic = false
  18. let style : FontProperties
  19. // What type are we and is there a font name set?
  20. switch line.lineStyle as! MarkdownLineStyle {
  21. case .h1:
  22. style = self.h1
  23. if #available(iOS 9, *) {
  24. textStyle = UIFont.TextStyle.title1
  25. } else {
  26. textStyle = UIFont.TextStyle.headline
  27. }
  28. case .h2:
  29. style = self.h2
  30. if #available(iOS 9, *) {
  31. textStyle = UIFont.TextStyle.title2
  32. } else {
  33. textStyle = UIFont.TextStyle.headline
  34. }
  35. case .h3:
  36. style = self.h3
  37. if #available(iOS 9, *) {
  38. textStyle = UIFont.TextStyle.title2
  39. } else {
  40. textStyle = UIFont.TextStyle.subheadline
  41. }
  42. case .h4:
  43. style = self.h4
  44. textStyle = UIFont.TextStyle.headline
  45. case .h5:
  46. style = self.h5
  47. textStyle = UIFont.TextStyle.subheadline
  48. case .h6:
  49. style = self.h6
  50. textStyle = UIFont.TextStyle.footnote
  51. case .codeblock:
  52. style = self.code
  53. textStyle = UIFont.TextStyle.body
  54. case .blockquote:
  55. style = self.blockquotes
  56. textStyle = UIFont.TextStyle.body
  57. default:
  58. style = self.body
  59. textStyle = UIFont.TextStyle.body
  60. }
  61. fontName = style.fontName
  62. fontSize = style.fontSize
  63. switch style.fontStyle {
  64. case .bold:
  65. globalBold = true
  66. case .italic:
  67. globalItalic = true
  68. case .boldItalic:
  69. globalItalic = true
  70. globalBold = true
  71. case .normal:
  72. break
  73. }
  74. if fontName == nil {
  75. fontName = body.fontName
  76. }
  77. if let characterOverride = characterOverride {
  78. switch characterOverride {
  79. case .code:
  80. fontName = code.fontName ?? fontName
  81. fontSize = code.fontSize
  82. case .link:
  83. fontName = link.fontName ?? fontName
  84. fontSize = link.fontSize
  85. case .bold:
  86. fontName = bold.fontName ?? fontName
  87. fontSize = bold.fontSize
  88. globalBold = true
  89. case .italic:
  90. fontName = italic.fontName ?? fontName
  91. fontSize = italic.fontSize
  92. globalItalic = true
  93. case .strikethrough:
  94. fontName = strikethrough.fontName ?? fontName
  95. fontSize = strikethrough.fontSize
  96. default:
  97. break
  98. }
  99. }
  100. fontSize = fontSize == 0.0 ? nil : fontSize
  101. var font : UIFont
  102. if let existentFontName = fontName {
  103. font = UIFont.preferredFont(forTextStyle: textStyle)
  104. let finalSize : CGFloat
  105. if let existentFontSize = fontSize {
  106. finalSize = existentFontSize
  107. } else {
  108. let styleDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: textStyle)
  109. finalSize = styleDescriptor.fontAttributes[.size] as? CGFloat ?? CGFloat(14)
  110. }
  111. if let customFont = UIFont(name: existentFontName, size: finalSize) {
  112. let fontMetrics = UIFontMetrics(forTextStyle: textStyle)
  113. font = fontMetrics.scaledFont(for: customFont)
  114. } else {
  115. font = UIFont.preferredFont(forTextStyle: textStyle)
  116. }
  117. } else {
  118. font = UIFont.preferredFont(forTextStyle: textStyle)
  119. }
  120. if globalItalic, let italicDescriptor = font.fontDescriptor.withSymbolicTraits(.traitItalic) {
  121. font = UIFont(descriptor: italicDescriptor, size: fontSize ?? 0)
  122. }
  123. if globalBold, let boldDescriptor = font.fontDescriptor.withSymbolicTraits(.traitBold) {
  124. font = UIFont(descriptor: boldDescriptor, size: fontSize ?? 0)
  125. }
  126. return font
  127. }
  128. func color( for line : SwiftyLine ) -> UIColor {
  129. // What type are we and is there a font name set?
  130. switch line.lineStyle as! MarkdownLineStyle {
  131. case .yaml:
  132. return body.color
  133. case .h1, .previousH1:
  134. return h1.color
  135. case .h2, .previousH2:
  136. return h2.color
  137. case .h3:
  138. return h3.color
  139. case .h4:
  140. return h4.color
  141. case .h5:
  142. return h5.color
  143. case .h6:
  144. return h6.color
  145. case .body:
  146. return body.color
  147. case .codeblock:
  148. return code.color
  149. case .blockquote:
  150. return blockquotes.color
  151. case .unorderedList, .unorderedListIndentFirstOrder, .unorderedListIndentSecondOrder, .orderedList, .orderedListIndentFirstOrder, .orderedListIndentSecondOrder:
  152. return body.color
  153. case .referencedLink:
  154. return link.color
  155. }
  156. }
  157. }
  158. #endif