NSTextAttachment使textview字体大小更小

时间:2017-12-22 15:53:43

标签: ios swift uitextview

我在textview中得到了一个属性字符串,图像显示正常。但是,当我将图像添加到textview时,textview的原始字体变小。我希望添加图像后字体保持不变。我不是造成这种情况的原因,任何帮助都会赞赏。

这是我到目前为止所尝试的:

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)]
    let textAttr = NSMutableAttributedString(string: "bottom of image \n", attributes: text)

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

    textView.attributedText = mutableAttrString

After image is added

enter image description here

0 个答案:

没有答案