Reputation: 570
I want to create a message box just like Telegram and others chats, that have a time in the down side of the box.
But if the text ends in the end of the line, the time label should be in the next line, otherwise, should stay in the same line.
So, I want something like this:
How to accomplish this in my xib, or programatically ?
Thanks in regards,
Upvotes: 3
Views: 950
Reputation: 11039
You can calculate the position of the last character in your textview and check if there is enough space for time label then show it, else add it on a new line.
UITextPosition *Pos2 = [textView positionFromPosition:textView.endOfDocument offset:0];
UITextPosition *Pos1 = [textView positionFromPosition:textView.endOfDocument offset:-1];
UITextRange *range = [textView textRangeFromPosition:Pos1 toPosition:Pos2];
CGRect lastCharRect = [textView firstRectForRange:(UITextRange *)range];
CGFloat freeSpace = textView.frame.size.width - lastCharRect.origin.x - lastCharRect.size.width;
if (freeSpace > requiredWidthForTimeLabel) {
//add time label to the last line
} else {
//add time label on a new line
}
Upvotes: 5