在“UITextView”中合并文本和图像

在我开始之前,让我承认存在类似的问题,有些答案是有答案的,有些则没有答案。 但是,他们没有解决我的具体问题。 如果你知道任何,请转介我。

现在,当我将图像添加到UITextView ,我将图像放置在光标所在的位置。 我通过每次追加5个空格为图像腾出空间来达到这个目的。 当游标到达UItextView的末尾时,它将停留在那里而不会自动移动到下一行,除非我键入一个键。 即使当我追加空格,它仍然停留在那里,所以我的图像堆积在那里。 我决定添加一个换行符"\n"将其移动到下一行手动。 现在我有两个问题。 首先,即使我有这样一个条件,只有angular落的一部分图像显示:

 if ((cursorPosition.x + 30) >= message.frame.width) { message.text = message.text.stringByAppendingString("\n"); } else { message.text = message.text.stringByAppendingString(" "); } 

我该如何解决这个问题,使它不会离开框架?

其次,因为我手动添加新行,当我删除一个文本,光标移动到一些任意的位置,这对我来说很奇怪。 例如,我可能会删除第4行的文本,但它会跳到第2行。任何有关如何解决其中一个或两个的build议将非常感激。 以下是UITextView外观:

编辑:我正在编辑这个帮助任何人可能有类似的问题。 我想创build一个应用程序,用户可以像在WhatsApp一样添加文本和图像。 我努力做到这一点,直到下面的解决scheme被build议给我。 它工作很好。 希望它可以帮助别人。

如果我理解你的目标,以及你目前如何实施。 你应该检查你是否需要先返回。

 if ((cursorPosition.x + 30) >= message.frame.width) { message.text = message.text.stringByAppendingString("\n"); } 

然后你应该得到当前的光标位置,并在那里添加你的UIImageView。

  let size = CGSize(width: 30, height: 30); let img = UIImage(named: change_arr[indexPath.row]); let addImg = UIImageView(image: UIImage(named: change_arr[indexPath.row])); addImg.frame = CGRect(origin: newCursorPosition, size: size); message.addSubview(addImg); 

那么如果你需要添加空格,现在就添加它们。

正如前面的评论所述,使用NSTextAttachment将简化所有这些,并防止您必须创buildUIViews等…

它看起来像这样,你不需要检查光标的位置,因为它会像处理文本一样处理。

 //create your UIImage let image = UIImage(named: change_arr[indexPath.row]); //create and NSTextAttachment and add your image to it. let attachment = NSTextAttachment() attachment.image = image //put your NSTextAttachment into and attributedString let attString = NSAttributedString(attachment: attachment) //add this attributed string to the current position. textView.textStorage.insertAttributedString(attString, atIndex: textView.selectedRange.location) 

现在图像是内联的,并且像文本一样对待。 如果用户退格,它就像文本一样删除。

 __block NSTextAttachment *imageAttachment = [NSTextAttachment new]; imageAttachment.bounds = CGRectMake(0, -5, 20, 20); NSAttributedString *stringWithImage = [NSAttributedString attributedStringWithAttachment:imageAttachment]; [deCodedString replaceCharactersInRange:NSMakeRange(deCodedString.length, 0) withAttributedString:stringWithImage]; incomingMessage.messageAttributedString = deCodedString; SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader]; imageAttachment.image = [UIImage imageNamed:@"profile_main_placeholder"]; [downloader downloadImageWithURL:aURL options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { // progression tracking code } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) { if (image && finished) { [image drawInRect:CGRectMake(0, 0, 20, 20)]; imageAttachment.image = image; dispatch_async(dispatch_get_main_queue(), ^(void) { [self.tbl_Conversation reloadRowsAtIndexPaths:[self.tbl_Conversation indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone]; [self.tbl_Conversation reloadData]; }); // NSAttributedString *stringWithImage = [NSAttributedString attributedStringWithAttachment:imageAttachment]; // [deCodedString replaceCharactersInRange:NSMakeRange(deCodedString.length, 0) withAttributedString:stringWithImage]; // incomingMessage.messageAttributedString = deCodedString; } }]; 

其实,我使用NSAttributedString类来解决这个问题。 它允许程序员在文本缓冲区中合并图像,并像缓冲区中的单个字符一样对待,这意味着在光标前用空格键移动图像,所有其他文本将空格移动到右侧。 同样在图像从缓冲区中删除之前点击删除键。 希望它可以帮助别人