在`UITextView`中组合文本和图像

时间:2015-02-10 04:20:32

标签: ios swift

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

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

if ((cursorPosition.x + 30) >= message.frame.width) {

        message.text = message.text.stringByAppendingString("\n");
    }
    else {
        message.text = message.text.stringByAppendingString("      ");
    }

如何解决此问题,使其不会超出框架?

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

已编辑:我正在编辑此内容以帮助可能遇到类似问题的任何人。我想创建一个应用程序,用户可以像在WhatsApp中一样添加文本和图像。我努力做到这一点,直到我建议下面的解决方案。它很棒。希望它可以帮到某人。

3 个答案:

答案 0 :(得分:10)

如果我了解您的目标,以及您目前如何实施该目标。您应该检查是否需要先退货。

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将简化所有这些,并防止您必须创建UIViews等...

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

//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)

现在图像是内联的,并且像文本一样处理。如果用户对其进行退格,则会删除文本。

答案 1 :(得分:0)

__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;
                               }
                           }];

答案 2 :(得分:0)

实际上,我使用NSAttributedString类解决了这个问题。它允许程序员在文本缓冲区中组合图像,并将其视为缓冲区中的单个字符,这意味着在该图像将图像移动到所有其他文本的一个空格之前,用光标敲击空格键。 同样在图像从缓冲区中删除之前按下删除键。 希望它可以帮助某人