瀏覽代碼

highlight all occourences of a search string within a message

cyberta 3 年之前
父節點
當前提交
157e6d998d
共有 2 個文件被更改,包括 10 次插入7 次删除
  1. 2 2
      deltachat-ios/Chat/ChatViewController.swift
  2. 8 5
      deltachat-ios/Helper/MessageUtils.swift

+ 2 - 2
deltachat-ios/Chat/ChatViewController.swift

@@ -975,7 +975,7 @@ class ChatViewController: UITableViewController {
                                                                 currentYPos -
                                                                 currentYPos -
                                                                 2 * UIFont.preferredFont(for: .body, weight: .regular).lineHeight -
                                                                 2 * UIFont.preferredFont(for: .body, weight: .regular).lineHeight -
                                                                 padding),
                                                                 padding),
-                                                    animated: animated)
+                                                    animated: false)
 
 
                     return
                     return
                 }
                 }
@@ -1851,7 +1851,7 @@ extension ChatViewController: UISearchResultsUpdating {
         debounceTimer?.invalidate()
         debounceTimer?.invalidate()
         debounceTimer = Timer.scheduledTimer(withTimeInterval: 0.25, repeats: false) { _ in
         debounceTimer = Timer.scheduledTimer(withTimeInterval: 0.25, repeats: false) { _ in
             let searchText = searchController.searchBar.text ?? ""
             let searchText = searchController.searchBar.text ?? ""
-            DispatchQueue.global().async {
+            DispatchQueue.global(qos: .userInteractive).async {
                 let resultIds = self.dcContext.searchMessages(chatId: self.chatId, searchText: searchText)
                 let resultIds = self.dcContext.searchMessages(chatId: self.chatId, searchText: searchText)
                 DispatchQueue.main.async { [weak self] in
                 DispatchQueue.main.async { [weak self] in
 
 

+ 8 - 5
deltachat-ios/Helper/MessageUtils.swift

@@ -142,11 +142,14 @@ public class MessageUtils {
             ]
             ]
             let mutableAttributedString = NSMutableAttributedString(string: messageText, attributes: fontAttributes)
             let mutableAttributedString = NSMutableAttributedString(string: messageText, attributes: fontAttributes)
 
 
-            if let searchText = searchText, messageText.lowercased().contains(searchText.lowercased()) {
-                let range = (messageText.lowercased() as NSString).range(of: searchText.lowercased())
-                mutableAttributedString.addAttribute(.font, value: UIFont.preferredFont(for: .body, weight: .semibold), range: range)
-                if highlight {
-                    mutableAttributedString.addAttribute(.backgroundColor, value: DcColors.highlight, range: range)
+            if let searchText = searchText {
+                let ranges = messageText.ranges(of: searchText, options: .caseInsensitive)
+                for range in ranges {
+                    let nsRange = NSRange(range, in: messageText)
+                    mutableAttributedString.addAttribute(.font, value: UIFont.preferredFont(for: .body, weight: .semibold), range: nsRange)
+                    if highlight {
+                        mutableAttributedString.addAttribute(.backgroundColor, value: DcColors.highlight, range: nsRange)
+                    }
                 }
                 }
             }
             }
             return mutableAttributedString
             return mutableAttributedString