字体 0.4.1

Font 0.4.1

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最后发布2019年1月
SPM支持SPM

Adam YanalunasAdam Yanalunas维护。



Font 0.4.1

  • 作者
  • Adam Yanalunas

Font

CI Status Version License Platform

Demo

您可以通过命令行中的pod try Font或克隆仓库后从Example目录运行pod install并加载Example工作区来运行Demo。

Swift 4.0

Font已准备好支持Swift 4.2。如果您需要构建Swift先前版本的版本,请尝试swift3swift2.3分支。虽然这些分支可用,但它们将不会接收更新,除非偶尔进行错误修复。

4.0版本所需的修改

  • fromUIFont已被删除。在版本1.0发布带有全新API之前,将不会有替代方案。

示例

第一步,您需要创建一个字体扩展以适当描述您自定义字体如何响应不同的粗细和样式。这里是一个Adobe《源无衬线》字体(《Source Sans Pro`_)的例子。请注意,`SourceSansPro`函数的名称和签名为虚构。您可以命名为`StandardFont`或`BodyFont`。您可以省略斜体或尺寸选项。完全取决于您和您字体的灵活性。

extension Font {
    private static func sourceSansProWeight(weight:FontWeight) -> String {
        switch weight {
        case .Ultralight:
            return "ExtraLight"

        case .Thin:
            fallthrough
        case .Light:
            return "Light"

        case .Regular:
            fallthrough
        case .Medium:
            return "Regular"

        case .Semibold:
            return "Semibold"

        case .Heavy:
            return "Bold"

        case .Black:
            return "Black"
        }
    }

    private static func name(weight:FontWeight, style:FontStyle) -> String {
        let base = "SourceSansPro"
        let weightNumber = sourceSansProWeight(weight)

        let weightAndStyle:String
        switch style {
        case _ where style == .Italic && (weight == .Regular || weight == .Medium):
            weightAndStyle = "It"
        case .Italic:
            weightAndStyle = "\(weightNumber)It"
        default:
            weightAndStyle = weightNumber
        }

        return "\(base)-\(weightAndStyle)"
    }

    static func SourceSansPro(size:CGFloat = 16, weight:FontWeight = .Medium, style:FontStyle = .None) -> Font {
        let fontName = name(weight, style:style)
        return Font(fontName: fontName, size: size)
    }
}

太好了!现在使用您的自定义字体。创建一个字体实例,生成一个`UIFont`并分配给某个`UILabel`。然后配置您的视图控制器(或其他)以关注用户何时更改动态字体大小。您无需担心用户选择的字体大小,字体会处理好这一点。

class ViewController: UIViewController {

    @IBOutlet weak var exampleLabel:UILabel!
    var headlineFont:Font!

    override func viewDidLoad() {
        super.viewDidLoad()

        headlineFont = Font.SourceSansPro(36, style: .Italic)
        exampleLabel.font = headlineFont.generate()
    }

    extension ViewController: DynamicTypeListener {
        // Subscribe to UIContentSizeCategoryDidChangeNotification notifications
        override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)

            listenForDynamicTypeChanges()
        }

        // Unsubscribe from UIContentSizeCategoryDidChangeNotification notifications
        override func viewWillDisappear(animated: Bool) {
            super.viewWillDisappear(animated)

            ignoreDynamicTypeChanges()
        }

        // Do something when UIContentSizeCategoryDidChangeNotification notifications come in
        func respondToDynamicTypeChanges(notification:NSNotification) {
            exampleLabel.font = headlineFont.generate()
        }
    }

自定义字体大小

想对用户设置的动态字体确切点大小有更精细的控制吗?定义自己的解析器,并在创建`UIFont`实例时将其传递给`generate()`函数。


    func restrainedSizes(sizeClass:String) -> CGFloat {
        let adjustedSize:CGFloat

        switch sizeClass {
        case UIContentSizeCategoryExtraSmall:
            fallthrough
        case UIContentSizeCategorySmall:
            fallthrough
        case UIContentSizeCategoryMedium:
            fallthrough
        case UIContentSizeCategoryLarge:
            adjustedSize = size //16
        case UIContentSizeCategoryExtraLarge:
            adjustedSize = floor(size * 1.15) //18
        case UIContentSizeCategoryExtraExtraLarge:
            adjustedSize = floor(size * 1.25) //20
        case UIContentSizeCategoryExtraExtraExtraLarge:
            adjustedSize = floor(size * 1.4) //22
        case UIContentSizeCategoryAccessibilityMedium:
            fallthrough
        case UIContentSizeCategoryAccessibilityLarge:
            fallthrough
        case UIContentSizeCategoryAccessibilityExtraLarge:
            fallthrough
        case UIContentSizeCategoryAccessibilityExtraExtraLarge:
            fallthrough
        case UIContentSizeCategoryAccessibilityExtraExtraExtraLarge:
            adjustedSize = floor(size * 1.65) //26
        default:
            adjustedSize = 16
        }

        return adjustedSize
    }

    func updateFonts() {
        someLabel.font = yourFont.generate(resizer:restrainedSizes)
    }

要求

  • Swift
  • 受许可的字体

安装

字体通过CocoaPods提供。安装它,只需将以下行添加到您的Podfile中。

pod "Font"

没有生成字体?

贡献

  • 检查最新主分支,确保该功能尚未被实现或错误尚未被修复。
  • 检查问题跟踪器,确保没有人已经要求过它或者提供了贡献。
  • 从功能分支或错误修复分支中分叉项目并提交合并请求。
  • 请包含测试。这对于我们未来版本不会无意中破坏您的更改非常重要。
  • 请不要修改podspec、版本或变更日志。如果您更改这些文件,请确保它们位于独立的提交中,这样我们可以在其周围剪枝。

作者

Adam Yanalunas, [email protected]

许可协议

字体可在MIT许可下使用。更多信息请参阅LICENSE文件。