美文网首页swift
Swift开发实现编辑内容@部分高亮(类似发微博)

Swift开发实现编辑内容@部分高亮(类似发微博)

作者: 一把好刀 | 来源:发表于2019-04-30 11:36 被阅读0次

前几天项目有一个类似发微博时@用户的需求,网上也没找到比较好的第三方,自己DIY了一个,希望可以帮到有需要的朋友!

看看代码

  • 给UITextView写一个继承子类
class ZQMentionTextView: UITextView,UITextViewDelegate {

    /**提示文字*/
    var placeholderText:String?
    
    /**提示颜色*/
    var placeholderColor:UIColor?
   
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        delegate = self
    }
    

    /// 视图加载完设置提示文字及颜色
    override func draw(_ rect: CGRect) {
        text = placeholderText ?? ""
        textColor = placeholderColor ?? .black
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /// UITextViewDelegate - 文本发生改变
    func textViewDidChange(_ textView: UITextView) {

        let selectedRange = textView.markedTextRange
        /// 有联想词时取消遍历查找
        guard let start =  selectedRange?.start else {
            findAllKeywordsChangeColor(textView: textView)
            return
        }
        /// 有联想词时取消遍历查找
        if textView.position(from: start, offset: 0) != nil {
            return
        }
    }

    /// 动态修改@和空格中间的字体颜色(本扩展的核心代码)
    ///
    /// - Parameter textView: textView
    func findAllKeywordsChangeColor(textView:UITextView) {
        let string = textView.text
        let rangeDefault = textView.selectedRange
        let attributedString = NSMutableAttributedString(string: string!)
        attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.black, range: NSRange(location: 0, length: (string?.count)!))
        // 正则匹配到@和空格之间的内容
        let reg = try? NSRegularExpression(pattern: "@[^\\s]+", options: [])
        guard let matches = reg?.matches(in: string!, options: [], range: NSRange(location: 0, length: (string?.count)!)) else {
            return
        }
        for result in matches {
            let range = result.range(at: 0)
            let subStr = (attributedString.string as NSString).substring(with: range)
            let length = subStr.count
            attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: NSRange(location: range.location, length: length))
        }
        attributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 16), range: NSRange(location: 0, length: (string?.count)!))
        textView.attributedText = attributedString
        let rangeNow = NSRange(location: rangeDefault.location, length: 0)
        // 光标还原
        textView.selectedRange = rangeNow
    }
}

  • 调用
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let frame = CGRect(x: 15, y:100, width: view.frame.width-30, height: view.frame.height-150)
        let textView  = ZQMentionTextView(frame: frame)
        textView.placeholderText = "加个话题,更能被注意哦"
        textView.placeholderColor = .lightGray
        textView.font = UIFont.systemFont(ofSize: 15)
        view.addSubview(textView)
    }
}



- 效果图
效果图

相关文章

  • Swift开发实现编辑内容@部分高亮(类似发微博)

    前几天项目有一个类似发微博时@用户的需求,网上也没找到比较好的第三方,自己DIY了一个,希望可以帮到有需要的朋友!...

  • 微博的文本编辑和显示(emoji表情,@某人、链接高亮点击)

    日常开发的过程中我们经常会需要实现类似微博的文本输入框,可以自定义的emoji、@某人高亮显示、快捷删除、文本显示...

  • 新浪微博@名字高亮Objective-C实现

    app中的社交功能通常需要实现新浪微博输入“@名字”时高亮类似的功能,先看一下"@名字"高亮的规则: 输入@之后的...

  • @他人等特殊文本变色功能 textView 封装

    今天项目新增的话题功能,类似于新浪微博,可以@他人、并且@他人和话题的文本必须是高亮状态。 其实实现名字高亮状态特...

  • iOS 一个灰常牛逼的Label

    一个灰常牛逼的label可响应部分文字的点击事件,常用于聊天微博等页面。类似微博的自动高亮label,包括url、...

  • 可选类型与强制解包

    Swift 前言 写在Swift4.0发布前夕.内容还是以swift3.1为主.不会讲述过于基础的部分.类似于字符...

  • 制作一个微博文本编辑器

    本文章的Demo工程 最近需要制作一个类似发微博的界面,支持“@用户”以及“#话题#”等格式高亮,并且可输入展示自...

  • 14- 首页完善

    首页完善 实现功能 首页表情显示 特殊字符高亮显示 特殊字符点击处理 首页表情显示 步骤 将微博内容字符串生成一个...

  • 知乎想法

    产品形态: 基本Timeline为基于知乎站内关注关系的类似“微博”的内容 延伸/隐藏timeline为基于编辑精...

  • 仿微博发博的编辑框的实现

    前言: 前一阵做公司的项目,有一个需求需要实现类似微博发博的功能,包括话题、表情、图片的等功能的实现。第一反应是通...

网友评论

    本文标题:Swift开发实现编辑内容@部分高亮(类似发微博)

    本文链接:https://www.haomeiwen.com/subject/kwemnqtx.html