The Swift Code之UITextView的創建,以及不同的狀態和外觀

UITextView顧名思義用來顯示文本的,其實文本內容可以有不同類型的,同UILabel一樣,使用NSAttributeString來設置文本的類型.

1.創建一個基本的UITextView,它可以有編輯狀態,似乎是UITextField的擴展,多行文本編輯嘛.

  var text: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, 5) //理解內容到邊框的距離
  text.dataDetectorTypes = UIDataDetectorTypes.Link

2.創建一個顯示HTML標籤的UITextView

var text1:UITextView = UITextView(frame: CGRect(x: 50, y: 160, width: 300, height: 100))
var data = "<img src='http://www.wutongwei.com/upload/2015/02/12/1423731853934.jpg' width=50 ><a href='http://www.wutongwei.com'>吳統威</a>的博客是一個分享技術的一個博客網站,分享編程技術,操作系統技術,移動開發技術,數據庫技術等".dataUsingEncoding(NSUTF32StringEncoding, allowLossyConversion: true)
var textatrr1 = 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)方法,設置不同類型的協議的請求

 var text2:UITextView = UITextView(frame: CGRect(x: 50, y: 270, width: 300, height: 150))
 var textatrr2 = 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纔能有點擊跳轉
//自定義協議,處理相關邏輯
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
    NSLog("鏈接地址:\(URL.description)")
    return true
}

附:全部代碼

import UIKit

class ViewController: UIViewController,UITextViewDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        //普通的UITextView
        var text: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, 5) //理解內容到邊框的距離
        text.dataDetectorTypes = UIDataDetectorTypes.Link
        
        self.view.addSubview(text)
        
        //UITextView顯示html文本
        var text1:UITextView = UITextView(frame: CGRect(x: 50, y: 160, width: 300, height: 100))
        var data = "<img src='http://www.wutongwei.com/upload/2015/02/12/1423731853934.jpg' width=50 ><a href='http://www.wutongwei.com'>吳統威</a>的博客是一個分享技術的一個博客網站,分享編程技術,操作系統技術,移動開發技術,數據庫技術等".dataUsingEncoding(NSUTF32StringEncoding, allowLossyConversion: true)
        var textatrr1 = NSMutableAttributedString(data: data!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], documentAttributes: nil, error: nil)
        
        text1.attributedText = textatrr1
        
        text1.editable = false
        text1.selectable = false //設置爲True時,鏈接可以點擊
     
        self.view.addSubview(text1)
        
        
        //帶鏈接的屬性文本,追加文本,鏈接配合textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange)方法,設置不同類型的協議的請求
        var text2:UITextView = UITextView(frame: CGRect(x: 50, y: 270, width: 300, height: 150))
        var textatrr2 = 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纔能有點擊跳轉
        
        self.view.addSubview(text2)
        
        
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //自定義協議,處理相關邏輯
    func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
        NSLog("鏈接地址:\(URL.description)")
        return true
    }
    
    
}

效果圖

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

轉載至吳統威的博客:http://www.wutongwei.com/front/infor_showone.tweb?id=90

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章