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

The Swift Code之UITextView的创建,以及不同的状态和外观

发布时间:2020-12-14 02:15:26 所属栏目:百科 来源:网络整理
导读:UITextView顾名思义用来显示文本的,其实文本内容可以有不同类型的,同UILabel一样,使用NSAttributeString来设置文本的类型. 1.创建一个基本的UITextView,它可以有编辑状态,似乎是UITextField的扩展,多行文本编辑嘛. vartext:UITextView=UITextView(frame:CGRe

UITextView顾名思义用来显示文本的,其实文本内容可以有不同类型的,同UILabel一样,使用NSAttributeString来设置文本的类型.

1.创建一个基本的UITextView,它可以有编辑状态,似乎是UITextField的扩展,多行文本编辑嘛.

vartext:UITextView=UITextView(frame:CGRect(x:50,y:50,width:300,height:100))
text.text="这是基本的UITextView"
text.textContainer.lineFragmentPadding=5//行离左边的距离
text.textContainerInset=UIEdgeInsetsMake(5,5,5)//理解内容到边框的距离
text.dataDetectorTypes=UIDataDetectorTypes.Link

2.创建一个显示HTML标签的UITextView

vartext1:UITextView=UITextView(frame:CGRect(x:50,y:160,height:100))
vardata="<imgsrc='http://www.wutongwei.com/upload/2015/02/12/1423731853934.jpg'width=50><ahref='http://www.wutongwei.com'>吴统威</a>的博客是一个分享技术的一个博客网站,分享编程技术,操作系统技术,移动开发技术,数据库技术等".dataUsingEncoding(NSUTF32StringEncoding,allowLossyConversion:true)
vartextatrr1=NSMutableAttributedString(data:data!,options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType],documentAttributes:nil,error:nil)
text1.attributedText=textatrr1
text1.editable=false
text1.selectable=false//设置为True时,链接可以点击

3.创建一个带链接的属性文本,追加文本,链接配合textView(textView: UITextView,shouldInteractWithURL URL: NSURL,inRange characterRange: NSRange)方法,设置不同类型的协议的请求

vartext2:UITextView=UITextView(frame:CGRect(x:50,y:270,height:150))
vartextatrr2=NSMutableAttributedString(string:"吴统威的博客",attributes:[NSLinkAttributeName:"app://www.wutongwei.com"])
textatrr2.appendAttributedString(NSAttributedString(string:"是一个分享技术的博客网站"))
text2.attributedText=textatrr2
text2.delegate=self
text2.scrollEnabled=false
text2.editable=false
text2.selectable=true//必须设置为true才能有点击跳转
//自定义协议,处理相关逻辑
functextView(textView:UITextView,shouldInteractWithURLURL:NSURL,inRangecharacterRange:NSRange)->Bool{
NSLog("链接地址:(URL.description)")
returntrue
}

附:全部代码

importUIKit

classViewController:UIViewController,UITextViewDelegate{

overridefuncviewDidLoad(){
super.viewDidLoad()
//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.

//普通的UITextView
vartext:UITextView=UITextView(frame:CGRect(x:50,5)//理解内容到边框的距离
text.dataDetectorTypes=UIDataDetectorTypes.Link

self.view.addSubview(text)

//UITextView显示html文本
vartext1:UITextView=UITextView(frame:CGRect(x:50,error:nil)

text1.attributedText=textatrr1

text1.editable=false
text1.selectable=false//设置为True时,链接可以点击

self.view.addSubview(text1)


//带链接的属性文本,链接配合textView(textView:UITextView,inRangecharacterRange:NSRange)方法,设置不同类型的协议的请求
vartext2:UITextView=UITextView(frame:CGRect(x:50,attributes:[NSLinkAttributeName:"app://www.wutongwei.com"])

textatrr2.appendAttributedString(NSAttributedString(string:"是一个分享技术的博客网站"))

text2.attributedText=textatrr2

text2.delegate=self
text2.scrollEnabled=false
text2.editable=false
text2.selectable=true//必须设置为true才能有点击跳转

self.view.addSubview(text2)



}

overridefuncdidReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//DispoSEOfanyresourcesthatcanberecreated.
}

//自定义协议,inRangecharacterRange:NSRange)->Bool{
NSLog("链接地址:(URL.description)")
returntrue
}


}

效果图

B7F961F5-CDAC-496F-B831-7491F403A46A.jpg

转载至吴统威的博客:http://www.wutongwei.com/front/infor_showone.tweb?id=90

(编辑:李大同)

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

    推荐文章
      热点阅读