|
| 1 | +// |
| 2 | +// DynamicFonts.swift |
| 3 | +// https://github.com/ysoftware |
| 4 | +// |
| 5 | +// Created by Yaroslav Erohin on 13.12.2019. |
| 6 | +// Copyright © 2015 Yaroslav Erohin. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +public extension NSMutableAttributedString { |
| 12 | + |
| 13 | + /// Adds a string with a dynamic font of style Title1. |
| 14 | + @available(iOS 9.0, *) @discardableResult |
| 15 | + func appendTitle1(_ string:String) -> Self { |
| 16 | + return append(string, font: .preferredFont(forTextStyle: .title1)) |
| 17 | + } |
| 18 | + |
| 19 | + /// Adds a string with a dynamic font of style Title2 |
| 20 | + @available(iOS 9.0, *) @discardableResult |
| 21 | + func appendTitle2(_ string: String) -> Self { |
| 22 | + return append(string, font: .preferredFont(forTextStyle: .title2)) |
| 23 | + } |
| 24 | + |
| 25 | + /// Adds a string with a dynamic font of style Title3 |
| 26 | + @available(iOS 9.0, *) @discardableResult |
| 27 | + func appendTitle3(_ string: String) -> Self { |
| 28 | + return append(string, font: .preferredFont(forTextStyle: .title3)) |
| 29 | + } |
| 30 | + |
| 31 | + /// Adds a string with a dynamic font of style Callout |
| 32 | + @available(iOS 9.0, *) @discardableResult |
| 33 | + func appendCallout(_ string: String) -> Self { |
| 34 | + return append(string, font: .preferredFont(forTextStyle: .callout)) |
| 35 | + } |
| 36 | + |
| 37 | + /// Adds a string with a dynamic font of style Subheadline |
| 38 | + @discardableResult |
| 39 | + func appendSubheadline(_ string: String) -> Self { |
| 40 | + return append(string, font: .preferredFont(forTextStyle: .subheadline)) |
| 41 | + } |
| 42 | + |
| 43 | + /// Adds a string with a dynamic font of style Caption1 |
| 44 | + @discardableResult |
| 45 | + func appendCaption1(_ string: String) -> Self { |
| 46 | + return append(string, font: .preferredFont(forTextStyle: .caption1)) |
| 47 | + } |
| 48 | + |
| 49 | + /// Adds a string with a dynamic font of style Caption2 |
| 50 | + @discardableResult |
| 51 | + func appendCaption2(_ string: String) -> Self { |
| 52 | + return append(string, font: .preferredFont(forTextStyle: .caption2)) |
| 53 | + } |
| 54 | + |
| 55 | + /// Adds a string with a dynamic font of style Headline |
| 56 | + @discardableResult |
| 57 | + func appendHeadline(_ string: String) -> Self { |
| 58 | + return append(string, font: .preferredFont(forTextStyle: .headline)) |
| 59 | + } |
| 60 | + |
| 61 | + /// Adds a string with a dynamic font of style Footnote |
| 62 | + @discardableResult |
| 63 | + func appendFootnote(_ string: String) -> Self { |
| 64 | + return append(string, font: .preferredFont(forTextStyle: .footnote)) |
| 65 | + } |
| 66 | + |
| 67 | + /// Adds a string with a dynamic font of style Body |
| 68 | + @discardableResult |
| 69 | + func appendBody(_ string: String) -> Self { |
| 70 | + return append(string, font: .preferredFont(forTextStyle: .body)) |
| 71 | + } |
| 72 | + |
| 73 | + /// Adds a string with a dynamic font of style Title1 or uses the fallback option if not available |
| 74 | + @discardableResult |
| 75 | + func appendTitle1(_ string: String, |
| 76 | + fallbackToStyle fallback: UIFont.TextStyle? = nil) -> Self { |
| 77 | + if #available(iOS 9.0, *) { return appendTitle1(string) } |
| 78 | + else { return append(string, style: fallback) } |
| 79 | + } |
| 80 | + |
| 81 | + /// Adds a string with a dynamic font of style Title2 or uses the fallback option if not available |
| 82 | + @discardableResult |
| 83 | + func appendTitle2(_ string: String, |
| 84 | + fallbackToStyle fallback: UIFont.TextStyle? = nil) -> Self { |
| 85 | + if #available(iOS 9.0, *) { return appendTitle2(string) } |
| 86 | + else { return append(string, style: fallback) } |
| 87 | + } |
| 88 | + |
| 89 | + /// Adds a string with a dynamic font of style Title3 or uses the fallback option if not available |
| 90 | + @discardableResult |
| 91 | + func appendTitle3(_ string: String, |
| 92 | + fallbackToStyle fallback: UIFont.TextStyle? = nil) -> Self { |
| 93 | + if #available(iOS 9.0, *) { return appendTitle3(string) } |
| 94 | + else { return append(string, style: fallback) } |
| 95 | + } |
| 96 | + |
| 97 | + /// Adds a string with a dynamic font of style Callout |
| 98 | + @discardableResult |
| 99 | + func appendCallout(_ string: String, |
| 100 | + fallbackToStyle fallback: UIFont.TextStyle? = nil) -> Self { |
| 101 | + if #available(iOS 9.0, *) { return appendCallout(string) } |
| 102 | + else { return append(string, style: fallback) } |
| 103 | + } |
| 104 | +} |
0 commit comments