具有多种颜色的大导航栏文本 [英] Large Navigation Bar Text With Multiple Colors

查看:45
本文介绍了具有多种颜色的大导航栏文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS 11 在导航栏中引入了较大文本的选项.我想要一个使用多种颜色的标题.例如:

iOS 11 introduces the option for larger text in the Navigation Bar. I would like to have a title that uses multiple colors. For example:

设置标题相当容易,甚至可以更改整个标题的颜色:

It's fairly easy to set the title, and even to change the color of the entire title:

[[self navigationItem] setTitle: @"Colors"];
[[[self navigationController] navigationBar] setLargeTitleTextAttributes: @{NSForegroundColorAttributeName: [UIColor colorFromHex: redColor]}];

我不知道如何更改标题的一部分.例如,一种选择范围的方法 - NSRangeMake(0, 1) - 这样我就可以为其应用颜色.

What I can't figure out is how to change just part of the title. For example, a way to select the range like this – NSRangeMake(0, 1) – so that I could apply a color to it.

这一定是可能的,对吧?

This must be possible, right?

推荐答案

没有公共 API 可以为大标题设置自己的属性文本.

There is no public API to set your own attributed text for the large title.

解决方案是向下导航视图层次结构.您特别提到要避免这种情况,但这是在免费获得 UINavigationBar 其余行为的同时修改颜色的唯一方法.

The solution is to navigate down the view hierarchy. You specifically mentioned you wanted to avoid this, but it's the only way to modify the colors while getting the rest of the UINavigationBar behavior for free.

当然,您始终可以创建自己的 UILabel 并设置其 attributedText,但您必须自己重新创建任何导航栏动画和其他行为.

Of course, you can always create your own UILabel and set its attributedText, but you will have to re-create any navigation bar animations and other behavior yourself.

老实说,最简单的解决方案是修改您的设计,使其不需要多色大标题,因为目前不支持.

Honestly the simplest solution is to modify your design so it doesn't require a multi-colored large title, as this is currently not supported.

我在洞穴探险"的道路上进行了深入研究,在动画恢复到原始文本颜色方面存在各种视觉问题.

I took a dive down the "spelunking" path, and there are a variety of visual issues with animations snapping back to the original text color.

这是我使用的代码,如果它对任何试图实现类似效果的人有用:

Here is the code I used if it's useful for anyone trying to achieve a similar effect:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    applyColorStyle(toLabels: findTitleLabels())
}

private func applyColorStyle(toLabels labels: [UILabel]) {
    for titleLabel in labels {
        let attributedString = NSMutableAttributedString(string: titleLabel.text ?? "")
        let fullRange = NSRange(location: 0, length: attributedString.length)
        attributedString.addAttribute(NSAttributedStringKey.font, value: titleLabel.font, range: fullRange)
        let colors = [UIColor.red, UIColor.orange, UIColor.yellow, UIColor.green, UIColor.blue, UIColor.purple]
        for (index, color) in colors.enumerated() {
            attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: NSRange(location: index, length: 1))
        }
        titleLabel.attributedText = attributedString
    }
}

private func findTitleLabels() -> [UILabel] {
    guard let navigationController = navigationController else { return [] }
    var labels = [UILabel]()
    for view in navigationController.navigationBar.subviews {
        for subview in view.subviews {
            if let label = subview as? UILabel {
                if label.text == title { labels.append(label) }
            }
        }
    }
    return labels
}

spelunking"方法的缺点是它不是受支持的 API,这意味着它很容易在未来的更新中中断或在各种边缘情况下无法按预期工作.

The downside of the "spelunking" approach is it's not a supported API, meaning that it could easily break in a future update or not work as intended in various edge cases.

这篇关于具有多种颜色的大导航栏文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