2016-04-02 8 views
3

UITextView画像にrtfd文書を表示しようとしています。問題は、画像がUITextViewのフレームと重なっていることです。幅が広いです。 UITextViewの幅に拡大するには、この画像が必要です。私もimg style="width: 100%"でwebarchiveファイルを使用してみましたが、これもうまくいきませんでしたNSAttributedStringの画像をUITextViewで自動サイズ調整する

if let text = try? NSMutableAttributedString(URL: NSBundle.mainBundle().URLForResource("License agreement", withExtension: "rtfd")!, options: [NSDocumentTypeDocumentAttribute : NSRTFDTextDocumentType], documentAttributes: nil) { 

     textView.attributedText = text 
    } 

:これは、私は、文字列をロードする方法です。

rtfdなどのドキュメントをこのように表示すると、画像が幅に合わせて拡大/縮小されます。

+0

反復を 'NSAttributedString'を通じて起因' NSAttachmentAttributeName'を探しています。次に、 'UITextView'の幅に合わせてイメージのサイズを変更することができます。 – Larme

答えて

2

あなたはコメントLarmeいただきありがとうございます、私はそれがこのように動作させるために管理:

override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews() 
    prepareTextImages() 
} 

private var text: NSMutableAttributedString? 
private func prepareTextImages() { 
    let width = CGRectGetWidth(self.view.frame) - self.containerView.frame.origin.x * 2 - 10 
    text?.enumerateAttribute(NSAttachmentAttributeName, inRange: NSRange(location: 0, length: text!.length), options: [], usingBlock: { [width] (object, range, pointer) in 
     let textViewAsAny: Any = self.textView 
     if let attachment = object as? NSTextAttachment, let img = attachment.imageForBounds(self.textView.bounds, textContainer: textViewAsAny as? NSTextContainer, characterIndex: range.location) { 
      if attachment.fileType == "public.png" { 
       let aspect = img.size.width/img.size.height 
       if img.size.width <= width { 
        attachment.bounds = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height) 
        return 
       } 
       let height = width/aspect 
       attachment.bounds = CGRect(x: 0, y: 0, width: width, height: height) 
      } 
     } 
     }) 
} 

この小さな問題があります - スクロールするときに、この画像を見たとき、私は小さなラグを参照してください。サイズを変更せずにフルサイズの画像で起こっていません。誰かがこれの理由を考えることができますか?

+0

帰ってきたテキストを 'viewDidLayoutSubviews()'にもう一度セットするのはなぜですか? – Larme

+0

あなたが正しいです、私はこれを後で削除しました。ここでこれを変更するのを忘れました。 –

+0

それは私の時間を救った、ありがとうDamianDudycz –

0

あなたはTextViewにのattributeTextプロパティをpo場合は、それがこのように多くのオブジェクトを持っていたことができます。

NSAttachment = "<NSTextAttachment: 0x6000002a39c0> \"XXXXXXX.jpg\""; 
NSColor = "kCGColorSpaceModelRGB 0 0 0 1 "; 
NSFont = "<UICTFont: 0x7f8356400f60> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 12.00pt"; 
NSKern = 0; 
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 12, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 15/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0"; 
NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0 1 "; 
NSStrokeWidth = 0; 

は、その後、あなたは NSTextAttachmentBingoであることがわかります。だから、 enumこれらのオブジェクトは、 NSTextAttachmentを取得し、それの境界を変更します。 は(私は画像を右画面に10pxのマージンを持って設定)

[attributeString enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, attributeString.length) options:0 usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) { 

      if (value) { 

       if ([value isKindOfClass:[NSTextAttachment class]]) { 

        NSTextAttachment *attach = value; 
        CGFloat scale = SCREEN_WIDTH/attach.bounds.size.width; 
        CGRect newRect = CGRectMake(attach.bounds.origin.x, attach.bounds.origin.y,SCREEN_WIDTH-10, attach.bounds.size.height*scale); 
        attach.bounds = newRect; 
       } 
      } 
     }]; 
関連する問題