Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何更改动态类型以使用不同的文字样式_Ios_Swift - Fatal编程技术网

Ios 如何更改动态类型以使用不同的文字样式

Ios 如何更改动态类型以使用不同的文字样式,ios,swift,Ios,Swift,我想对我应用程序中的所有文本使用动态类型。我使用的字体是苹果的旧金山 我不喜欢默认设置,因为它们实际上不使用Bold作为文本 下面是一个列表,其中列出了所有这些问题: Style Weight Point size Leading Tracking -------------------------------------------------------- Large Title Regular 34pt 41pt 11pt Title 1

我想对我应用程序中的所有文本使用
动态类型
。我使用的字体是苹果的旧金山

我不喜欢默认设置,因为它们实际上不使用
Bold
作为文本

下面是一个列表,其中列出了所有这些问题:

Style         Weight      Point size    Leading Tracking
--------------------------------------------------------
Large Title   Regular     34pt  41pt    11pt
Title 1       Regular     28pt  34pt    13pt
Title 2       Regular     22pt  28pt    16pt
Title 3       Regular     20pt  25pt    19pt
Headline      Semi-Bold   17pt  22pt.  -24pt
Body          Regular     17pt  22pt.  -24pt
Callout       Regular     16pt  21pt.  -20pt
Subhead       Regular     15pt  20pt.  -16pt
Footnote      Regular     13pt  18pt.  -6pt
Caption 1     Regular     12pt  16pt    0pt
Caption 2     Regular     11pt  13pt    6pt

那么,有没有一种方法可以配置文本样式的精确程度呢?

使用动态类型获得不同权重的一种方法是,除了使用
首选字体(forTextStyle:)
之外,还使用其他
UIFont
属性和方法

您可以将
pointSize
属性与
UIFont.systemFont(ofSize:weight:)
结合使用,以获得不同的权重:

let title1Font = UIFont.preferredFont(forTextStyle: .title1)
let title1PointSize = title1Font.pointSize
let boldTitle1Font = UIFont.systemFont(ofSize: title1PointSize, weight: .bold)
您可以扩展
UIFont
,以提供任何必要的便利。例如,如果需要不同权重的
title1
body
样式,则可以创建:

extension UIFont {

    static var title1: UIFont {
        return UIFont.preferredFont(forTextStyle: .title1)
    }
    static var body: UIFont {
        return UIFont.preferredFont(forTextStyle: .body)
    }

    func with(weight: UIFont.Weight) -> UIFont {
        return UIFont.systemFont(ofSize: pointSize, weight: weight)
    }

}
然后,访问各种大小和字体非常容易:

UIFont.title1.with(weight: .bold)
UIFont.body.with(weight: .semibold)
UIFont.body.with(weight: .light)
UIFont.title1 // normal weight

Swift 5

通过扩展tktsubota的答案并添加以下内容,您可以动态调整字体大小以便于访问:

let font = UIFont.largeTitle.with(weight: .bold)
yourLabel.font = UIFontMetrics.default.scaledFont(for: font)
yourLabel.adjustsFontForContentSizeCategory = true
以及tktsubota的UIFont扩展:

extension UIFont {

    static var largeTitle: UIFont {
        return UIFont.preferredFont(forTextStyle: .largeTitle)
    }

    static var body: UIFont {
        return UIFont.preferredFont(forTextStyle: .body)
    }

    func with(weight: UIFont.Weight) -> UIFont {
        return UIFont.systemFont(ofSize: pointSize, weight: weight)
    }

}

这不起作用,它破坏了可访问性,当您以编程方式设置字体时,字体不会动态调整。