我有由两个字符串组成的文本: A(B)
我想使用这两个字符串作为UINavigationItem的标题,但是在截断的情况下,我只希望A被截断,而不是B。
例子:我想要的标题是“快速的棕色狐狸跳过懒惰的狗”。屏幕太小了,所以标题会被截断,现在是“快速褐狐跳跃”(超过.)。
我想要的是:“快速br.(对懒惰的狗)”
我怎么能这么做?
发布于 2016-12-21 14:34:20
首先检查字符导航栏的数目,允许作为标题而不截断。接下来,在设置标题之前,检查总字符串长度是否小于这些字符数。现在,基于条件集,要么组合字符串,要么B。
发布于 2016-12-21 15:47:57
由于xcode无法检测出您想要停止的标题的哪个部分,以及您希望重新开始的部分,所以您需要自己执行它。
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Heal the world make it a better place for you and for me and the entire human race"
setNavTitle(title: self.navigationItem.title!)
}
func setNavTitle(title: String)
{
//get the last 10 characters of your title, change the number to your need
let last10 = title.substring(from:title.index(title.endIndex, offsetBy: -14))
//get the first 10 characters of your title, change the number to your need
let first10 = title.substring(to: title.index(title.startIndex, offsetBy: 14))
//concatenate the strings
let title = first10 + " ... " + last10
self.navigationItem.title = title
}https://stackoverflow.com/questions/41265064
复制相似问题