【问题标题】:Multiline Navigationbar Title多行导航栏标题
【发布时间】:2016-03-21 18:49:21
【问题描述】:

我正在尝试在导航栏中设置标题标签以允许多行。我有自定义导航控制器代码,我将多行代码放入其中。我知道那里的代码可以工作,但我的多行部分不工作。

let titleLabel = UILabel()
titleLabel.frame = CGRectMake(0, 0, self.navigationBar.frame.width, self.navigationBar.frame.height * 2)
titleLabel.numberOfLines = 0
titleLabel.lineBreakMode = .ByWordWrapping

navigationItem.titleView = titleLabel

但文本仍然在最后运行。我还尝试将其放入单个视图控制器本身,在 navigationItem 前面添加 self.navigationController?. 并获得相同的结果。

我的代码中是否缺少某些内容可以防止标题标签使用多行?

【问题讨论】:

  • 您的navigationBar.frame.width 的大小可能尚未适合屏幕。您可能需要覆盖 viewDidLayoutSubviews 以检测正确的帧。

标签: ios swift xcode uinavigationbar swift3


【解决方案1】:

这是一个如何创建多行导航栏标题的代码示例

let label: UILabel = UILabel(frame: CGRectMake(0, 0, 400, 50))
label.backgroundColor = UIColor.clearColor()
label.numberOfLines = 2
label.font = UIFont.boldSystemFontOfSize(16.0)
label.textAlignment = .Center
label.textColor = UIColor.whiteColor()
label.text = "This is a\nmultiline string for the navBar"
self.navigationItem.titleView = label

Swift 5.x:

let label = UILabel()
label.backgroundColor = .clear
label.numberOfLines = 2
label.font = UIFont.boldSystemFont(ofSize: 16.0)
label.textAlignment = .center
label.textColor = .white
label.text = "This is a\nmultiline string for the navBar"
self.navigationItem.titleView = label

【讨论】:

  • 需要进行一些调整才能使其表现得更像我想要的那样,但这确实有帮助。
  • 我使用navigationItem.titleView 没有明确设置框架,它工作得很好。
  • @Rashwan L 如果您定义 UILabel 的框架,它将导致标签左对齐,所以只需留下框架部分并用作“let label = UILabel()”。它将完美地工作并将其自身设置在中心对齐。
  • 为什么宽度是 400 点?
【解决方案2】:

这在故事板中是可行的。只需将 UIView 拖入导航栏,然后在文档大纲中将 UILabel 拖到其上,将行设置为 2 并将对齐方式设置为居中。

【讨论】:

    【解决方案3】:

    使用它来获得你想要的标签位置:

    let labelWidth = navBar.bounds.width - 110
    
        let label = UILabel(frame: CGRect(x:(navBar.bounds.width/2) - (labelWidth/2), y:0, width:labelWidth, height:navBar.bounds.height))
        label.backgroundColor = UIColor.clear
        label.numberOfLines = 0
        label.font = UIFont.boldSystemFont(ofSize: 13.0)
        label.textAlignment = .center
        label.textColor = UIColor.black
        label.lineBreakMode = .byWordWrapping
    
        label.text = loadedName
        navBar.topItem?.title = nil
        navBar.addSubview(label)
    

    顶行中的 110 值是您希望标签两侧的间距。

    【讨论】:

    • 关于在 swiftui 中执行此操作的任何提示?
    【解决方案4】:

    swift 5+ 非常简单的解决方案

    func titleMultiLine(topText: String, bottomText: String) {
        //            let titleParameters = [NSForegroundColorAttributeName : UIColor.white,
        //                                   NSFontAttributeName : UIFont.<Font>]
    //            let subtitleParameters = [NSForegroundColorAttributeName : UIColor.<Color>(),
        //                                      NSFontAttributeName : UIFont.<Font>]
        let titleParameters = [NSAttributedString.Key.foregroundColor : UIColor.white]
        
        let subtitleParameters = [NSAttributedString.Key.foregroundColor : UIColor.white]
        
        let title:NSMutableAttributedString = NSMutableAttributedString(string: topText, attributes: titleParameters)
        let subtitle:NSAttributedString = NSAttributedString(string: bottomText, attributes: subtitleParameters)
        
        title.append(NSAttributedString(string: "\n"))
        title.append(subtitle)
        
        let size = title.size()
        
        let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        titleLabel.attributedText = title
        titleLabel.numberOfLines = 0
        titleLabel.textAlignment = .center
        
        navigationItem.titleView = titleLabel
    }
    

    函数调用

    self.titleMultiLine(topText: "I am top text Title", bottomText: "bottom text")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      • 1970-01-01
      • 2013-12-17
      相关资源
      最近更新 更多