在字符串的 5 个字符之前截断 UILabel 文本 [英] Truncate UILabel text before 5 character of string

查看:43
本文介绍了在字符串的 5 个字符之前截断 UILabel 文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以根据屏幕大小和不同的设备方向在自动布局中截断字符串的 5 个字符之前的 UILabel 文本.

Is it possible to truncate the UILabel text before 5 character of string in autolayout based on the screen size and different device orientation ex.

测试测试测试测试测试...*1234

我知道 UILabel 有几种可能的 lineBreakMode,例如 .byTruncatingTail、.byTruncatingMiddle 等. 但对我的情况没有任何作用.任何建议将不胜感激.

I know there are several lineBreakMode possible for UILabel like .byTruncatingTail, .byTruncatingMiddle etc. but nothing is working for my case. Any suggestions will be appreciated.

推荐答案

感谢上面的每一次回答和评论!

Appreciate every once answers and comments above!

所以最后我能够完成它,它在 landscapeiPhoneiPad 的所有设备上都运行良好iPad 中的肖像 模式也是 split 模式

So finally I am able to finish it, which is working great in all devices of iPhone and iPad in landscape and portrait mode also split mode in iPad too!

所以我想在这里分享我的实现:

So i would like to share my implementation here:

第一步:创建一个 TitleView.xib 文件.

Step1: Create a TitleView.xib file.

Step2:取两个标签的Name LabelNumber Label.

第 3 步:给定以下两个标签的约束:

Step3: Given the following constraints to both the labels:

  • 名称标签限制:

  • 名称标签换行:截断尾部:

  • 数字标签限制:

步骤 4:然后在viewDidLoad方法中加载xib:

Step4: Then load the xib in viewDidLoad method:

override func viewDidLoad() {
        super.viewDidLoad()
        let titleView = Bundle.main.loadNibNamed("TitleView", owner: self, options: nil)?[0] as! TitleView
        self.navigationItem.titleView = titleView
    } 

这篇关于在字符串的 5 个字符之前截断 UILabel 文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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