【问题标题】:how to add a background image to a UITextView如何将背景图像添加到 UITextView
【发布时间】:2017-02-19 06:27:57
【问题描述】:

我正在尝试向我的 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 发送到后面,但这也没有用。

【问题讨论】:

  • 在textView后面放一个image view,让textview背景清晰

标签: ios swift swift3 uitextview


【解决方案1】:

添加以下两行并检查一次

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)

你得到输出

【讨论】:

  • 我正在使用第二个选项并且它正在工作,但是有没有办法将 UIImage 剪辑到 TextView 的框架,或者设置图像调整大小模式,以便它调整自身的大小到文本视图?
  • @GabeSpound - 选择权归你
  • @GabeSpound - 设置图像大小调整模式,使其自身调整为 textView 的框架 - 它会根据 superviee 自动调整框架,
猜你喜欢
  • 1970-01-01
  • 2012-05-12
  • 2016-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-19
  • 1970-01-01
  • 2015-11-06
相关资源
最近更新 更多