自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

聊聊 iOS16 中的三種新字體寬度樣式

移動(dòng)開(kāi)發(fā) iOS
在 Xcode 14.1 中,SwiftUI 提供了兩個(gè)新的 API 設(shè)置這種新的寬度樣式。 width(_:) 和 fontWidth(_:)。

前言

在 iOS 16 中,Apple 引入了三種新的寬度樣式字體到 SF 字體庫(kù)。

  1. Compressed
  2. Condensed
  3. Expend

圖片

UIFont.Width

Apple 引入了新的結(jié)構(gòu)體 UIFont.Width,這代表了一種新的寬度樣式。

目前已有的四種樣式。

  • standard:我們總是使用的默認(rèn)寬度。
  • compressed:最窄的寬度樣式。
  • condensed:介于壓縮和標(biāo)準(zhǔn)之間的寬度樣式。
  • expanded:最寬的寬度樣式。

圖片

SF 字體和新的寬度樣式

如何將 SF 字體和新的寬度樣式一起使用

為了使用新的寬度樣式,Apple 有一個(gè)新的 UIFont 的類(lèi)方法來(lái)接收新的 UIFont.Width 。

class UIFont : NSObject {
class func systemFont(
ofSize fontSize: CGFloat,
weight: UIFont.Weight,
width: UIFont.Width
) -> UIFont
}

你可以像平常創(chuàng)建字體那樣來(lái)使用新的方法。

let condensed = UIFont.systemFont(ofSize: 46, weight: .bold, width: .condensed)
let compressed = UIFont.systemFont(ofSize: 46, weight: .bold, width: .compressed)
let standard = UIFont.systemFont(ofSize: 46, weight: .bold, width: .standard)
let expanded = UIFont.systemFont(ofSize: 46, weight: .bold, width: .expanded)

SwiftUI

更新:在 Xcode 14.1 中,SwiftUI 提供了兩個(gè)新的 API 設(shè)置這種新的寬度樣式。 width(_:) 和 fontWidth(_:)。

目前(Xcode 16 beta 6),這種新的寬度樣式和初始值設(shè)定只能在 UIKit 中使用,幸運(yùn)的是,我們可以在 SwiftUI 中輕松的使用它。

有很多種方法可以將 UIKit 集成到 SwiftUI 。我將會(huì)展示在 SwiftUI 中使用新寬度樣式的兩種方法。

  1. 將 UIfont 轉(zhuǎn)為 Font。
  2. 創(chuàng)建 Font 擴(kuò)展。

將 UIfont 轉(zhuǎn)為 Font

我們從 在 SwiftUI 中如何將 UIFont 轉(zhuǎn)換為 Font[1] 中了解到,F(xiàn)ont 有初始化方法可以接收 UIFont 作為參數(shù)。

步驟如下

  1. 你需要?jiǎng)?chuàng)建一個(gè)帶有新寬度樣式的 UIFont。
  2. 使用該 UIFont 創(chuàng)建一個(gè) Font 。
  3. 然后像普通 Font 一樣使用它們。

struct NewFontExample: View {
// 1
let condensed = UIFont.systemFont(ofSize: 46, weight: .bold, width: .condensed)
let compressed = UIFont.systemFont(ofSize: 46, weight: .bold, width: .compressed)
let standard = UIFont.systemFont(ofSize: 46, weight: .bold, width: .standard)
let expanded = UIFont.systemFont(ofSize: 46, weight: .bold, width: .expanded)

var body: some View {
VStack {
// 2
Text("Compressed")
.font(Font(compressed))
Text("Condensed")
.font(Font(condensed))
Text("Standard")
.font(Font(standard))
Text("Expanded")
.font(Font(expanded))
}
}
}

  • 創(chuàng)建帶有新寬度樣式的 UIFont。
  • 用 UIFont 初始化 Font, 然后傳遞給 .font 修改。

創(chuàng)建一個(gè) Font 擴(kuò)展

這種方法實(shí)際上和將 UIfont 轉(zhuǎn)為 Font 是同一種方法。我們只需要?jiǎng)?chuàng)建一個(gè)新的 Font 擴(kuò)展在 SwiftUI 中使用起來(lái)更容易一些。

extension Font {
public static func system(
size: CGFloat,
weight: UIFont.Weight,
width: UIFont.Width) -> Font
{
// 1
return Font(
UIFont.systemFont(
ofSize: size,
weight: weight,
width: width)
)
}
}

