2016-05-04 6 views
3

TextViewsqlite databaseにデータを追加しています。私はNSTextAttachmentでやったParaやLineのデータの後に30〜35枚の画像を追加しています。今私がしたいのは、2つの画像があり、それがCarousel Viewであり、その後に残っているデータが必要な場合はどこにでもあります。私のコードを見てください。私は別の方法を試みましたが、成功を収めませんでした。どんな助けでも大歓迎です。テキストビュー内にカルーセルビューを追加する

NSTextAttachment *textAttachment1 = [[NSTextAttachment alloc] init]; 
    textAttachment1.image = [UIImage imageNamed:@"img1.png"]; 
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; 
    textAttachment.image = [UIImage imageNamed:@"img2.png"]; 
    NSTextAttachment *textAttachment2 = [[NSTextAttachment alloc] init]; 
    textAttachment2.image = [UIImage imageNamed:@"img3.png"]; 
    NSTextAttachment *textAttachment3 = [[NSTextAttachment alloc] init]; 

    CGFloat oldWidth1 = textAttachment1.image.size.width; 
    CGFloat oldWidth = textAttachment.image.size.width; 
    CGFloat oldWidth2 = textAttachment2.image.size.width; 

    CGFloat scaleFactor1 = oldWidth1/(self.textViewResearch.frame.size.width + 70); 
    CGFloat scaleFactor = oldWidth/(self.textViewResearch.frame.size.width + 70); 
    CGFloat scaleFactor2 = oldWidth2/(self.textViewResearch.frame.size.width + 70); 

    textAttachment1.image = [UIImage imageWithCGImage:textAttachment1.image.CGImage scale:scaleFactor1 orientation:UIImageOrientationUp]; 
    textAttachment.image = [UIImage imageWithCGImage:textAttachment.image.CGImage scale:scaleFactor orientation:UIImageOrientationUp]; 
    textAttachment2.image = [UIImage imageWithCGImage:textAttachment2.image.CGImage scale:scaleFactor2 orientation:UIImageOrientationUp]; 

    NSAttributedString *attrStringWithImage1 = [NSAttributedString attributedStringWithAttachment:textAttachment1]; 
    NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment]; 
    NSAttributedString *attrStringWithImage2 = [NSAttributedString attributedStringWithAttachment:textAttachment2]; 

    [attributedString replaceCharactersInRange:NSMakeRange(0, 0) withAttributedString:attrStringWithImage1]; 
    [attributedString replaceCharactersInRange:NSMakeRange(13740, 0) withAttributedString:attrStringWithImage]; 
    [attributedString replaceCharactersInRange:NSMakeRange(13741, 0) withAttributedString:attrStringWithImage2]; 

    self.textView.attributedText = attributedString; 
+0

代わりにuicollectionviewを使用しますか? uicollectionviewcellに含まれる最初の2つの画像を最初のセルに配置します。その後、残りの部分を共通のセルにしておきます。私はあなたの考えが正しいかどうか疑問に思います... – Calios

+0

@Calios。アプリは異なっていますUIColllectionVIewを追加することはできません – iDeveloper

+0

*私は別の方法を試みましたが、成功を得られませんでした。 –

答えて

1

私が正しく理解していれば、画像をテキストビュー内に置き、テキストを周囲に流したいと思っています。 2つ以上のイメージを持っていて、カルーセルを持ちたい場合は、クラスiCarouselのインスタンスです。

UITextViewのインスタンス内にサブビューを置いて、テキストを周囲に浮かべることは、既知のタスクです。あなたは除外領域、サブビューの変更のたびフレームを変更する必要があります。もちろん、

// Get the dimension of the subview 
iCarousel *subview = …; 
UIBezierPath *subviewPath = [UIBezierPath bezierPathWithRect:subview.frame]; 

// Somewhere you should add it 
UITextView *textView = …; // Wherever it comes from 
[textView addSubview:subview]; 

// Let the text flow around it 
UITextContainer *textContainer = textView.textContainer; // The text container is the region text is laid out in. 
textContainer.exclusionPaths = [subviewPath]; // Do not lay out in this region. 

:最も簡単な方法は、このようなテキストコンテナの除外パスを使用することのようです。

より柔軟でより複雑なアプローチは、独自のテキストレイアウトを行うことです。上記の方法がうまくいかない場合は、教えてください。私はこのためのコードを追加します。

+0

あなたの貢献に対するThanx Amin、上記のコードは機能せず、私が考えたことは、texviewの中にカルーセルビューを追加することはできません。私の友人が提案した他の解決策は、カスタムセルでTableViewを使うことです。私が解決策を実装しているのを見てみましょう。私はそれを済ませたら、必ずあなたに知らせるでしょう。 – iDeveloper

+0

私は決してカルーセルビューをテキストビューに入れません。しかし、テキストビューにビューを配置することは間違いありません。なぜ、カルーセルビューがそこでは機能しないのか、理由はありません。 –

+0

こんにちはAminはあなたの助けにたくさんの、私はまだそれを実装していないが、私は簡単に見つけたtableviewを介して解決策を見つけた、まだあなたの解決策は有効です。だから賞金はあなたのものです。 :) – iDeveloper

関連する問題