2012-11-07 19 views
11

में छवियां जोड़ना मेरे ऐप में UITextView है और संपादन के दौरान UITextView में फ़ोटो डालने के लिए टेक्स्ट व्यू के ठीक नीचे एक बटन है।UITextView

मेरी आवश्यकता यह है कि उपयोगकर्ता उपयोगकर्ता टेक्स्ट को संपादित करने में सक्षम है, और आवश्यकता होने पर छवियों को सम्मिलित करने में सक्षम है।

StackOverflow के एप्लिकेशन की स्वयं की UITextView के समान:

enter image description here

उत्तर

17

आप UITextView की एक subview के रूप में छवि को देखने जोड़ सकते हैं। इस बाहर

CGRect aRect = CGRectMake(156, 8, 16, 16); 
[imageView setFrame:aRect]; 
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(CGRectGetMinX(imageView.frame), CGRectGetMinY(imageView.frame), CGRectGetWidth(yourTextView.frame), CGRectGetHeight(imageView.frame))]; 
yourTextView.textContainer.exclusionPaths = @[exclusionPath]; 
[yourTextView addSubview:imageView]; 
+9

ओवरराइड लेकिन आप से imageView रोकूँ टेक्स्टव्यू में पाठ को कवर करना? आप छविदृश्य के चारों ओर पाठ कैसे प्रवाह प्राप्त करते हैं? –

+5

सीजीआरएक्ट एआरक्ट = सीजीआरईटीमेक (156, 8, 16, 16); [अटैचमेंट व्यू सेटफ्रेम: एआरक्ट]; UIBezierPath * exclusionPath = [UIBezierPath bezierPathWithRect: CGRectMake (CGRectGetMinX (attachmentView.frame), CGRectGetMinY (attachmentView.frame), CGRectGetWidth (internalTextView.frame), CGRectGetHeight (attachmentView.frame))]; internalTextView.textContainer.exclusionPaths = @ [exclusionPath]; [आंतरिकTextView addSubview: attachmentView]; –

1

चेक, ios-5-rich-text-editing-series:

छवि के साथ एक imageView बनाएँ:

UIImageView *imageView = [[UIImageView alloc] initWithImage:yourImage]; 
[imageView setFrame:yourFrame]; 
[yourTextView addSubview:imageView]; 

संपादित करें:

अतिव्यापी उपयोग (धन्यवाद @chris) से बचने के लिए। आईओएस 5 में आप छवियां सम्मिलित कर सकते हैं और एचटीएमएल ग्रंथों का उपयोग कर सकते हैं। आपको UIWebview और वेबकिट का उपयोग करना पड़ सकता है।

आप EGOTextView के साथ भी जांच सकते हैं जिसमें बहुत समृद्ध टेक्स्ट संपादन सुविधाएं हैं।

+0

@ user1645721, यदि आप चाहते हैं कि उपयोगकर्ता छवि के बाद टेक्स्ट दर्ज करना प्रारंभ करें, तो आपको मेरे उत्तर में उपरोक्त सुझावों का उपयोग करना पड़ सकता है। – iDev

1

सिर्फ bellow तरह TextView की एक subview के रूप में जोड़ने ..

[yourTextView addSubview:yourImageView]; 
9

आप केवल एक subview के रूप में यह जोड़ते हैं, तो कुछ पाठ "पीछे" छवि हो सकता है। तो कोड है जो "बता" होगा पाठ जोड़ने, छवि का वह क्षेत्र दुर्गम है:

UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(CGRectGetMinX(imageView.frame),  
CGRectGetMinY(imageView.frame), CGRectGetWidth(imageView.frame), 
CGRectGetHeight(imageView.frame))]; 

textView.textContainer.exclusionPaths = @[exclusionPath]; 
0

UITextView के उपवर्ग बनाएँ और इस विधि

- (void)paste:(id)sender 
{ 
    NSData *data = [[UIPasteboard generalPasteboard] dataForPasteboardType:@"public.png"]; 

    if (data) 
    { 

     NSMutableAttributedString *attributedString = [[self attributedText] mutableCopy]; 

     NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; 

     textAttachment.image = [UIImage imageWithData:data scale:5]; 

     NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment]; 

     [attributedString replaceCharactersInRange:self.selectedRange withAttributedString:attrStringWithImage]; 

     self.attributedText = attributedString; 

    } 
    else 
    { 

     UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard]; 

     NSAttributedString *text = [[NSAttributedString alloc] initWithString:pasteBoard.string]; 

     NSMutableAttributedString *attributedString = [self.attributedText mutableCopy]; 

     [attributedString replaceCharactersInRange:self.selectedRange withAttributedString:text]; 

     self.attributedText = attributedString; 

    } 
}