Before I begin, let me acknowledge that similar questions exist, some with answers, others without. However, they do not address my specific problem. If you know of any, please refer me.
Now, when I add images to the UITextView
, I place the images at the point where the cursor is. I achieve this by appending 5 spaces each time to make room for the image. When the cursor gets to the end of the UItextView
, it stays there without moving automatically to the next line unless I type a key. Even when I append spaces, it still stays there so my images just pile up there. I decided to add a line-break "\n"
to move it to the next line manually.
Now I have two problems. First, only part of the images at the corner display even though I have a condition like:
if ((cursorPosition.x + 30) >= message.frame.width) {
message.text = message.text.stringByAppendingString("\n");
}
else {
message.text = message.text.stringByAppendingString(" ");
}
How do I fix this to make it not go out of the frame?
Secondly, because am adding new line manually, when I delete a text, the cursor moves to some arbitrary position which is quite weird to me. Example, I could be deleting text on line 4 but it will just jump to line 2.
Any suggestion on how to solve either or both would be very much appreciated. Below is how the UITextView
looks like:
Edited: I am editing this to help anyone who may have a similar problem. I wanted to create an app where users add text and images together like we do in WhatsApp
for example. I struggled to do this until the solution below was suggested to me. It works great. Hope it helps someone.