Add spaces to Text View

1.5k views Asked by At

I have an app that has a text field where I enter text though code. Here's an example:

var text = ["This is the text that appears in the Text View."]

I want to know how I can add paragraph spaces in the Text View through the code. Such as the text view says this:

Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

But, I want to add a paragraph space like this:

Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Could you help me with this?

iOS app using Swift 3 and Xcode 8

1

There are 1 answers

0
Anusha Kottiyal On

If you are using static text in the app, you can simply use the newline characters along with the text "\n". If it is dynamic / entered by user, then also you can check for new line character in textView delegate method and can add addition new line characters as you want.

Also, as in the comment above you can use NSParagraphStyle and can set paragraphSpacing (The space after the end of the paragraph) and paragraphSpacingBefore (The distance between the paragraph’s top and the beginning of its text content) properties to get desired spacing.

let paraStyle = NSMutableParagraphStyle()
paraStyle.paragraphSpacing = 20

let attributes = [NSParagraphStyleAttributeName: paraStyle]
yourTextView.attributedText = NSAttributedString(string: yourText, attributes:attributes)