swift - 如何在 iOS 11 中换行较长的大标题?

标签 swift navbar title ios11 xcode9

我正在尝试使用 Swift 在 iOS 11 中使用新的大标题系统。当标题太长时(参见图片示例),它会添加 ... 而不是换行或缩小文本大小。如何添加换行符?

Example image with long title

这是我用来设置标题的一些代码:

self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: MyGlobalVariable.themeMainColor]
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 22)]
navigationItem.title = "Search by Name"

最佳答案

试试这个:

for navItem in (self.navigationController?.navigationBar.subviews)! {
    for itemSubView in navItem.subviews {
        if let largeLabel = itemSubView as? UILabel {
            largeLabel.text = self.title
            largeLabel.numberOfLines = 0
            largeLabel.lineBreakMode = .byWordWrapping
        }
    }
}

它对我有用。

enter image description here

关于swift - 如何在 iOS 11 中换行较长的大标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46438881/

相关文章:

templates - 如何使用带有 latex 模板的 pandoc 指定标题颜色?

objective-c - Xcode 符号导航器不显示应用程序符号

ios - 读取 BLE 外设特性并检查其值?

Swift 3 - UIImageWriteToSavedPhotosAlbum 内存问题

css - 在保留样式的同时居中 Bootstrap 导航栏

Joomla 3.3.4 中的浏览器页面标题

swift - 如何在不进入应用程序的情况下从快速操作菜单复制到 UIPasteBoard?

r - Shiny :动态更改选项卡名称

html - 导航栏 : align inline text, 左右

html - 抑制链接标题