|
@@ -630,6 +630,8 @@ extension String {
|
|
|
|
|
|
public func richText(isEditing: Bool = false, isSearching: Bool = false, textSearch: String = "", group_id: String = "", listMentionInTextField: [User] = []) -> NSMutableAttributedString {
|
|
|
let font = UIFont.systemFont(ofSize: 12)
|
|
|
+ let boldFont = UIFont.boldSystemFont(ofSize: 12)
|
|
|
+ let italicFont = UIFont.italicSystemFont(ofSize: 12)
|
|
|
let textUTF8 = String(self.utf8)
|
|
|
let finalText = NSMutableAttributedString(string: textUTF8, attributes: [NSAttributedString.Key.font: font])
|
|
|
let boldSign: Character = "*"
|
|
@@ -645,10 +647,9 @@ extension String {
|
|
|
var continueCheckingBold = false
|
|
|
var totalEmoji = 0
|
|
|
for i in 0..<rangeBold.count {
|
|
|
- if lastFirstRange == -1 {
|
|
|
- let stringCharStart = textUTF8.substring(from: rangeBold[i].startIndex - 1, to: rangeBold[i].startIndex - 1)
|
|
|
- let stringCharEnd = textUTF8.substring(from: rangeBold[i].endIndex + 1, to: rangeBold[i].endIndex + 1)
|
|
|
- if (rangeBold[i].startIndex == 0 || checkCharBefore(char: stringCharStart) || (checkCharRich(char: stringCharStart) && stringCharStart == stringCharEnd)) {
|
|
|
+ if rangeBold[i].startIndex > lastFirstRange {
|
|
|
+ let charStart: Character = rangeBold[i].startIndex != 0 ? Array(textUTF8.substring(from: rangeBold[i].startIndex - 1, to: rangeBold[i].startIndex - 1))[0] : Array("0")[0]
|
|
|
+ if (rangeBold[i].startIndex == 0 || (!charStart.isLetter && !charStart.isNumber)) {
|
|
|
lastFirstRange = rangeBold[i].startIndex
|
|
|
continueCheckingBold = true
|
|
|
} else {
|
|
@@ -667,7 +668,7 @@ extension String {
|
|
|
let countEmojiBefore = finalText.string.substring(from: 0, to: lastFirstRange - (2*countRemoveBoldSign)).countEmojiCharacter()
|
|
|
let countEmoji = finalText.string.substring(from: lastFirstRange - (2*countRemoveBoldSign), to: rangeBold[i].endIndex - (2*countRemoveBoldSign)).countEmojiCharacter()
|
|
|
totalEmoji = countEmoji + countEmojiBefore
|
|
|
- finalText.addAttribute(.font, value: font.bold, range: NSRange(location: lastFirstRange - (2*countRemoveBoldSign) + countEmojiBefore, length: (rangeBold[i].endIndex + countEmoji + 1) - lastFirstRange))
|
|
|
+ finalText.addAttribute(.font, value: boldFont, range: NSRange(location: lastFirstRange - (2*countRemoveBoldSign) + countEmojiBefore, length: (rangeBold[i].endIndex + countEmoji + 1) - lastFirstRange))
|
|
|
if !isEditing{
|
|
|
finalText.mutableString.replaceOccurrences(of: "\(boldSign)", with: "", options: .literal, range: NSRange(location: lastFirstRange + countEmojiBefore - (2*countRemoveBoldSign), length: 1))
|
|
|
finalText.mutableString.replaceOccurrences(of: "\(boldSign)", with: "", options: .literal, range: NSRange(location: rangeBold[i].endIndex + totalEmoji - (2*countRemoveBoldSign) - 1, length: 1))
|
|
@@ -676,7 +677,8 @@ extension String {
|
|
|
finalText.addAttribute(.foregroundColor, value: UIColor.gray, range: NSRange(location: lastFirstRange + countEmojiBefore, length: 1))
|
|
|
finalText.addAttribute(.foregroundColor, value: UIColor.gray, range: NSRange(location: rangeBold[i].endIndex + totalEmoji, length: 1))
|
|
|
}
|
|
|
- lastFirstRange = -1
|
|
|
+ lastFirstRange = rangeBold[i].endIndex
|
|
|
+ continueCheckingBold = false
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -689,10 +691,9 @@ extension String {
|
|
|
var continueCheckingItalic = false
|
|
|
var totalEmoji = 0
|
|
|
for i in 0..<rangeItalic.count {
|
|
|
- if lastFirstRange == -1 {
|
|
|
- let stringCharStart = textUTF8.substring(from: rangeItalic[i].startIndex - 1, to: rangeItalic[i].startIndex - 1)
|
|
|
- let stringCharEnd = textUTF8.substring(from: rangeItalic[i].endIndex + 1, to: rangeItalic[i].endIndex + 1)
|
|
|
- if (rangeItalic[i].startIndex == 0 || checkCharBefore(char: stringCharStart) || (checkCharRich(char: stringCharStart) && stringCharStart == stringCharEnd)) {
|
|
|
+ if rangeItalic[i].startIndex > lastFirstRange {
|
|
|
+ let charStart: Character = rangeItalic[i].startIndex != 0 ? Array(textUTF8.substring(from: rangeItalic[i].startIndex - 1, to: rangeItalic[i].startIndex - 1))[0] : Array("0")[0]
|
|
|
+ if (rangeItalic[i].startIndex == 0 || (!charStart.isLetter && !charStart.isNumber)) {
|
|
|
lastFirstRange = rangeItalic[i].startIndex
|
|
|
continueCheckingItalic = true
|
|
|
} else {
|
|
@@ -711,7 +712,7 @@ extension String {
|
|
|
let countEmojiBefore = finalText.string.substring(from: 0, to: lastFirstRange - (2*countRemoveItalicSign)).countEmojiCharacter()
|
|
|
let countEmoji = finalText.string.substring(from: lastFirstRange - (2*countRemoveItalicSign), to: rangeItalic[i].endIndex - (2*countRemoveItalicSign)).countEmojiCharacter()
|
|
|
totalEmoji = countEmoji + countEmojiBefore
|
|
|
- finalText.addAttribute(.font, value: UIFont.italicSystemFont(ofSize: 12), range: NSRange(location: lastFirstRange - (2*countRemoveItalicSign) + countEmojiBefore, length: (rangeItalic[i].endIndex + countEmoji + 1) - lastFirstRange))
|
|
|
+ finalText.addAttribute(.font, value: italicFont, range: NSRange(location: lastFirstRange - (2*countRemoveItalicSign) + countEmojiBefore, length: (rangeItalic[i].endIndex + countEmoji + 1) - lastFirstRange))
|
|
|
if !isEditing{
|
|
|
finalText.mutableString.replaceOccurrences(of: "\(italicSign)", with: "", options: .literal, range: NSRange(location: lastFirstRange + countEmojiBefore - (2*countRemoveItalicSign), length: 1))
|
|
|
finalText.mutableString.replaceOccurrences(of: "\(italicSign)", with: "", options: .literal, range: NSRange(location: rangeItalic[i].endIndex + totalEmoji - (2*countRemoveItalicSign) - 1, length: 1))
|
|
@@ -720,7 +721,8 @@ extension String {
|
|
|
finalText.addAttribute(.foregroundColor, value: UIColor.gray, range: NSRange(location: lastFirstRange + countEmojiBefore, length: 1))
|
|
|
finalText.addAttribute(.foregroundColor, value: UIColor.gray, range: NSRange(location: rangeItalic[i].endIndex + totalEmoji, length: 1))
|
|
|
}
|
|
|
- lastFirstRange = -1
|
|
|
+ lastFirstRange = rangeItalic[i].endIndex
|
|
|
+ continueCheckingItalic = false
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -733,10 +735,9 @@ extension String {
|
|
|
var continueCheckingUnderline = false
|
|
|
var totalEmoji = 0
|
|
|
for i in 0..<rangeUnderline.count {
|
|
|
- if lastFirstRange == -1 {
|
|
|
- let stringCharStart = textUTF8.substring(from: rangeUnderline[i].startIndex - 1, to: rangeUnderline[i].startIndex - 1)
|
|
|
- let stringCharEnd = textUTF8.substring(from: rangeUnderline[i].endIndex + 1, to: rangeUnderline[i].endIndex + 1)
|
|
|
- if (rangeUnderline[i].startIndex == 0 || checkCharBefore(char: stringCharStart) || (checkCharRich(char: stringCharStart) && stringCharStart == stringCharEnd)) {
|
|
|
+ if rangeUnderline[i].startIndex > lastFirstRange {
|
|
|
+ let charStart: Character = rangeUnderline[i].startIndex != 0 ? Array(textUTF8.substring(from: rangeUnderline[i].startIndex - 1, to: rangeUnderline[i].startIndex - 1))[0] : Array("0")[0]
|
|
|
+ if (rangeUnderline[i].startIndex == 0 || (!charStart.isLetter && !charStart.isNumber)) {
|
|
|
lastFirstRange = rangeUnderline[i].startIndex
|
|
|
continueCheckingUnderline = true
|
|
|
} else {
|
|
@@ -764,7 +765,8 @@ extension String {
|
|
|
finalText.addAttribute(.foregroundColor, value: UIColor.gray, range: NSRange(location: lastFirstRange + countEmojiBefore, length: 1))
|
|
|
finalText.addAttribute(.foregroundColor, value: UIColor.gray, range: NSRange(location: rangeUnderline[i].endIndex + totalEmoji, length: 1))
|
|
|
}
|
|
|
- lastFirstRange = -1
|
|
|
+ lastFirstRange = rangeUnderline[i].endIndex
|
|
|
+ continueCheckingUnderline = false
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -777,10 +779,9 @@ extension String {
|
|
|
var continueCheckingStrikethrough = false
|
|
|
var totalEmoji = 0
|
|
|
for i in 0..<rangeStrikethrough.count {
|
|
|
- if lastFirstRange == -1 {
|
|
|
- let stringCharStart = textUTF8.substring(from: rangeStrikethrough[i].startIndex - 1, to: rangeStrikethrough[i].startIndex - 1)
|
|
|
- let stringCharEnd = textUTF8.substring(from: rangeStrikethrough[i].endIndex + 1, to: rangeStrikethrough[i].endIndex + 1)
|
|
|
- if (rangeStrikethrough[i].startIndex == 0 || checkCharBefore(char: stringCharStart) || (checkCharRich(char: stringCharStart) && stringCharStart == stringCharEnd)) {
|
|
|
+ if rangeStrikethrough[i].startIndex > lastFirstRange {
|
|
|
+ let charStart: Character = rangeStrikethrough[i].startIndex != 0 ? Array(textUTF8.substring(from: rangeStrikethrough[i].startIndex - 1, to: rangeStrikethrough[i].startIndex - 1))[0] : Array("0")[0]
|
|
|
+ if (rangeStrikethrough[i].startIndex == 0 || (!charStart.isLetter && !charStart.isNumber)) {
|
|
|
lastFirstRange = rangeStrikethrough[i].startIndex
|
|
|
continueCheckingStrikethrough = true
|
|
|
} else {
|
|
@@ -808,51 +809,31 @@ extension String {
|
|
|
finalText.addAttribute(.foregroundColor, value: UIColor.gray, range: NSRange(location: lastFirstRange + countEmojiBefore, length: 1))
|
|
|
finalText.addAttribute(.foregroundColor, value: UIColor.gray, range: NSRange(location: rangeStrikethrough[i].endIndex + totalEmoji, length: 1))
|
|
|
}
|
|
|
- lastFirstRange = -1
|
|
|
+ lastFirstRange = rangeStrikethrough[i].endIndex
|
|
|
+ continueCheckingStrikethrough = false
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//Check Mention
|
|
|
let finalTextAfterRichText = finalText.string
|
|
|
if finalTextAfterRichText.contains("@") {
|
|
|
- if !group_id.isEmpty {
|
|
|
- let listMembers = Member.getAllMember(group_id: group_id)
|
|
|
- if listMembers.count > 0 {
|
|
|
- for i in 0..<listMembers.count {
|
|
|
- if isEditing {
|
|
|
- if listMentionInTextField.count > 0 {
|
|
|
- let name = (listMembers[i].firstName + " " + listMembers[i].lastName).trimmingCharacters(in: .whitespaces)
|
|
|
- if listMentionInTextField.firstIndex(where: { ($0.firstName + " " + $0.lastName).trimmingCharacters(in: .whitespaces) == name }) != nil {
|
|
|
- let range = NSString(string: finalTextAfterRichText).range(of: "@\(name)", options: .caseInsensitive)
|
|
|
- finalText.addAttribute(.foregroundColor, value: UIColor.mentionColor, range: range)
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- let name = (listMembers[i].firstName + " " + listMembers[i].lastName).trimmingCharacters(in: .whitespaces)
|
|
|
- let range = NSString(string: finalTextAfterRichText).range(of: listMembers[i].pin, options: .caseInsensitive)
|
|
|
- if range.lowerBound != range.upperBound {
|
|
|
- finalText.mutableString.replaceOccurrences(of: listMembers[i].pin, with: name, options: .literal, range: range)
|
|
|
+ let listTextEnter = finalText.string.split(separator: "\n")
|
|
|
+ for j in 0...listTextEnter.count - 1 {
|
|
|
+ let listText = listTextEnter[j].split(separator: " ")
|
|
|
+ let listMention = listText.filter({ $0.starts(with: "@")})
|
|
|
+ if listMention.count > 0 {
|
|
|
+ for i in 0..<listMention.count {
|
|
|
+ let f_pin = (String(listMention[i])).substring(from: 1, to: listMention[i].count)
|
|
|
+ let member = Member.getMember(f_pin: f_pin)
|
|
|
+ if member != nil {
|
|
|
+ let name = (member!.firstName + " " + member!.lastName).trimmingCharacters(in: .whitespaces)
|
|
|
+ finalText.mutableString.replaceOccurrences(of: f_pin, with: name, options: .literal, range: NSString(string: finalText.string).range(of: f_pin))
|
|
|
+ if !group_id.isEmpty && Member.getMemberInGroup(f_pin: f_pin, group_id: group_id) != nil {
|
|
|
finalText.addAttribute(.foregroundColor, value: UIColor.mentionColor, range: NSString(string: finalText.string).range(of: "@\(name)"))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- } else {
|
|
|
- let listTextEnter = finalText.string.split(separator: "\n")
|
|
|
- for j in 0...listTextEnter.count - 1 {
|
|
|
- let listText = listTextEnter[j].split(separator: " ")
|
|
|
- let listMention = listText.filter({ $0.starts(with: "@")})
|
|
|
- if listMention.count > 0 {
|
|
|
- for i in 0..<listMention.count {
|
|
|
- let f_pin = (String(listMention[i])).substring(from: 1, to: listMention[i].count)
|
|
|
- let member = Member.getMember(f_pin: f_pin)
|
|
|
- if member != nil {
|
|
|
- let name = (member!.firstName + " " + member!.lastName).trimmingCharacters(in: .whitespaces)
|
|
|
- finalText.mutableString.replaceOccurrences(of: f_pin, with: name, options: .literal, range: NSString(string: finalText.string).range(of: f_pin))
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
if isSearching {
|