iOS 富文本初探

有的時候啊,咱們須要在一行或者多行文本中顯示不一樣顏色,不一樣字號的文字,甚至於有的須要點擊,有的不須要。這統稱爲富文本。
git

在網頁中,有不少相似的應用。除開網頁,我如今遇到的這種狀況也是非用不可,用戶政策和用戶協議在多語言的實現中,考慮到自適應,就必須顯示在同一個控件中(UILabel/UITextView).github

NSMutableAttributedString/NSAttributedString用來表示富文本。app

不如咱們有一段文本,中間有兩段是要求不一樣顏色顯示,能夠點擊的,由於是國際化,每一段的長度都不同,因此索性所有拆開了。url

var attrDic1 = [NSFontAttributeName: UIFont.systemFontOfSize(15)]spa

        var str1 = NSLocalizedString("register prompt first segment", comment: "register prompt first segment").net

        var attrDic2 = [NSFontAttributeName: UIFont.systemFontOfSize(15), NSForegroundColorAttributeName: UIColor(red: 59/255, green: 126/255, blue: 55/255, alpha: 1), NSLinkAttributeName:"http://www.baidu.com"]orm

        var str2 = NSLocalizedString("register prompt second segment", comment: "register prompt second segment")blog

        var attrDic3 = [NSFontAttributeName: UIFont.systemFontOfSize(15)]three

        var str3 = NSLocalizedString("register prompt three segment", comment: "register prompt three segment")get

        var attrDic4 = [NSFontAttributeName: UIFont.systemFontOfSize(15), NSForegroundColorAttributeName: UIColor(red: 59/255, green: 126/255, blue: 55/255, alpha: 1), NSLinkAttributeName:"http://www.baidu.com"]

        var str4 = NSLocalizedString("register prompt four segment", comment: "register prompt four segment")

        var attrDic5 = [NSFontAttributeName: UIFont.systemFontOfSize(15)]

        var str5 = NSLocalizedString("register prompt five segment", comment: "register prompt five segment")

        

        var allStr = str1+str2+str3+str4+str5

        var attrStr1 = NSMutableAttributedString(string: str1, attributes: attrDic1)

        var attrStr2 = NSMutableAttributedString(string: str2, attributes: attrDic2)

        var attrStr3 = NSMutableAttributedString(string: str3, attributes: attrDic3)

        var attrStr4 = NSMutableAttributedString(string: str4, attributes: attrDic4)

        var attrStr5 = NSMutableAttributedString(string: str5, attributes: attrDic5)

        

        attrStr1.appendAttributedString(attrStr2);

        attrStr1.appendAttributedString(attrStr3);

        attrStr1.appendAttributedString(attrStr4);

        attrStr1.appendAttributedString(attrStr5);

        vPromptTextView.attributedText = attrStr1

//        vPromptTextView.linkTextAttributes = attrDic2

//        vPromptTextView.userInteractionEnabled = true

//        vPromptTextView.scrollEnabled = false

//        vPromptTextView.editable = false

//        vPromptTextView.selectable = true

//        vPromptTextView.textContainer.lineFragmentPadding = 0

//        vPromptTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0)

//        vPromptTextView.delegate = self;

        vPromptTextView.textAlignment = NSTextAlignment.Center

//        vPromptTextView.dataDetectorTypes = UIDataDetectorTypes.All

        var tap = UITapGestureRecognizer(target: self, action: "tappedTextView:")

        vPromptTextView.addGestureRecognizer(tap)

這裏是5段內容,使用了國際化,UILabel說是不能點擊,事實上,我把全部的註釋都打開,同時實現了了UITextView的委託,也沒辦法點擊,好吧,我不知道問題在哪裏。據網上搜索到的內容講,能夠點擊以後,也就是用safiri打開一段連接,這樣的話和個人實際應用場景有差異,因此使用了tapgesture的方式,看看具體實現:

    func tappedTextView(tapGesture:UITapGestureRecognizer){

        if tapGesture.state != UIGestureRecognizerState.Ended {

            return

        }

        

        var textView = tapGesture.view as UITextView

        var tapLocation = tapGesture.locationInView(textView)

        var textPosition = textView.closestPositionToPoint(tapLocation)

        

        var attributes = textView.textStylingAtPosition(textPosition, inDirection: UITextStorageDirection.Backward)

        if let url = attributes[NSLinkAttributeName] as? String{

//            UIApplication.sharedApplication().openURL(NSURL(string: url)!)

            link = url

            self.performSegueWithIdentifier("openLink", sender: self)

        }

    }


這樣就能夠實現我本身想要的內容的,也能夠實現內部點擊。

圖文混排下一步就是core text 渲染,這個內容仍是很豐富的。

iOS7 新增了TextKit,是在Core Text之上進行了封裝。

iOS的內容是至關豐富的,是在不敢稱iOS高手了,學的越多,發現不知道的東西越多。

最後,推薦兩個第三方庫


兩個庫:DTCoreText=> http://blog.cnbang.net/tech/2630/

TTTAttributedLabel=> https://github.com/TTTAttributedLabel/TTTAttributedLabel

相關文章
相關標籤/搜索