iOS 富文本初探

有的时候啊,我们需要在一行或者多行文本中显示不同颜色,不同字号的文字,甚至于有的需要点击,有的不需要。这统称为富文本。

在网页中,有很多类似的应用。除开网页,我现在遇到的这种情况也是非用不可,用户政策和用户协议在多语言的实现中,考虑到自适应,就必须显示在同一个控件中(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


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。