加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift UITextView添加背景图片,设定光标位置

发布时间:2020-12-14 06:49:05 所属栏目:百科 来源:网络整理
导读:ios提供的api中,UITextField有background属性可以直接uiTextField.background=…添加背景图,但是uitextview却没有这个api其实也不难,新建一个uiimageview然后添加进text就行了上代码: let textView = UITextView() self .view .addSubview (textView) le

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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读