Search code examples
jsonswiftstringlabelbold

From Json string text unable to show bold text properly(few letters are missing) in swift


This is my JSON string text from backend

"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"

some bold text is missing in o/p. how to fix this? please guide me.

enter image description here

code: with this code some bold word letters are missing. like School Timings l and T missing 08:25 am here a missing and Friday F missing. something like this... how to fix this?

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
}

Solution

  • You have to use objString.removeFirst() only when objString.first == "*"

    Similarly objGetString.removeLast() when objString.last == "*"

    Just put those conditions and you will be good to go...

    if (objString.first == "*" || objString.last == "*") && objString.count > 2 {
        if (objString.first == "*") {
            objString.removeFirst()
        }
    
        if (objString.last == "*") {
            objString.removeLast()
        }
        objString.append(" ")
    

    Update this for all cases


    Suggestion

    I suggest to create function which will accept text & option text as * OR _ OR / to make necessary action

    func updateText(mainString : String, optionString : String) -> String {
        if (mainString.first == optionString || mainString.last == optionString) && mainString.count > 2 {
            mainString.removeFirst()
            mainString.removeLast()
            mainString.append(" ")
    
            // add case here for font
            var fontName : String = ""
            switch optionString {
                case "*" :
                   fontName = "Ubuntu-Bold"
                case "_" :
                   fontName = "Ubuntu-Italic"
            }
            let attributes:[NSAttributedString.Key : Any] = [
                .font :  UIFont(name: fontName, size: 16)!
            ]
            finalString.append(NSAttributedString(string: objString, attributes:attributes))
            objFinalString.append(objString)
    
            finalString.mutableString.replaceOccurrences(of: optionString, with: optionString, options: NSString.CompareOptions.caseInsensitive, range: NSRange(location: 0, length: finalString.length))
            objFinalString.replacingOccurrences(of: optionString, with: optionString)
         }
    }
    

    This is not exact code, but will give you idea.