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


转载于:https://my.oschina.net/u/1169661/blog/396710

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值