iOSSwift富文本

iOS 利用NSMutableAttributedString为

2017-04-09  本文已影响113人  MrZhaoCn

在iOS开发中,经常需要对一段文本的特殊文字显示不同的颜色,比如在发朋友圈时@某人时要把这个人高亮,同时点击删除按钮这个人的名字要一起删除而不是一个字一个字删,笔者公司目前开发的发表周报,日报功能就遇到这方面的需求,现总结如下:
UITextView/UITextField/UILabel有一个属性叫:attributedText,该属性为NSAttributedString类型,该类的作用是可以为一段文本的不同rang的字体设置不同的大小,颜色,等。可以利用该类实现富文本,通常我们使用它的可变类型NSMutableAttributedString实现。

NSMutableAttributedString具有如下几个方法:

//为某一范围内文字设置多个属性 
setAttributes: range: 
//为某一范围内文字添加某个属性 
addAttribute: value: range: 
//为某一范围内文字添加多个属性 
addAttributes: range: 
//移除某范围内的某个属性 
removeAttribute: range:

NSMutableAttributedString的几个常见属性:

NSFontAttributeName 
字体
NSParagraphStyleAttributeName 
段落格式 
NSForegroundColorAttributeName 
字体颜色
NSBackgroundColorAttributeName  
背景颜色
NSStrikethroughStyleAttributeName
删除线格式
NSUnderlineStyleAttributeName     
下划线格式
NSStrokeColorAttributeName       
删除线颜色
NSStrokeWidthAttributeName
删除线宽度
NSShadowAttributeName 
阴影

下面进行简单的应用:
1.初始化一个UITextView。

        let string = "今日主要工作:完成NSMutableAttributedString学习,@点点"
        //UITextView初始化
        textView = UITextView()
        textView?.delegate = self
        textView?.alwaysBounceVertical = true
        textView?.font = UIFont.systemFont(ofSize: 14)
        textView?.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 200)
        textView?.text = string

2.此时我们相对@点点几个字进行高亮,模仿@mo某人效果,我们只需要找到@点点在文本中的rang,然后设置textView的attributedText属性即可。

        let mutableAtributSting = NSMutableAttributedString(string: string)
        //找到@点点rang
        let rang = (string as NSString).range(of: "@点点")
        //颜色
        mutableAtributSting.addAttribute(NSForegroundColorAttributeName, value: UIColor(red: 4/255.0, green: 186/255.0, blue: 215/255.0, alpha: 1.0), range:rang)
        //字体大小
        mutableAtributSting.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 18), range: rang)
        textView?.attributedText = mutableAtributSting

以上就完成了对@点点几个字的高亮,效果如下图所示:

WechatIMG1.jpeg

3.现在如果光标聚焦在点点后面(或者在两个点字之间)点击删除按钮,需要将@点点这三个字一起删除。首先我们得知道什么时候点击了删除按钮,可以在textView的代理方法中做文章,当text为空时表示点击了删除按钮,此时我们判断光标的位置是否在@点点的rang范围内即可。

 func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    }

代码如下

 func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if !text.isEmpty {//不是删除操作
            return true
        }
        // 计算当前光标相对于文本开始位置的偏移量
        let cursorOffset = textView.offset(from: textView.beginningOfDocument, to: (textView.selectedTextRange?.start)!)
        let rang = (textView.text as NSString).range(of: "@点点")
        if rang.location <= cursorOffset && rang.length + rang.location >= cursorOffset {//找到了
            textView.text = (textView.text as NSString).replacingCharacters(in: rang, with: " ")
            let cursorRange = NSRange(location:rang.location, length: 0)
            textView.selectedRange = cursorRange
            return false
        }
        return true
    }

以上就是NSMutableAttributedString的基本使用及对于UITextView如何将某几个字作为一个整体删除。

上一篇 下一篇

猜你喜欢

热点阅读