2017-10-10 11 views
0

NSAttributedStringにある画像のサイズを変更すると奇妙な問題が発生します。サイズ変更の拡張機能は正常に動作していますが、イメージがNSAttributedStringに追加されると、何らかの理由で垂直方向に反転します。NSAttributedStringに追加すると画像が反転する

これはリサイズ拡張したものです:私はブレークポイントを設定し、画像を点検し、彼らはきた

newAttributedString.enumerateAttribute(NSAttributedStringKey.attachment, in: NSMakeRange(0, newAttributedString.length), options: []) { value, range, stop in 
    if let attachement = value as? NSTextAttachment { 
     let image = attachement.image(forBounds: attachement.bounds, textContainer: NSTextContainer(), characterIndex: range.location)! 

     let newImage = image.resize(containerWidth: markdown.bounds.width) 
     let newAttribute = NSTextAttachment() 
     newAttribute.image = newImage 
     newAttributedString.addAttribute(NSAttributedStringKey.attachment, value: newAttribute, range: range) 
    } 
} 

extension NSImage { 
    func resize(containerWidth: CGFloat) -> NSImage { 

    var scale : CGFloat = 1.0 
    let currentWidth = self.size.width 
    let currentHeight = self.size.height 

    if currentWidth > containerWidth { 
     scale = (containerWidth * 0.9)/currentWidth 
    } 

    let newWidth = currentWidth * scale 
    let newHeight = currentHeight * scale 

    self.size = NSSize(width: newWidth, height: newHeight) 

    return self 
    } 
} 

そしてここでは、画像の列挙は、属性付き文字列でありますすべてが正しい回転になりますが、次の行に達した場合を除きます。

newAttributedString.addAttribute(NSAttributedStringKey.attachment, value: newAttribute, range: range) 

wh画像が垂直方向に反転される。

私はこの垂直フリップを引き起こしている可能性があります。これを修正する方法はありますか?

答えて

0

私はそれを理解しました。それは私が作っていたよりずっと簡単でした。

画像がNSTextViewに追加されるNSAttribuetdStringにあったので、私は、NSAttributedStringで各画像のサイズを変更する必要はありませんでしたが、むしろ私はちょうど

markdown.layoutManager?.defaultAttachmentScaling = NSImageScaling.scaleProportionallyDown 

一つでNSTextView内部の添付ファイルのスケーリングを設定する必要がありましたラインはそれだけで完了しました

0

あなたはNSTextAttachmentのために開発者向けドキュメントを見れば:

は「テキスト座標系における受信機のグラフィック表現のレイアウト境界を定義し、次のよう

https://developer.apple.com/documentation/uikit/nstextattachment

boundsパラメータが定義されています"

CoreTextを使用してテキストをレイアウトする場合、座標を反転する必要があることを知っています。境界線のパラメータを垂直反射も。

希望に役立ちます。

関連する問題