【已解决】swift中UILabel中显示文件名长度太长时保证后缀可以显示
DelorasEIOK
9年前
<h2>问题</h2> <p>之前的代码是:</p> <pre> <code class="language-swift">//2. file name label self.nameLabel.text = resMsg.fileItem.name self.nameLabel.font = UIFont.systemFontOfSize(13) self.nameLabel.textColor = ColorConversationText self.nameLabel.textAlignment = NSTextAlignment.Left self.nameLabel.lineBreakMode = NSLineBreakMode.ByCharWrapping self.nameLabel.numberOfLines = 0 //self.fileView.addSubview(self.nameLabel) self.addSubview(self.nameLabel) constrain(nameLabel, iconImageView) { nameLabel, iconImageView in nameLabel.top == nameLabel.superview!.top + 6 nameLabel.left == iconImageView.right + 10 nameLabel.right <= nameLabel.superview!.right - 8 nameLabel.bottom <= nameLabel.superview!.bottom - 20 }</code></pre> <p>效果是:</p> <p><img src="https://simg.open-open.com/show/d1bfa6c30660a578ae0e5303febad400.png"></p> <p>希望达到的效果是:</p> <p>文件名太长时,保持文件名的开始后最后的后缀显示,中间部分可以变成…</p> <p>无意间发现,直接修改参数即可:</p> <p>现在改为:</p> <pre> <code class="language-swift">//self.nameLabel.lineBreakMode = NSLineBreakMode.ByCharWrapping self.nameLabel.lineBreakMode = NSLineBreakMode.ByTruncatingMiddle</code></pre> <p>效果是:</p> <p><img src="https://simg.open-open.com/show/b062507b703aca9a5e018d6f7ef1368e.png"></p> <h2>总结</h2> <p>对于UILabel的的话,设置lineBreakMode为NSLineBreakMode.ByTruncatingMiddle,即可使得当文字太长,优先显示前面和最后面的内容,而让中间内容显示省略号三个点</p> <p>-》对于文件名来说,就是保证文件名后缀显示了。</p> <p> </p> <p>来自: <a href="/misc/goto?guid=4959673502498300259" rel="nofollow">http://www.crifan.com/swift_uilabel_filename_too_long_show_suffix/</a></p> <p> </p>