首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UINavigationItem标题截断

UINavigationItem标题截断
EN

Stack Overflow用户
提问于 2016-12-21 14:25:01
回答 2查看 1.6K关注 0票数 2

我有由两个字符串组成的文本: A(B)

我想使用这两个字符串作为UINavigationItem的标题,但是在截断的情况下,我只希望A被截断,而不是B。

例子:我想要的标题是“快速的棕色狐狸跳过懒惰的狗”。屏幕太小了,所以标题会被截断,现在是“快速褐狐跳跃”(超过.)

我想要的是:“快速br.(对懒惰的狗)”

我怎么能这么做?

EN

回答 2

Stack Overflow用户

发布于 2016-12-21 14:34:20

首先检查字符导航栏的数目,允许作为标题而不截断。接下来,在设置标题之前,检查总字符串长度是否小于这些字符数。现在,基于条件集,要么组合字符串,要么B。

票数 1
EN

Stack Overflow用户

发布于 2016-12-21 15:47:57

由于xcode无法检测出您想要停止的标题的哪个部分,以及您希望重新开始的部分,所以您需要自己执行它。

代码语言:javascript
复制
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
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41265064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档