用两行更改此自定义导航栏中的文本颜色?
Change the color of text in this custom navigation bar with two rows?
我目前有一个启用了大标题的导航栏,它还支持两行,在 viewdidLoad 中使用以下代码:
navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationItem.largeTitleDisplayMode = .automatic
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd"
let result = formatter.string(from: date)
self.title = “This is a Test\n\(result)"
var count = 0
for item in(self.navigationController?.navigationBar.subviews)! {
for sub in item.subviews{
if sub is UILabel{
if count == 1 {
break;
}
let titleLab :UILabel = sub as! UILabel
titleLab.numberOfLines = 0
titleLab.text = self.title
titleLab.lineBreakMode = .byWordWrapping
count = count + 1
}
}
}
self.navigationController?.navigationBar.layoutSubviews()
self.navigationController?.navigationBar.layoutIfNeeded()
如何改变每行文字的字体和颜色
self.title = “This is a Test\n\(result)"
例如,使 "This is a Test"
黑色和 "(result)"
灰色。
首先从 string
创建一个 attributedString
并添加所需的 attributes
,即
let result = "13 June 2019"
let text = "This is a Test\n\(result)"
let arr = text.components(separatedBy: .newlines)
let attributedString = NSMutableAttributedString()
for (index, str) in arr.enumerated() {
let attrStr = NSMutableAttributedString(string: str)
if index == 0 {
attrStr.addAttribute(.foregroundColor, value: UIColor.red, range: NSRange(location: 0, length: str.count))
attrStr.addAttribute(.font, value: UIFont.systemFont(ofSize: 12.0, weight: .bold), range: NSRange(location: 0, length: str.count))
} else {
attrStr.addAttribute(.foregroundColor, value: UIColor.blue, range: NSRange(location: 0, length: str.count))
attrStr.addAttribute(.font, value: UIFont.systemFont(ofSize: 12.0), range: NSRange(location: 0, length: str.count))
}
if index < arr.count {
attributedString.append(NSAttributedString(string: "\n"))
}
attributedString.append(attrStr)
}
创建一个 UILabel
并将此 attributedString
添加为 label
的 attributedText
。
let label = UILabel()
label.textAlignment = .center
label.numberOfLines = 0
label.attributedText = attributedString
将此 label
添加为 navigationItem
的 titleView
,即
self.navigationItem.titleView = label
我目前有一个启用了大标题的导航栏,它还支持两行,在 viewdidLoad 中使用以下代码:
navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationItem.largeTitleDisplayMode = .automatic
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd"
let result = formatter.string(from: date)
self.title = “This is a Test\n\(result)"
var count = 0
for item in(self.navigationController?.navigationBar.subviews)! {
for sub in item.subviews{
if sub is UILabel{
if count == 1 {
break;
}
let titleLab :UILabel = sub as! UILabel
titleLab.numberOfLines = 0
titleLab.text = self.title
titleLab.lineBreakMode = .byWordWrapping
count = count + 1
}
}
}
self.navigationController?.navigationBar.layoutSubviews()
self.navigationController?.navigationBar.layoutIfNeeded()
如何改变每行文字的字体和颜色
self.title = “This is a Test\n\(result)"
例如,使 "This is a Test"
黑色和 "(result)"
灰色。
首先从 string
创建一个 attributedString
并添加所需的 attributes
,即
let result = "13 June 2019"
let text = "This is a Test\n\(result)"
let arr = text.components(separatedBy: .newlines)
let attributedString = NSMutableAttributedString()
for (index, str) in arr.enumerated() {
let attrStr = NSMutableAttributedString(string: str)
if index == 0 {
attrStr.addAttribute(.foregroundColor, value: UIColor.red, range: NSRange(location: 0, length: str.count))
attrStr.addAttribute(.font, value: UIFont.systemFont(ofSize: 12.0, weight: .bold), range: NSRange(location: 0, length: str.count))
} else {
attrStr.addAttribute(.foregroundColor, value: UIColor.blue, range: NSRange(location: 0, length: str.count))
attrStr.addAttribute(.font, value: UIFont.systemFont(ofSize: 12.0), range: NSRange(location: 0, length: str.count))
}
if index < arr.count {
attributedString.append(NSAttributedString(string: "\n"))
}
attributedString.append(attrStr)
}
创建一个 UILabel
并将此 attributedString
添加为 label
的 attributedText
。
let label = UILabel()
label.textAlignment = .center
label.numberOfLines = 0
label.attributedText = attributedString
将此 label
添加为 navigationItem
的 titleView
,即
self.navigationItem.titleView = label