ios - 如何在TruncatingMiddle中设置导航标题的linebreakMode

标签 ios swift uinavigationcontroller uinavigationbar

我使用 titleTextAttributes 在 TruncatingMiddle 中设置 lineBreakmode。
但是我无法使用换行模式显示标题。
我怎么了?
谢谢。

let paragraph = NSMutableParagraphStyle()
    paragraph.lineBreakMode = .byTruncatingMiddle

self.navigationController?.navigationBar.titleTextAttributes = [
        NSFontAttributeName: defaultTitleFont ?? "" ,
        NSParagraphStyleAttributeName: paragraph ]

最佳答案

从你的问题中,我得到的是你想要在导航栏中使用多行标题,为此,你可以简单地执行以下操作:

 let label = UILabel(frame: CGRect(x:0, y:0, width:400, height:50))
          label.backgroundColor = .clear
          label.numberOfLines = 2
          label.font = UIFont.boldSystemFont(ofSize: 16.0)
          label.textAlignment = .center
          label.textColor = .black
          label.text = "This is a\nmultiline string for the navBar"
          self.navigationItem.titleView = label

我希望它能解决您的问题。

关于ios - 如何在TruncatingMiddle中设置导航标题的linebreakMode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48017313/

相关文章:

ios - 在 UINavigationController 中包装 UIViewController

ios - 应该显式调用 willMoveToParentViewController :(UIViewController *)parent & didMoveToParentViewController:(UIViewController *)parent?

ios - 如何在 iOS 10 中安装自签名证书

ios - iOS 上的 NSInteger 与 64 位架构

swift - 在 viewDidLoad 中设置 navigationBar.tintColor 的奇怪行为

arrays - Swift 闭包表达式

ios - 在 Swift 中禁用没有 UserInteractionEnabled 的单元格选择

swift - UIGesture 识别器使应用程序崩溃

iphone - 使用堆栈的导航 Controller / View Controller

ios - 我是否需要在导航 Controller subview 中使用单例 GADRequest() ?