gpt4 book ai didi

ios - 如何在UINavigationBar中自定义导航栏标题字体和大小?

转载 作者:行者123 更新时间:2023-11-30 10:31:50 24 4
gpt4 key购买 nike

我只想自定义导航栏标题的特定部分。

例如,“导航栏标题”。我把这句话当成了标题。

在“导航”的情况下,应用蓝色和系统粗体字体,对于“栏标题”。我想应用红色和系统常规字体。

这怎么可能?

最佳答案

您应该使用带有属性标题的 UILabel 作为导航栏的自定义标题 View 。

let titleLabel = UILabel()

//attributes for the first part of the string
let firstAttr: [NSAttributedString.Key: Any] = [.font: UIFont.boldSystemFont(ofSize: 16),
.foregroundColor: UIColor.blue]

//attributes for the second part of the string
let secondAttr: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 16),
.foregroundColor: UIColor.red]

//initializing and merging the two parts
let attrString = NSMutableAttributedString(string: "Navigation", attributes: firstAttr)
let secondAttrString = NSAttributedString(string: " Bar Title", attributes: secondAttr)
attrString.append(secondAttrString)

//setting the attributed string as an attributed text
titleLabel.attributedText = attrString

//set the size of the label to fit its contents
titleLabel.sizeToFit()

//setting the label as the title view of the navigation bar
navigationItem.titleView = titleLabel

结果:

navigation bar with custom title view

关于ios - 如何在UINavigationBar中自定义导航栏标题字体和大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59019420/

24 4 0