Sci/Tech

Replace Attributed Strings With Rich Text Format (RTF) Documents In Swift 5

Here’s an example directly from my EchoTools app – a video on normal pleural sliding. The description (red box) required the following attributed string code:

// Create the attributed string
let mv3dglass = NSMutableAttributedString(string:"Finding: Pleural Sliding\nProbe: linear\n\nPleural sliding is a normal, respirophasic movement seen as the visceral and parietal pleura move against each other during the respiratory cycle. The presence of pleural sliding excludes pneumothorax at the site of examination. Therefore, one should carefully examine multiple interspaces in both lung fields.")

// Declare the fonts
let mv3dglassFont1 = UIFont(name:"HelveticaNeue-Bold", size:14.0)!
let mv3dglassFont2 = UIFont(name:"HelveticaNeue", size:14.0)!

// Declare the colors
let mv3dglassColor1 = UIColor(red: 1.000000, green: 0.999981, blue: 0.999802, alpha: 0.000000)
let mv3dglassColor2 = UIColor(red: 0.000000, green: 0.000000, blue: 0.000000, alpha: 1.000000)

// Declare the paragraph styles
let mv3dglassParaStyle1 = NSMutableParagraphStyle()
mv3dglassParaStyle1.tabStops = [NSTextTab(textAlignment: NSTextAlignment.left, location: 28.000000, options: [:]), NSTextTab(textAlignment: NSTextAlignment.left, location: 56.000000, options: [:]), NSTextTab(textAlignment: NSTextAlignment.left, location: 84.000000, options: [:]), NSTextTab(textAlignment: NSTextAlignment.left, location: 112.000000, options: [:]), NSTextTab(textAlignment: NSTextAlignment.left, location: 140.000000, options: [:]), NSTextTab(textAlignment: NSTextAlignment.left, location: 168.000000, options: [:]), NSTextTab(textAlignment: NSTextAlignment.left, location: 196.000000, options: [:]), NSTextTab(textAlignment: NSTextAlignment.left, location: 224.000000, options: [:]), NSTextTab(textAlignment: NSTextAlignment.left, location: 252.000000, options: [:]), NSTextTab(textAlignment: NSTextAlignment.left, location: 280.000000, options: [:]), NSTextTab(textAlignment: NSTextAlignment.left, location: 308.000000, options: [:]), NSTextTab(textAlignment: NSTextAlignment.left, location: 336.000000, options: [:]), ]

