Ios 将光标放在新图像的末尾,而不是之前

Ios 将光标放在新图像的末尾,而不是之前,ios,swift,uitextview,nsattributedstring,nstextstorage,Ios,Swift,Uitextview,Nsattributedstring,Nstextstorage,我正在创建一个应用程序,用户可以将图像和文本添加到UITextView中,并与朋友共享。 感谢其他StackExchange用户,我成功地将图像和文本添加到一起。我现在面临一个光标定位的小问题 插入文本时,光标按预期移动。但是,当我添加图像时,光标仍保留在图像后面,即上次插入文本的末尾。下面是我的代码。如何将光标移动到新插入图像的末尾,以便新添加的文本/图像附加到右侧而不是左侧 UIGraphicsBeginImageContext(CGSizeMake(25, 25)); let img =

我正在创建一个应用程序,用户可以将图像和文本添加到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);
编辑:我找到了问题的解决办法

我只需向selectedRange.location添加一个,如下所示:
textView.selectedRange.location+=1

在OSX上,有一个文本编辑器,它保留了一个可修改的选定范围。@ThomasKilian,如果您能详细说明一下,我将不胜感激。我对iOS编程还是相当陌生的。这实际上是我的第一个应用程序。如何修改所选范围?添加到其中的类消息可能是NSText,对吗?