2017-12-22 16 views
0

テキストビュー内にイメージ付きの属性付き文字列があり、属性付きイメージが表示されています。問題は、テキストビュー内のテキストを置き換えるイメージを追加するたびに問題になります。帰属画像を追加して、カーソルを画像の最後に置き、画像の下にテキストを追加することができます。これは私がこれまでに試したことです、事前に感謝します。UItextview内の属性付き文字列イメージの置換

let attachment = NSTextAttachment() 
    attachment.image = selectedImage 
    let newImageWidth = (textView.bounds.size.width - 10) 
    let scale = newImageWidth/(selectedImage.size.width) 
    let newImageHeight = selectedImage.size.height * scale 
    attachment.bounds = CGRect(x: 0, y: 0, width: newImageWidth, height: newImageHeight) 
    let myAttributedString = NSAttributedString(attachment: attachment) 
    textView.textStorage.insert(myAttributedString, at: textView.selectedRange.location) 

答えて

0

私はNSMutableAttributedString()を作成し、カーソルを画像の下に置く新しい線属性を追加しました。これは誰か助けて欲しい。

var mutableAttrString = NSMutableAttributedString() 

    let attachment = NSTextAttachment() 
    attachment.image = selectedImage 
    let newImageWidth = (textView.bounds.size.width - 10) 
    let scale = newImageWidth/(selectedImage.size.width) 
    let newImageHeight = selectedImage.size.height * scale 
    attachment.bounds = CGRect(x: 0, y: 0, width: newImageWidth, height: newImageHeight) 
    let myAttributedString = NSAttributedString(attachment: attachment) 
    let text:[String:Any] = [NSFontAttributeName:UIFont.boldSystemFont(ofSize: 20)] 

    // Fixed my problem of having image replacing text, instead I place the text at the bottom of the image 
    let textAttr = NSMutableAttributedString(string: "bottom of image \n", attributes: text) 

    mutableAttrString.append(myAttributedString) 
    mutableAttrString.append(textAttr) 

    textView.attributedText = mutableAttrString 
関連する問題