2015-02-10 50 views
0

在開始之前,請讓我承認存在類似的問題,其中一些有答案,而另一些則沒有答案。但是,他們沒有解決我的具體問題。如果你知道任何,請轉介我。在「UITextView」中合併文本和圖像

現在,當我將圖像添加到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中一樣添加文本和圖像。我努力做到這一點,直到下面的解決方案被建議給我。它效果很好。希望它能幫助別人。

+0

你可以證明你是如何添加圖像? – 2015-02-10 04:43:48

+0

你什麼時候設置cursorPosition1?在添加換行符之前還是之後? – 2015-02-10 05:00:30

+0

@Jeremy Pope,我在添加圖片後設置它。對於你來說,它和上面的cursorPosition一樣。我只是改變了。 – 2015-02-10 05:02:55

回答

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) 

現在圖像是內聯的,並且像文本一樣處理。如果用戶退格,它會像文本一樣刪除。

+0

這工作正常。現在第二個問題仍然存在。 當我刪除任何文本時,光標跳轉到某個任意位置。任何想法是什麼做錯了或如何處理它? – 2015-02-10 05:26:40

+0

我更新後是否嘗試過這種方法?使用NSTextAttachment可解決刪除問題。 – 2015-02-10 15:10:02

+0

我修復了這個問題。它現在很好用。謝謝。 當您insertAttributedString(attString,atIndex)'時,光標停留在新插入的對象後面。 你必須手動將它移動到對象之後:'textView.selectedRange.location + = 1;' – 2015-02-12 04:10:08

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

其實,我使用NSAttributedString類解決了這個問題。它允許程序員將圖像合併到文本緩衝區中,並像緩衝區中的單個字符一樣對待,這意味着在光標移動圖像和所有其他文本之後,光標將移動到空格鍵的右側。 同樣在圖像從緩衝區中刪除它之前點擊刪除鍵。 希望它可以幫助別人