Gabriel Bastos
Gabriel Bastos

Reputation: 570

Get the end of a string in a uitextview

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: new line for date when content is goes until the end

How to accomplish this in my xib, or programatically ?

Thanks in regards,

Upvotes: 3

Views: 950

Answers (1)

arturdev
arturdev

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

Related Questions