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 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
result: