NavigationTitle 与 subTitle 重叠
NavigationTitle with subTitle are overlapping
我有一个带有 rightButtonItem 的导航栏和另一个带有 right&leftButtonItem 的导航栏。
我练习尝试带副标题的导航栏,但是当我设置标题和副标题字符串很长时。
它会重叠,并且字符串被截断。
我该如何解决这个问题?
Issue picture_1
Issue picture_2
func setTitle(title:String, subtitle:String) -> UIView {
let titleLabel = UILabel(frame: CGRect(x:0, y:-5, width:0, height:0))
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = UIColor.red
titleLabel.font = UIFont.boldSystemFont(ofSize: 14)
titleLabel.text = title
titleLabel.sizeToFit()
let subtitleLabel = UILabel(frame: CGRect(x:0, y:18, width:0, height:0))
subtitleLabel.backgroundColor = UIColor.clear
subtitleLabel.textColor = UIColor.blue
subtitleLabel.font = UIFont.systemFont(ofSize: 12)
subtitleLabel.text = subtitle
subtitleLabel.sizeToFit()
let titleView = UIView(frame: CGRect(x:0, y:0, width:max(titleLabel.frame.size.width, subtitleLabel.frame.size.width), height:30))
titleView.addSubview(titleLabel)
titleView.addSubview(subtitleLabel)
let widthDiff = subtitleLabel.frame.size.width - titleLabel.frame.size.width
if widthDiff > 0 {
var frame = titleLabel.frame
frame.origin.x = widthDiff / 2
titleLabel.frame = frame.integral
} else {
var frame = subtitleLabel.frame
frame.origin.x = abs(widthDiff) / 2
titleLabel.frame = frame.integral
}
return titleView
}//source from here:https://gist.github.com/nazywamsiepawel/0166e8a71d74e96c7898
您可以尝试在一个标签中添加两条线,这样它们就不会重叠,而不是创建另一个标签。使用此代码
label.numberOfLines = 2
设置文本时,在字符串中使用 \n 表示下一行,如下所示
label.text= "Main title\nSub title"
如果您想要两个标题使用不同的字体,请尝试使用属性字符串,例如 this
您的代码的这一部分需要稍作修改:
if widthDiff > 0 {
var frame = titleLabel.frame
frame.origin.x = widthDiff / 2
titleLabel.frame = frame.integral
} else {
var frame = subtitleLabel.frame
frame.origin.x = abs(widthDiff) / 2
// this should be *subtitleLabel*
//titleLabel.frame = frame.integral
subtitleLabel.frame = frame.integral
}
我有一个带有 rightButtonItem 的导航栏和另一个带有 right&leftButtonItem 的导航栏。
我练习尝试带副标题的导航栏,但是当我设置标题和副标题字符串很长时。
它会重叠,并且字符串被截断。
我该如何解决这个问题?
Issue picture_1
Issue picture_2
func setTitle(title:String, subtitle:String) -> UIView {
let titleLabel = UILabel(frame: CGRect(x:0, y:-5, width:0, height:0))
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = UIColor.red
titleLabel.font = UIFont.boldSystemFont(ofSize: 14)
titleLabel.text = title
titleLabel.sizeToFit()
let subtitleLabel = UILabel(frame: CGRect(x:0, y:18, width:0, height:0))
subtitleLabel.backgroundColor = UIColor.clear
subtitleLabel.textColor = UIColor.blue
subtitleLabel.font = UIFont.systemFont(ofSize: 12)
subtitleLabel.text = subtitle
subtitleLabel.sizeToFit()
let titleView = UIView(frame: CGRect(x:0, y:0, width:max(titleLabel.frame.size.width, subtitleLabel.frame.size.width), height:30))
titleView.addSubview(titleLabel)
titleView.addSubview(subtitleLabel)
let widthDiff = subtitleLabel.frame.size.width - titleLabel.frame.size.width
if widthDiff > 0 {
var frame = titleLabel.frame
frame.origin.x = widthDiff / 2
titleLabel.frame = frame.integral
} else {
var frame = subtitleLabel.frame
frame.origin.x = abs(widthDiff) / 2
titleLabel.frame = frame.integral
}
return titleView
}//source from here:https://gist.github.com/nazywamsiepawel/0166e8a71d74e96c7898
您可以尝试在一个标签中添加两条线,这样它们就不会重叠,而不是创建另一个标签。使用此代码
label.numberOfLines = 2
设置文本时,在字符串中使用 \n 表示下一行,如下所示
label.text= "Main title\nSub title"
如果您想要两个标题使用不同的字体,请尝试使用属性字符串,例如 this
您的代码的这一部分需要稍作修改:
if widthDiff > 0 {
var frame = titleLabel.frame
frame.origin.x = widthDiff / 2
titleLabel.frame = frame.integral
} else {
var frame = subtitleLabel.frame
frame.origin.x = abs(widthDiff) / 2
// this should be *subtitleLabel*
//titleLabel.frame = frame.integral
subtitleLabel.frame = frame.integral
}