// Create the attributes and add them to the string
mv3dglass.addAttribute(NSAttributedString.Key.paragraphStyle, value:mv3dglassParaStyle1, range:NSRange(location: 0, length: 8))
mv3dglass.addAttribute(NSAttributedString.Key.backgroundColor, value:mv3dglassColor1, range:NSRange(location: 0, length: 8))
mv3dglass.addAttribute(NSAttributedString.Key.font, value:mv3dglassFont1, range:NSRange(location: 0, length: 8))
mv3dglass.addAttribute(NSAttributedString.Key.foregroundColor, value:mv3dglassColor2, range:NSRange(location: 0, length: 8))
mv3dglass.addAttribute(NSAttributedString.Key.paragraphStyle, value:mv3dglassParaStyle1, range:NSRange(location: 8, length: 17))
mv3dglass.addAttribute(NSAttributedString.Key.backgroundColor, value:mv3dglassColor1, range:NSRange(location: 8, length: 17))
mv3dglass.addAttribute(NSAttributedString.Key.font, value:mv3dglassFont2, range:NSRange(location: 8, length: 17))
mv3dglass.addAttribute(NSAttributedString.Key.foregroundColor, value:mv3dglassColor2, range:NSRange(location: 8, length: 17))
mv3dglass.addAttribute(NSAttributedString.Key.paragraphStyle, value:mv3dglassParaStyle1, range:NSRange(location: 25, length: 6))
mv3dglass.addAttribute(NSAttributedString.Key.backgroundColor, value:mv3dglassColor1, range:NSRange(location: 25, length: 6))
mv3dglass.addAttribute(NSAttributedString.Key.font, value:mv3dglassFont1, range:NSRange(location: 25, length: 6))
mv3dglass.addAttribute(NSAttributedString.Key.foregroundColor, value:mv3dglassColor2, range:NSRange(location: 25, length: 6))
mv3dglass.addAttribute(NSAttributedString.Key.paragraphStyle, value:mv3dglassParaStyle1, range:NSRange(location: 31, length: 9))
mv3dglass.addAttribute(NSAttributedString.Key.backgroundColor, value:mv3dglassColor1, range:NSRange(location: 31, length: 9))
mv3dglass.addAttribute(NSAttributedString.Key.font, value:mv3dglassFont2, range:NSRange(location: 31, length: 9))
mv3dglass.addAttribute(NSAttributedString.Key.foregroundColor, value:mv3dglassColor2, range:NSRange(location: 31, length: 9))
mv3dglass.addAttribute(NSAttributedString.Key.paragraphStyle, value:mv3dglassParaStyle1, range:NSRange(location: 40, length: 15))
mv3dglass.addAttribute(NSAttributedString.Key.backgroundColor, value:mv3dglassColor1, range:NSRange(location: 40, length: 15))
mv3dglass.addAttribute(NSAttributedString.Key.font, value:mv3dglassFont1, range:NSRange(location: 40, length: 15))
mv3dglass.addAttribute(NSAttributedString.Key.foregroundColor, value:mv3dglassColor2, range:NSRange(location: 40, length: 15))
mv3dglass.addAttribute(NSAttributedString.Key.paragraphStyle, value:mv3dglassParaStyle1, range:NSRange(location: 55, length: 6))
mv3dglass.addAttribute(NSAttributedString.Key.backgroundColor, value:mv3dglassColor1, range:NSRange(location: 55, length: 6))
mv3dglass.addAttribute(NSAttributedString.Key.font, value:mv3dglassFont2, range:NSRange(location: 55, length: 6))
mv3dglass.addAttribute(NSAttributedString.Key.foregroundColor, value:mv3dglassColor2, range:NSRange(location: 55, length: 6))
mv3dglass.addAttribute(NSAttributedString.Key.paragraphStyle, value:mv3dglassParaStyle1, range:NSRange(location: 61, length: 6))
mv3dglass.addAttribute(NSAttributedString.Key.backgroundColor, value:mv3dglassColor1, range:NSRange(location: 61, length: 6))
mv3dglass.addAttribute(NSAttributedString.Key.font, value:mv3dglassFont1, range:NSRange(location: 61, length: 6))
mv3dglass.addAttribute(NSAttributedString.Key.foregroundColor, value:mv3dglassColor2, range:NSRange(location: 61, length: 6))
mv3dglass.addAttribute(NSAttributedString.Key.paragraphStyle, value:mv3dglassParaStyle1, range:NSRange(location: 67, length: 152))
mv3dglass.addAttribute(NSAttributedString.Key.backgroundColor, value:mv3dglassColor1, range:NSRange(location: 67, length: 152))
mv3dglass.addAttribute(NSAttributedString.Key.font, value:mv3dglassFont2, range:NSRange(location: 67, length: 152))
mv3dglass.addAttribute(NSAttributedString.Key.foregroundColor, value:mv3dglassColor2, range:NSRange(location: 67, length: 152))
mv3dglass.addAttribute(NSAttributedString.Key.paragraphStyle, value:mv3dglassParaStyle1, range:NSRange(location: 219, length: 21))
mv3dglass.addAttribute(NSAttributedString.Key.backgroundColor, value:mv3dglassColor1, range:NSRange(location: 219, length: 21))
mv3dglass.addAttribute(NSAttributedString.Key.font, value:mv3dglassFont1, range:NSRange(location: 219, length: 21))
mv3dglass.addAttribute(NSAttributedString.Key.foregroundColor, value:mv3dglassColor2, range:NSRange(location: 219, length: 21))
mv3dglass.addAttribute(NSAttributedString.Key.paragraphStyle, value:mv3dglassParaStyle1, range:NSRange(location: 240, length: 110))
mv3dglass.addAttribute(NSAttributedString.Key.backgroundColor, value:mv3dglassColor1, range:NSRange(location: 240, length: 110))
mv3dglass.addAttribute(NSAttributedString.Key.font, value:mv3dglassFont2, range:NSRange(location: 240, length: 110))
mv3dglass.addAttribute(NSAttributedString.Key.foregroundColor, value:mv3dglassColor2, range:NSRange(location: 240, length: 110))

Although this was fairly easy to create using an app called Attributed String Creator Pro, it was such an eye sore and very cumbersome to modify.

With version 3.1 of EchoTools, I’m transitioning to rich text format (RTF) documents containing the text and formatting I desire. I created an RTF (called “pocus-lung-pleural-sliding.rtf”) with the same text and formatting above. This is the one line of code I used to parse the RTF and replace the aforementioned attributed string chaos.

try! NSMutableAttributedString(url: Bundle.main.url(forResource: "pocus-lung-pleural-sliding", withExtension: "rtf")!, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.rtf], documentAttributes: nil)

The biggest benefit is I can use familiar word processors to modify the text and formatting whenever i see fit. It’s a much faster way for me to add content. Hopefully this helps a curious Internet-goer! 🙂

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button