淡化标签文本的结尾 [英] Fade the end of a label text
本文介绍了淡化标签文本的结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 UILabel
,我希望淡化字符串的结尾,这将超出界限。对此有什么更好的解决方案?
我应该计算标签的宽度,将它与字符串宽度进行比较,如果字符串宽度大于标签的宽度,我应该淡化最后两个字母吗?我该怎么做?
我希望这很容易。请写下你的解决方案。谢谢!
我更喜欢使用此方法计算宽度:
I have one UILabel
, and i want to fade the end of the string, that is going to be out of bounds. What is the better solution for this?
Should i calculate the width of the label, compare it with the string width, and if string width is bigger than label's, i should fade last two letters? How exactly should i do that?
I hope it will be easy. Please write your solutions. Thanks!
I prefer to use this method for calculating the width:
CGRect labelRect = [text
boundingRectWithSize:labelSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{
NSFontAttributeName : [UIFont systemFontOfSize:14]
}
context:nil];
推荐答案
Swift 4.0示例:
Swift 4.0 example:
let gradient = CAGradientLayer()
gradient.frame = titleLabel.bounds
gradient.colors = [UIColor.white.cgColor, UIColor.clear.cgColor]
gradient.startPoint = CGPoint(x: 0.1, y: 0.0)
gradient.endPoint = CGPoint(x: 0.95, y: 0.0)
label.lineBreakMode = .byClipping
label.layer.mask = gradient
结果:
这篇关于淡化标签文本的结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文