0

I have a string that require to set attributes on it. The attributes(bold, italic, strikethrough, underline) are getting added separately but they are not keeping intact with the string. I want to know how to keep different attributes intact with a string and then remove them one by one as required.

Bold - 

   if textBold == true {
                            let string = text
                            let attributes: [NSAttributedString.Key: Any] = [
                                .font: UIFont.boldSystemFont(ofSize: 25)
                            ]
                            let attributedString = NSAttributedString(string: string, attributes: attributes)
                            modifiedString = attributedString
                            text_View.attributedText = modifiedString

                            text_View.sizeToFit()
                            databaseHandlerObj.editLabelData(textProjectId: fetchTextProjectId, text_Id: fetchTextId, text: modifiedString.string)
                        }

Italic - 
        if textItalic == true {

                            text_View.sizeToFit()
                            let string = text
                            
                            let attributes: [NSAttributedString.Key: Any] = [
                                .font: UIFont.italicSystemFont(ofSize: CGFloat(fontSize))
                            ]
                            let attributedString = NSAttributedString(string: string, attributes: attributes)
                            self.modifiedString = attributedString
                            text_View.attributedText = modifiedString
                            
                            databaseHandlerObj.editLabelData(textProjectId: fetchTextProjectId, text_Id: fetchTextId, text: modifiedString.string)
                        }

Underline -
   if textUnderline == true {
                            text_View.sizeToFit()
                            let string = text
                            let attributedString = NSMutableAttributedString.init(string: string)
                            attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range:
                                                            NSRange.init(location: 0, length: attributedString.length))
                            self.modifiedString = attributedString
                            text_View.attributedText = modifiedString
                            
                            databaseHandlerObj.editLabelData(textProjectId: fetchTextProjectId, text_Id: fetchTextId, text: modifiedString.string)
                        }
                        

Strikethrough - 
      if textStrikethrough == true {
                            text_View.sizeToFit()
                            let string = text
                            let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: string)
                            attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
                            self.modifiedString = attributeString
                            text_View.attributedText = modifiedString
                            databaseHandlerObj.editLabelData(textProjectId: fetchTextProjectId, text_Id: fetchTextId, text: modifiedString.string)
                        }
iosDevSan
  • 65
  • 1
  • 13

1 Answers1

1

Add condition on attributes dictionary.

let string = text
var attributes: [NSAttributedString.Key: Any] = [:]

if bold {
    attributes[.font] = UIFont.boldSystemFont(ofSize: 25)
}
if italic {
    attributes[.font] = UIFont.italicSystemFont(ofSize: CGFloat(fontSize))
}
if underline {
    attributes[.underlineStyle] = 1
}

let attributedString = NSAttributedString(string: string, attributes: attributes)
// Other Code

Raja Kishan
  • 16,767
  • 2
  • 26
  • 52
  • Wow! Thanks a lot. It worked. I was stuck on this from days. According to the code it is either keeping bold or italic style to text. The only question I have now is, how do I keep bold and italic style on text at the same time. – iosDevSan Dec 18 '21 at 08:19
  • For Bold & Italic, you need to use a font that is bold AND italic. See https://stackoverflow.com/questions/38533323/ios-swift-making-font-toggle-bold-italic-bolditalic-normal-without-change-oth – Larme Dec 18 '21 at 10:28
  • Not sure why OP accepted this answer. It doesn’t even come close to answer the question – Leo Dabus Dec 18 '21 at 15:56
  • @leo what’s wrong with my answer?? – Raja Kishan Dec 19 '21 at 05:04
  • 1
    If the font is italic it will simply overwrite the bold font – Leo Dabus Dec 19 '21 at 13:19