ios提供的api中,UITextField有background属性可以直接uiTextField.background=…添加背景图,但是uitextview却没有这个api其实也不难,新建一个uiimageview然后添加进text就行了上代码:
let textView = UITextView()
self.view.addSubview(textView)
let image = UIImage(named: "edit_password.png")!
let imageview = UIImageView()
imageview.image = image
textView.textContainerInset=UIEdgeInsets(top: 20,left: 50,bottom: 1,right: 0)
imageview.addSubview(imageview)
imageview.sendSubviewToBack(imageview)
constrain(imageview){
v in
v.edges == v.superview!.edges
}
注意的是添加完背景之后要写sendsubviewtoback否则背景会讲文字覆盖
至于改变光标的位置就是这一句啦
textView.textContainerInset=UIEdgeInsets(top: 20,left: 50,right: 0)