日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

iOS?UITextView?實(shí)現(xiàn)類似微博的話題、提及用戶效果_IOS

作者:XboxYan ? 更新時(shí)間: 2022-08-01 編程語言

最近接了一個(gè)需求,在發(fā)布動(dòng)態(tài)的時(shí)候,增加類似微博的#話題#、@提及用戶的效果,在此做一簡(jiǎn)要記錄。

#話題#

最終效果是:

  • 編輯過程中#話題內(nèi)容#實(shí)時(shí)高亮

  • 高亮部分可以響應(yīng)點(diǎn)擊事件

1.高亮

基本思路是:使用正則匹配出成對(duì)的#,再利用UITextView的富文本實(shí)現(xiàn)高亮效果。

func refreshTopicStyle() {
        
        let regex = try! NSRegularExpression(pattern: "此處填寫正則表達(dá)式",
                                             options:[NSRegularExpression.Options.caseInsensitive])
        // 注意點(diǎn)
        let totalRange = NSMakeRange(0, (inputTextView.attributedText.string as NSString).length)
        let results = regex.matches(in: inputTextView.attributedText.string,
                                    options: NSRegularExpression.MatchingOptions.init(rawValue: 0),
                                    range: totalRange)
        let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: inputTextView.attributedText.string)
        attributedString.setAttributes(normalAttributes, range: totalRange)
        
        for result in results {
            attributedString.setAttributes(topicAttributes, range: result.range)
        }
        inputTextView.attributedText = attributedString
    }

這有一個(gè)注意點(diǎn),計(jì)算 totalRange 前,先將 String 轉(zhuǎn)成了 NSString,這是因?yàn)榇颂?NSRange 中的 length 需要的是 UTF-16 長(zhǎng)度,也就是與 NSString 的 length 定義一致,而 Swift 中的 String 沒有 length 只有 count,指的是字符數(shù),當(dāng)文本中出現(xiàn) emoji 表情時(shí),二者就不一致了。

當(dāng)然,也有一些其他辦法來處理,如:

let lengthA = inputTextView.textStorage.length
let lengthB = inputTextView.attributedText.string.utf16.count

2.點(diǎn)擊事件

實(shí)現(xiàn)高亮部分的點(diǎn)擊事件,目前有3種實(shí)現(xiàn)方案:

  • 直接給UITextView添加點(diǎn)擊事件
  • 通過設(shè)置LinkAttribute,利用超文本鏈接的點(diǎn)擊實(shí)現(xiàn)
  • 重寫UITextViewtouches...方法

其中,第二種只限于在非編輯狀態(tài)(即 textView.isEditable = false)下的點(diǎn)擊,故排除,①、③均可,本文采用第一種,主要實(shí)現(xiàn)如下:

inputTextView.addTapGesture(self, handler: #selector(tapAttributedText(tap:)))
@objc private func tapAttributedText(tap: UITapGestureRecognizer) {
    guard tap.isKind(of: UITapGestureRecognizer.self), let textView = tap.view as? UITextView else {
        return
    }
    let layoutManager = textView.layoutManager
    var tapLocation = tap.location(in: textView)
    tapLocation.x -= textView.textContainerInset.left
    tapLocation.y -= textView.textContainerInset.top
    let characterIndex = layoutManager.characterIndex(for: tapLocation,
                                                      in: textView.textContainer,
                                                      fractionOfDistanceBetweenInsertionPoints: nil)
    for result in getCheckResult(format: Constants.TopicRegularExpression, text: inputTextView.attributedText.string) {
        if result.range.location < characterIndex, characterIndex < result.range.location + result.range.length {
            // 此處響應(yīng)點(diǎn)擊事件
            MBProgressHUD.showOnlyText(to: self.view, title: "美好時(shí)光")
            return
        }
    }
    inputTextView.becomeFirstResponder()
}

@提及用戶

  • 編輯過程中 @提及用戶 實(shí)時(shí)高亮,且只允許選取的用戶名高亮,手動(dòng)輸入不高亮;

  • 點(diǎn)擊刪除鍵的時(shí)候,一次性刪除整個(gè)高亮部分

1.高亮

  • 記錄位置

本來準(zhǔn)備用正則匹配的,但因?yàn)橹辉试S選取的用戶名高亮,純手動(dòng)輸入的不高亮,所以使用正則匹配就不合理了,這里采用實(shí)時(shí)記錄、更新已選取用戶名位置的方式實(shí)現(xiàn)。

/// 用來保存已選取用戶信息的結(jié)構(gòu)體
struct UserInfo {
    /// 用戶名
    var userName: String
    /// 位置信息
    var range: NSRange
    /// 用于臨時(shí)替換的等長(zhǎng)字符串
    var placeholder: String
}
  • 臨時(shí)替換

因?yàn)?code>#話題#和@提及用戶可以同時(shí)存在,所以需要考慮可能互相影響的問題,比如@提及用戶中間可能出現(xiàn)#,導(dǎo)致前后話題的正則匹配發(fā)生錯(cuò)亂。

解決方案是:先使用一個(gè)@開頭且與@提及用戶等長(zhǎng)的字符串替換@提及用戶,再執(zhí)行#話題#的正則匹配,最后再換回來。

2.整體刪除

刪除操作分為兩步:第一次點(diǎn)刪除僅選中整個(gè)用戶名(提醒用戶是整體刪除);第二次點(diǎn)刪除才真的刪除文本。

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if text == "" {
        for (num, user) in usersArray.enumerated() { // usersArray 用于存放已選取的用戶信息
            // ②刪除選中的用戶名
            if textView.selectedRange.location <= user.range.location && NSMaxRange(user.range) <= NSMaxRange(textView.selectedRange) {
                textView.replace(textView.selectedTextRange ?? UITextRange(), withText: "")
                return false
            }
            // ①選中用戶名
            if textView.selectedRange.length == 0 && (textView.selectedRange.location == user.range.location + user.range.length) {
                textView.selectedRange = user.range
                return false
            }
        }
    }
}

原文鏈接:https://juejin.cn/post/7105590718336335902

欄目分類
最近更新