diff --git a/CHANGELOG.md b/CHANGELOG.md index 6925799d2c5fd632d5cff7a0b87ee6fc51f4dc17..1cd06b418fbeb03a7f6b37a1130503f44b064b45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,18 @@ All notable changes to this project will be documented in this file. `UIFontComplete` adheres to [Semantic Versioning](http://semver.org/). ---- +--- +Draft changelog of 4.0.0 + +#### Summary + +This version adds support for SwiftUI's Font. + +## Breaking changes + +* Font renamed to BuiltInFont to avoid naming collision with SwiftUI. +* Initializing fonts with BuiltInFont now requires you to explicitly specify type: UIFont? or Font. + ## [3.0.0](https://github.com/Nirma/UIFontComplete/releases/tag/3.0.0) (01/03/2018) #### Summary diff --git a/README.md b/README.md index d99b53006604720e41d90b4fb0621cc02b1997c7..293364af774a52ef382f07aecbba391c02368ab8 100644 --- a/README.md +++ b/README.md @@ -26,16 +26,16 @@ You can now simply start typing the name of the font enum and let code completio ![](http://i.imgur.com/yBYRQVB.gif) This library currently provides two different options for creating `UIFont` objects. -The first is calling the font name off of the provided `Font` enumeration and then calling `of(size:)` +The first is calling the font name off of the provided `BuiltInFont` enumeration and then calling `of(size:)` to provide the desired size. - -```swif -let myFont = Font.helvetica.of(size: 12.0) + +```swift +let myFont: UIFont? = BuiltInFont.helvetica.of(size: 12.0) ``` The other `UIFont` creation method offered by this library is similar to the normal `UIFont` constructor except that instead of providing a `String` of the desired font, a case of the `Font` enum is provided instead. - + ```swift let font = UIFont(font: .arialBoldItalicMT, size: 12.0) ``` @@ -55,6 +55,23 @@ enum CustomFont: String, FontRepresentable { CustomFont.alexBrushRegular.of(size: 12.0) // => UIFont ``` +### SwiftUI support + +SwiftUI Font is handled in a same way as UIKit UIFont. + +You can do both: + +```swift +let myFont: Font = BuiltInFont.helvetica.of(size: 12.0) +``` + + + +```swift +let font = Font(font: .arialBoldItalicMT, size: 12.0) +``` + + ## Installation diff --git a/Sources/Font.swift b/Sources/BuiltInFont.swift similarity index 99% rename from Sources/Font.swift rename to Sources/BuiltInFont.swift index 5057cc1393697e719a555c7cf4a0a88995f12839..db3111155fbaf7aa5ccb474a15899b1739838697 100644 --- a/Sources/Font.swift +++ b/Sources/BuiltInFont.swift @@ -1,4 +1,4 @@ -// Font.swift +// BuiltInFont.swift // Copyright (c) 2016-2019 Nicholas Maccharoli // // Permission is hereby granted, free of charge, to any person obtaining a copy @@ -19,7 +19,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -public enum Font: String, FontRepresentable { +public enum BuiltInFont: String, FontRepresentable { // ======== ONLY Available in iOS 13+ ======== #if os(iOS) diff --git a/Sources/Font+Extension.swift b/Sources/Font+Extension.swift index ae0d158ba582b13a1939176a51831283da6a8325..a3afe28cde1ad2a086ff4d3bb6e5c7a8ff9b24ea 100644 --- a/Sources/Font+Extension.swift +++ b/Sources/Font+Extension.swift @@ -19,21 +19,14 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -typealias UIFontComplete_Font = Font - import CoreGraphics import SwiftUI import UIKit -@available(iOS 13.0, *) -extension SwiftUI.Font { +@available(tvOS 13.0, iOS 13.0, *) +extension Font { /// Create a UIFont object with a `Font` enum - init?(font: UIFontComplete_Font, size: CGFloat) { - let fontIdentifier: String = font.rawValue - if let uiFont = UIFont(name: fontIdentifier, size: size) { - self.init(uiFont) - } else { - return nil - } + init(font: BuiltInFont, size: CGFloat) { + self = Font.custom(font.rawValue, size: size) } } diff --git a/Sources/FontRepresentable.swift b/Sources/FontRepresentable.swift index f51acb545820473872a8e44cbb3f8addf8c58313..08dd0c3b0d51903a11af9828a8a55ca5979b2747 100644 --- a/Sources/FontRepresentable.swift +++ b/Sources/FontRepresentable.swift @@ -19,12 +19,13 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. +import SwiftUI import UIKit public protocol FontRepresentable: RawRepresentable {} extension FontRepresentable where Self.RawValue == String { - /// An alternative way to get a particular `UIFont` instance from a `Font` + /// An alternative way to get a particular `UIFont` instance from a `BuiltInFont` /// value. /// /// - parameter of size: The desired size of the font. @@ -38,4 +39,14 @@ extension FontRepresentable where Self.RawValue == String { public func of(size: Double) -> UIFont? { return UIFont(name: rawValue, size: CGFloat(size)) } + + @available(tvOS 13.0, iOS 13.0, *) + public func of(size: CGFloat) -> Font { + return Font.custom(rawValue, size: size) + } + + @available(tvOS 13.0, iOS 13.0, *) + public func of(size: Double) -> Font { + return Font.custom(rawValue, size: CGFloat(size)) + } } diff --git a/Sources/UIFont+Extension.swift b/Sources/UIFont+Extension.swift index 4f6c3f5f05b5f6c91efdfbb0701afd913c7ce07d..ca8e24707402febcc7a6c19dbce981daa34cc58d 100644 --- a/Sources/UIFont+Extension.swift +++ b/Sources/UIFont+Extension.swift @@ -1,4 +1,4 @@ -// Font+Extension.swift +// UIFont+Extension.swift // Copyright (c) 2016-2019 Nicholas Maccharoli // // Permission is hereby granted, free of charge, to any person obtaining a copy @@ -23,7 +23,7 @@ import UIKit extension UIFont { /// Create a UIFont object with a `Font` enum - public convenience init?(font: Font, size: CGFloat) { + public convenience init?(font: BuiltInFont, size: CGFloat) { let fontIdentifier: String = font.rawValue self.init(name: fontIdentifier, size: size) } diff --git a/Tests/UIFontCompleteTests.swift b/Tests/UIFontCompleteTests.swift index 1e7778bd894babb1fdcbc63f257788c17c230cae..1dcfcedb131514801a10e43dd857763bda5036b3 100644 --- a/Tests/UIFontCompleteTests.swift +++ b/Tests/UIFontCompleteTests.swift @@ -1086,11 +1086,11 @@ class UIFontCompleteTests: XCTestCase { func testOfSizeCGFloat() { let testValue: CGFloat = 12.0 - XCTAssertNotNil(Font.helvetica.of(size: testValue)) + XCTAssertNotNil(BuiltInFont.helvetica.of(size: testValue)) } func testOfSizeDouble() { let testValue: Double = 12.0 - XCTAssertNotNil(Font.helvetica.of(size: testValue)) + XCTAssertNotNil(BuiltInFont.helvetica.of(size: testValue)) } } diff --git a/UIFontComplete.xcodeproj/project.pbxproj b/UIFontComplete.xcodeproj/project.pbxproj index 6dde433d6f9d62ff41bbb16868cd7a9ebcd260de..90b1495d03231e4b962c8594f5baa6e08ca8386f 100644 --- a/UIFontComplete.xcodeproj/project.pbxproj +++ b/UIFontComplete.xcodeproj/project.pbxproj @@ -10,12 +10,12 @@ 4E4C4E8A1FFA0D9400058722 /* UIFontComplete.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4E4C4E811FFA0D9400058722 /* UIFontComplete.framework */; }; 4E4C4E8F1FFA0D9400058722 /* UIFontComplete_tvOSTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E4C4E8E1FFA0D9400058722 /* UIFontComplete_tvOSTests.swift */; }; 4E4C4E911FFA0D9400058722 /* UIFontComplete_tvOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 4E4C4E831FFA0D9400058722 /* UIFontComplete_tvOS.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 4E4C4E981FFA7CDE00058722 /* Font.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA813B551F0092450096ABBD /* Font.swift */; }; + 4E4C4E981FFA7CDE00058722 /* BuiltInFont.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA813B551F0092450096ABBD /* BuiltInFont.swift */; }; 4E4C4E991FFA7CE500058722 /* FontRepresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA813B561F0092450096ABBD /* FontRepresentable.swift */; }; 4E6A41E11FFA942300DC7D56 /* UIFont+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA813B571F0092450096ABBD /* UIFont+Extension.swift */; }; 4E6A41F01FFA95CF00DC7D56 /* UIFontComplete.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4E6A41E71FFA95CF00DC7D56 /* UIFontComplete.framework */; }; 4E6A41F71FFA95CF00DC7D56 /* UIFontComplete_iOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 4E6A41E91FFA95CF00DC7D56 /* UIFontComplete_iOS.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 4E6A41FE1FFA967900DC7D56 /* Font.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA813B551F0092450096ABBD /* Font.swift */; }; + 4E6A41FE1FFA967900DC7D56 /* BuiltInFont.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA813B551F0092450096ABBD /* BuiltInFont.swift */; }; 4E6A41FF1FFA967D00DC7D56 /* FontRepresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA813B561F0092450096ABBD /* FontRepresentable.swift */; }; 4E6A42001FFA968100DC7D56 /* UIFont+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA813B571F0092450096ABBD /* UIFont+Extension.swift */; }; 4E6A42031FFA96D700DC7D56 /* UIFontCompleteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAED536B1F53EEA7009C874C /* UIFontCompleteTests.swift */; }; @@ -52,7 +52,7 @@ 4E6A41EA1FFA95CF00DC7D56 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 4E6A41EF1FFA95CF00DC7D56 /* UIFontComplete-iOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "UIFontComplete-iOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 4E6A41F61FFA95CF00DC7D56 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - AA813B551F0092450096ABBD /* Font.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Font.swift; path = Sources/Font.swift; sourceTree = SOURCE_ROOT; }; + AA813B551F0092450096ABBD /* BuiltInFont.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = BuiltInFont.swift; path = Sources/BuiltInFont.swift; sourceTree = SOURCE_ROOT; }; AA813B561F0092450096ABBD /* FontRepresentable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FontRepresentable.swift; path = Sources/FontRepresentable.swift; sourceTree = SOURCE_ROOT; }; AA813B571F0092450096ABBD /* UIFont+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "UIFont+Extension.swift"; path = "Sources/UIFont+Extension.swift"; sourceTree = SOURCE_ROOT; }; AAE1A2A31E02618D00610C40 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -97,7 +97,7 @@ 136122C41EB2AD2A009F45E4 /* Sources */ = { isa = PBXGroup; children = ( - AA813B551F0092450096ABBD /* Font.swift */, + AA813B551F0092450096ABBD /* BuiltInFont.swift */, AA813B561F0092450096ABBD /* FontRepresentable.swift */, ); name = Sources; @@ -370,7 +370,7 @@ buildActionMask = 2147483647; files = ( 4E6A41E11FFA942300DC7D56 /* UIFont+Extension.swift in Sources */, - 4E4C4E981FFA7CDE00058722 /* Font.swift in Sources */, + 4E4C4E981FFA7CDE00058722 /* BuiltInFont.swift in Sources */, C6EAE3222784A32C00315B5C /* Font+Extension.swift in Sources */, 4E4C4E991FFA7CE500058722 /* FontRepresentable.swift in Sources */, ); @@ -389,7 +389,7 @@ buildActionMask = 2147483647; files = ( 4E6A42001FFA968100DC7D56 /* UIFont+Extension.swift in Sources */, - 4E6A41FE1FFA967900DC7D56 /* Font.swift in Sources */, + 4E6A41FE1FFA967900DC7D56 /* BuiltInFont.swift in Sources */, C6EAE3212784A32C00315B5C /* Font+Extension.swift in Sources */, 4E6A41FF1FFA967D00DC7D56 /* FontRepresentable.swift in Sources */, );