創(chuàng)建一個(gè)靜態(tài)函數(shù)傳遞 UIFont 需要的參數(shù)。然后,初始化 UIFont 和創(chuàng)建 Font 。

我們就可以像這樣使用了。

Text("Compressed")
.font(.system(size: 46, weight: .bold, width: .compressed))
Text("Condensed")
.font(.system(size: 46, weight: .bold, width: .condensed))
Text("Standard")
.font(.system(size: 46, weight: .bold, width: .standard))
Text("Expanded")
.font(.system(size: 46, weight: .bold, width: .expanded))

如何使用新的寬度樣式

你可以在你想使用的任何地方使用。不會(huì)有任何限制,所有的新寬度都有一樣的尺寸,同樣的高度,只會(huì)有寬度的變化。

這里是擁有同樣文本,同樣字體大小和同樣字體樣式的不同字體寬度樣式展示。

圖片

新的寬度樣式優(yōu)點(diǎn)

你可以使用新的寬度樣式在已經(jīng)存在的字體樣式上,比如 thin 或者 bold ,在你的 app 上創(chuàng)造出獨(dú)一無(wú)二的體驗(yàn)。

Apple 將它使用在他們的照片app ,在 "回憶'' 功能中,通過(guò)組合不同的字體寬度和樣式在標(biāo)題或者子標(biāo)題上。

圖片

這里有一些不同寬度和樣式的字體組合,希望可以激發(fā)你的靈感。

Text("Pet Friends")
.font(Font(UIFont.systemFont(ofSize: 46, weight: .light, width: .expanded)))
Text("OVER THE YEARS")
.font(Font(UIFont.systemFont(ofSize: 30, weight: .thin, width: .compressed)))

Text("Pet Friends")
.font(Font(UIFont.systemFont(ofSize: 46, weight: .black, width: .condensed)))
Text("OVER THE YEARS")
.font(Font(UIFont.systemFont(ofSize: 20, weight: .light, width: .expanded)))

圖片

你也可以用新的寬度樣式來(lái)控制文本的可讀性。

下面的這個(gè)例子,說(shuō)明不同寬度樣式如何影響每行的字符數(shù)和段落長(zhǎng)度

圖片

下載這種字體

你可以在 Apple 字體平臺(tái)[2] 來(lái)下載這種新的字體寬度樣式。

下載安裝后,你將會(huì)發(fā)現(xiàn)一種結(jié)合了現(xiàn)有寬度和新寬度樣式的新樣式。

圖片

基本上,除了在模擬器的模擬系統(tǒng) UI 中,在任何地方都被禁止使用 SF 字體。請(qǐng)確保你在使用前閱讀并理解許可證。

參考資料

[1] 在 SwiftUI 中如何將 UIFont 轉(zhuǎn)換為 Font: ??https://www.jianshu.com/p/56ee0d1ea0e1.??

[2] Apple 字體平臺(tái): ??https://developer.apple.com/fonts/.??

責(zé)任編輯:姜華 來(lái)源: Swift社區(qū)
相關(guān)推薦

2009-07-28 08:55:35

Windows 7本地化字體

2023-04-13 07:41:14

RoCE技術(shù)RDMA

2023-10-13 00:00:00

Redis模塊空間對(duì)象

2011-04-08 11:13:50

CISCO IOS令牌桶雙桶

2010-09-06 10:04:31

CSS樣式表

2011-01-18 15:35:59

jQueryJavaScriptweb

2023-06-04 17:11:13

iOSAndroidApple

2023-03-07 08:25:39

探針Kubernetes

2024-07-01 12:42:58

2009-05-07 15:02:42

OracleJoin查詢(xún)

2021-07-05 06:57:06

VMware vSph虛擬機(jī)磁盤(pán)

2011-06-03 11:53:06

Spring接口

2022-12-06 23:32:47

CSS語(yǔ)言LCH

2010-09-08 13:29:48

CSS

2010-05-11 14:08:50

MySQL數(shù)字類(lèi)型

2021-12-20 07:11:26

Java List排序 Java 基礎(chǔ)

2010-09-24 19:18:22

SQL索引

2018-03-28 16:10:23

閱讀源碼境界

2012-07-17 09:16:16

SpringSSH

2015-09-14 09:31:44

結(jié)對(duì)設(shè)計(jì)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)