Home > OS >  Swiftui UITextView new line margin
Swiftui UITextView new line margin

Time:10-07

I have a UITextView which I use as a text editor. However, I would like each new line to have a margin bottom.

Test text 1. Paragraph 1

Second paragraph. Test text 2

I have seen that it is not possible to do this from a paragraphStyle to since changing the line height

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 5

but not setting the margin for each new line.

I tried to set paragraphSpacing but the various paragraphs have the right margin at the bottom but the height of the last line of the paragraph is different from the others and shows a spaced cursor.

paragraphStyle.paragraphSpacing = 20

I tried with a regex to translate each \n to \n\n

func textViewDidChange(_ textView: UITextView) {
let newPattern = "(?<!\n)\n(?!\n)"
let regexOptions: NSRegularExpression.Options = [.anchorsMatchLines, .dotMatchesLineSeparators,]

but here too I got inaccurate results

Is it possible to set a margin bottom for each new line in a text editor?

Any ideas on how to proceed?

CodePudding user response:

Based on this answer: enter image description here

enter image description here

  • Related