Truncate string to fit CGSize?

Viewed 401

I am trying to have string truncate if the actual width of the string exceeds a certain size (not to number of characters in string!)

I can get the width of the string using NSString.sizeWithAttributes

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 20))
label.text = "Testing text"
let labelStringSize = (label.text! as NSString).sizeWithAttributes([NSFontAttributeName: label.font]) 
// labelStringSize dimensions: w 89.267 h 20.287

But how would I handle truncating the label's text if, say, labelStringSize exceeded 50?

Is there an easier way to to do this I have overlooked?

0 Answers
Related