iOS 富文本初探

來源:互聯網
上載者:User

標籤:

有的時候啊,我們需要在一行或者多行文本中顯示不同顏色,不同字型大小的文字,甚至於有的需要點擊,有的不需要。這統稱為富文本。

在網頁中,有很多類似的應用。除開網頁,我現在遇到的這種情況也是非用不可,使用者政策和使用者協議在多語言的實現中,考慮到自適應,就必須顯示在同一個控制項中(UILabel/UITextView).

NSMutableAttributedString/NSAttributedString用來表示富文本。

不如我們有一段文本,中間有兩段是要求不同顏色顯示,可以點擊的,因為是國際化,每一段的長度都不一樣,所以索性全部拆開了。

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

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

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

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

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

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

        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


iOS 富文本初探

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.