gpt4 book ai didi

swift - 如何使用 prefersLargeTitles 更改所有权注册表?

转载 作者:可可西里 更新时间:2023-11-01 00:27:45 25 4
gpt4 key购买 nike

我知道可以使用 prefersLargeTitles 为 «large» 和 «small» 标题分别设置字体系列、字体大小和颜色。

问题是:导航 Controller 是否有任何选项可以在打开的导航面板中以大写字母显示 «large title»?

enter image description here

现在我使用自定义导航 Controller :

class MyNavigationController: UINavigationController {

public var titleSaved: String?

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

guard let topItem = navigationBar.topItem else {
return
}

if navigationBar.frame.size.height > 60 {
topItem.title = topItem.title?.uppercased()
} else {
if let titleSaved = titleSaved {
topItem.title = titleSaved
} else {
topItem.title = topItem.title?.applyingTransform(StringTransform(rawValue: "Title"), reverse: false)
}
}
}
}

从 View Controlle 设置标题:

class MyViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

navigationController?.navigationBar.prefersLargeTitles = true

let title = "Sign In"
navigationItem.title = title

if let nc = navigationController as? MyNavigationController {
nc.titleSaved = title
}
}

}

这个解决方案有效,但是当你从“大”标题切换到“小”标题并向后切换时,它会抽动并且看起来很糟糕

最佳答案

您可以使用 Small caps fonts 为 «large title» 设置大写标题,为 «small title» 设置大写标题

titleTextAttributes改成其他字体,将largeTitleTextAttributes改成caps字体

class ViewController: UITableViewController {

override func viewDidLoad() {
super.viewDidLoad()

self.title = "Sign In"
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
self.navigationController?.navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.red,
.font:UIFont(name: "Panton-LightCaps", size: 30)!]
}
}

或者您可以自定义您的字体。我在 http://www.glyphrstudio.com/online/ 中使用 OpenSans 创建了一种新样式字体

可以下载here

self.title = "Sign In"
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.titleTextAttributes = [.font:UIFont(name: "OpenSans-Regular", size: 30)!]
self.navigationController?.navigationBar.largeTitleTextAttributes = [.font:UIFont(name: "MyFontRegular", size: 30)!]

enter image description here

关于swift - 如何使用 prefersLargeTitles 更改所有权注册表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55370707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com