diff --git a/submodules/ChatListUI/Sources/Node/ChatListItem.swift b/submodules/ChatListUI/Sources/Node/ChatListItem.swift index 2d8ea068ab..c01105c60f 100644 --- a/submodules/ChatListUI/Sources/Node/ChatListItem.swift +++ b/submodules/ChatListUI/Sources/Node/ChatListItem.swift @@ -1822,11 +1822,23 @@ class ChatListItemNode: ItemListRevealOptionsItemNode { let mutableString = NSMutableAttributedString(string: messageText, font: textFont, textColor: theme.messageTextColor) if let spoilers = spoilers { for range in spoilers { + var range = range + if range.location > mutableString.length { + continue + } else if range.location + range.length > mutableString.length { + range.length = mutableString.length - range.location + } mutableString.addAttribute(NSAttributedString.Key(rawValue: TelegramTextAttributes.Spoiler), value: true, range: range) } } if let customEmojiRanges = customEmojiRanges { for (range, attribute) in customEmojiRanges { + var range = range + if range.location > mutableString.length { + continue + } else if range.location + range.length > mutableString.length { + range.length = mutableString.length - range.location + } mutableString.addAttribute(ChatTextInputAttributes.customEmoji, value: attribute, range: range) } } @@ -1857,6 +1869,12 @@ class ChatListItemNode: ItemListRevealOptionsItemNode { for range in chatListSearchResult.resultRanges { let stringRange = NSRange(range, in: chatListSearchResult.text) if stringRange.location >= 0 && stringRange.location + stringRange.length <= composedString.length { + var stringRange = stringRange + if stringRange.location > composedString.length { + continue + } else if stringRange.location + stringRange.length > composedString.length { + stringRange.length = composedString.length - stringRange.location + } composedString.addAttribute(.foregroundColor, value: theme.messageHighlightedTextColor, range: stringRange) } } diff --git a/submodules/TextFormat/Sources/StringWithAppliedEntities.swift b/submodules/TextFormat/Sources/StringWithAppliedEntities.swift index 7400c9b364..ec6f8826a0 100644 --- a/submodules/TextFormat/Sources/StringWithAppliedEntities.swift +++ b/submodules/TextFormat/Sources/StringWithAppliedEntities.swift @@ -18,8 +18,10 @@ public func chatInputStateStringWithAppliedEntities(_ text: String, entities: [M if nsString == nil { nsString = text as NSString } + if range.location >= stringLength { + continue + } if range.location + range.length > stringLength { - range.location = max(0, stringLength - range.length) range.length = stringLength - range.location } switch entity.type { @@ -75,7 +77,6 @@ public func stringWithAppliedEntities(_ text: String, entities: [MessageTextEnti if range.location > stringLength { continue } else if range.location + range.length > stringLength { - range.location = max(0, stringLength - range.length) range.length = stringLength - range.location } switch entity.type {