NSMutableParagraphStyle Linespacing Used in Bhagavad Gita – Swift 5

In my first app ‘Bhagavad Gita‘ I encountered this issue wherein I had to display the text nicely spaced out in a TableView (In the Settings page of the app). In one of the tutorials, I came across – NSMutableParagraphStyle.

NSMutableParagraphStyle has a property called linespacing that lets you nicely space out your text! Isn’t that pretty neat ?πŸ€“

The implementation is :

let attr = NSMutableAttributedString(string: contents)

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 5
attr.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attr.length))
let attr = NSMutableAttributedString(string: contents)
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineSpacing = 5
    attr.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attr.length))

contents is the string that you want to be shown spaced out.

In the below image,

  • the first cell contains text with their default tableview cell formatting.
  • the second cell has linespacing property used. Look at how nicely spaced its contents are:
iPhone example🀸🏽

A simple example showing the usage is provided in the GitHub project here : https://github.com/Kavisha-Dev/TTT_LineSpacing

References from a post : https://jayeshkawli.ghost.io/ios-add-spacing-to-line-of-text-swift/


#theteenycode – is a quick short snippets of code that I have found useful at work. Find me on twitter

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: