ユーザーが画像とテキストを一緒に UITextView に追加して友達と共有するアプリを作成しています。StackExchange
他のユーザーのおかげで、画像とテキストを一緒に追加することに成功しました。現在、カーソルの配置に関する小さな問題に直面しています。
テキストを挿入すると、カーソルが期待どおりに移動します。ただし、画像を追加すると、カーソルは画像の後ろに残ります (つまり、最後に挿入されたテキストの終わり)。以下は私のコードです。新しく追加されたテキスト/画像が左ではなく右に追加されるように、新しく挿入された画像の最後にカーソルを移動するにはどうすればよいですか。
UIGraphicsBeginImageContext(CGSizeMake(25, 25));
let img = UIImage(named:change_arr[indexPath.row]);
img?.drawInRect(CGRect(x: 0, y: 0, width: 25, height: 25));
let img1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Create a NSTextAttachment object which will hold the created emoji
let textAttachment = NSTextAttachment();
// Attach the emoji to the NSTextAttachment object
textAttachment.image = img1;
// Create an NSAttributedString object to hold any dynamic data like the created emoji
var attributedString = NSAttributedString(attachment: textAttachment);
// Add the NSAttributedString to the current position of the cursor
message.textStorage.insertAttributedString(attributedString, atIndex: message.selectedRange.location);
編集済み:問題の解決策を見つけました。
次のように 1 つ追加するだけで済みましたselectedRange.location
: textView.selectedRange.location += 1