提交 fb64494a 编写于 作者: M Mikhail Barashkov

Complete SwiftUI Font support

上级 83dd9002
......@@ -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
......
......@@ -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
......
// 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)
......
......@@ -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)
}
}
......@@ -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))
}
}
// 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)
}
......
......@@ -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))
}
}
......@@ -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 = "<group>"; };
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 = "<group>"; };
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 = "<group>"; };
......@@ -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 */,
);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册