如何将背景图像添加到UITextView

加布·庞德

我正在尝试将背景图像添加到我的UITextView中,但是似乎没有选择这样做。我尝试添加一个imageView并将图像作为子视图

self.bioTextView.delegate = self
let imgView = UIImageView(frame: self.bioTextView.frame)
imgView.image = #imageLiteral(resourceName: "text_field_bio")
self.bioTextView.addSubview(imgView)

但这也不起作用。textView背景的颜色很清晰。我也尝试过将imgView发送到后面,但这也不起作用。

安布·卡尔提克

将foll0wing添加到两行并检查一次

bioTextView.addSubview(imgView)
bioTextView.sendSubview(toBack: imgView)

或使用

bioTextView.backgroundColor = UIColor(patternImage: UIImage(named: "Image.png"))

更新

let textView = UITextView(frame: CGRect(x: CGFloat(20), y: CGFloat(20), width: CGFloat(400), height: CGFloat(400 )))
    textView.text = "this is a test \n this is test \n this is a test"
    let img = UIImageView(frame: textView.bounds)
    img.image = UIImage(named: "1.jpg")
    textView.backgroundColor = UIColor.clear
    textView.addSubview( img)
    self.view.addSubview(textView)
    textView.sendSubview(toBack: img)

你得到的输出

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章