这是我来自后端的 JSON 字符串文本
"Body" : "16.03.2024\r\n\r\nDear Parents (KG)\r\n\r\nKindly adhere to the summer timings from *Monday (18\/03\/2024) to Friday(29\/03\/2024)\r\n\r\n **School Timings:* \r\n **Monday - Friday* \r\n *08:25 am - 11:30 am*** \r\n\r\n *Note:* Students should be present in the school campus before *8:25 am* .\r\n\r\n* From *01\/04\/2024(Monday),* the school school timings will be *08:25 am to 01:00 pm.* \r\n\r\n* Students will adhere to summer uniform from *April 1, 2024 (Monday).* \r\n\r\nRegards\r\n\r\nSr Anandi Pinto\r\nPrincipal"
o/p 中缺少一些粗体文本。如何解决这个问题?请指导我。
代码: 此代码缺少一些粗体字母。比如学校时间
l and T
缺失08:25 am here a missing
和周五F
缺失。像这样...如何解决这个问题?
lblMessage?.attributedText = checkingTheBoldItalicAndStrickLineForBoadyMessage(objText: /modal.body)
func checkingTheBoldItalicAndStrickLineForBoadyMessage(objText:String) -> NSMutableAttributedString {
var objFinalString = String()
let fullNameArr = objText.components(separatedBy: " ")
let finalString = NSMutableAttributedString()
var objGetString = objText
if objGetString.first == " " && objGetString.last == "_" && objGetString.count > 2 {
objGetString.removeFirst()
objGetString.removeLast()
objGetString.append(" ")
let attributes:[NSAttributedString.Key : Any] = [
.font : UIFont(name: "Ubuntu-Bold", size: 16)!
]
finalString.append(NSAttributedString(string: objGetString, attributes:attributes))
objFinalString.append(objGetString)
} else if objGetString.first == "/" && objGetString.last == "/" && objGetString.count > 2 {
objGetString.removeFirst()
objGetString.removeLast()
objGetString.append(" ")
let attributes:[NSAttributedString.Key : Any] = [
.font : UIFont(name: "Ubuntu-Italic", size: 14)!
]
finalString.append(NSAttributedString(string: objGetString, attributes:attributes))
objFinalString.append(objGetString)
}
else {
for objStr in fullNameArr {
var objString = objStr
if (objString.first == "*" || objString.last == "*") && objString.count > 2 {
objString.removeFirst()
objString.removeLast()
objString.append(" ")
let attributes:[NSAttributedString.Key : Any] = [
.font : UIFont(name: "Ubuntu-Bold", size: 16)!
]
finalString.append(NSAttributedString(string: objString, attributes:attributes))
objFinalString.append(objString)
finalString.mutableString.replaceOccurrences(of: "*", with: "*", options: NSString.CompareOptions.caseInsensitive, range: NSRange(location: 0, length: finalString.length))
objFinalString.replacingOccurrences(of: "*", with: "*")
}
else if (objString.first == "_" || objString.last == "_") && objString.count > 2 {
objString.removeFirst()
objString.removeLast()
objString.append(" ")
let attributes:[NSAttributedString.Key : Any] = [
.font : UIFont(name: "Ubuntu-Italic", size: 14)!
]
finalString.append(NSAttributedString(string: objString, attributes:attributes))
objFinalString.append(objString)
finalString.mutableString.replaceOccurrences(of: "_", with: "", options: NSString.CompareOptions.caseInsensitive, range: NSRange(location: 0, length: finalString.length))
objFinalString.replacingOccurrences(of: "_", with: "")
}
else {
objString.append(" ")
finalString.append(NSMutableAttributedString(string:objString))
objFinalString.append(objString)
}
}
return finalString
}
仅当
objString.removeFirst()
时才必须使用
objString.first == "*"
同样
objGetString.removeLast()
当 objString.last == "*"
只要满足这些条件,你就可以开始了......
if (objString.first == "*" || objString.last == "*") && objString.count > 2 {
if (objString.first == "*") {
objString.removeFirst()
}
if (objString.last == "*") {
objString.removeLast()
}
objString.append(" ")
针对所有情况更新此内容