Reputation: 1239
I would like to shorten long strings with an ending of "see more...".
Everything goes well if the string does not have new lines.
"The cat ate the cake and went outside the house, without any problems. Then the next day something really bad happened. The owner kicked the cat out, and the little animal had no chance to go back."
becomes
"The cat ate the cake and went outside the house, without any problems. Then the see more..."
But besides this, I would like to limit the height as well. So that the string can be maximum 200pt in height, like this:
2 new lines
>
1 new line
3 new lines
>
>
0 new line
should become
2 new lines
>
1 new line
See more...
How can I determine, after which character should I to put the "See more..." text?
Code:
func shorten(maxWidth : CGFloat) -> NSMutableAttributedString {
let font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body)
// If height is more than 200pt
if self.height(withConstrainedWidth: maxWidth, font: font) > 200 {
// Here.. how?
return finalString
}
// If char is more than 400
if self.count > 400 {
// BLA BLA CUT TEXT
return finalString
}
let finalString = NSMutableAttributedString(string: self, attributes: [NSAttributedString.Key.font : font])
return finalString
}
Upvotes: 0
Views: 38
Reputation: 2875
I would highly recommend you not put see more...
as plain string inside your text.
Better approach is to UIButton
with see more...
text right after you label.
In such case you can easily manipulate it's position, form, UI, etc
Upvotes: 1