Extra whitespace that causes a newline by UITextView at end is ignored
Asked Answered
A

2

-1

This is my code:

import UIKit

class ViewController: UIViewController {
    init() {
        super.init(nibName: nil, bundle: nil)

        let textView = UITextView(frame: .zero)
        textView.isScrollEnabled = false
        textView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(textView)

        NSLayoutConstraint.activate([
           textView.widthAnchor.constraint(equalToConstant: 150),
            textView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            textView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])

        // As we can see, LOT'S of whitespace at the end.
        textView.text = "Some Random Text That Has Whitespaces At The End                                                        "

        view.backgroundColor = .yellow
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError()
    }
}

This is the result:

enter image description here

By the amount of spaces, it should have created an empty newline. However, as we can see this wasn't the case. If I add another character at the very end of the string, the newline will be shown (but with the character, which I do not want).

How can I show an empty newline if needed in an UITextView?

Across answered 24/5, 2019 at 20:1 Comment(9)
How about "\n"?Parisian
Note that you have the same behavior when writing a message on Message.appCharolettecharon
@LinusGeffarth That always creates a newline. The UITextView should only append a newline if it is neededAcross
@Charolettecharon yes, that is accomplished by removing the leading & trailing whitespaces. I must keep them.Across
Wait, can you please clear up what you're trying to do? What do you mean by if needed?Parisian
I'm wondering if using another "invisible" char at the end would do the trick.Charolettecharon
@LinusGeffarth I have a string with lots of whitespaces at the end. The textview just ignores it. I don't want that.Across
Hm, I'm not sure if the text field ignores it. The space after "end" seems to be enough to fit the textview. Try adding more spaces to see if it works.Rositaroskes
@Rositaroskes not enough??? The amount of whitespaces is equal to the existing text. But it does not matter how many whitespaces there are.Across
N
1

Use this:

‏‏‎ ‏‏‎

There is some invisible characters inside the quote that iOS is not count them as whiteSpace

Natashianatassia answered 24/5, 2019 at 20:48 Comment(4)
I've used this and it worked for me in 90% of the cases, I have a few where the last line is nonetheless cut off. Do you have any other solution? In my case I'm using a UILabelDelafuente
@Delafuente you found a solution?Across
Nope. Did you @J.Doe?Delafuente
@Delafuente I have an extremely hacky solution by using NSMutableAttributedString. Long story short: append something like ' |' behind your text, wrap in inside an NSMutableAttributedString and change the color of the last character (which is |) to UIColor.clear. Than it always works in every scenarioAcross
B
0

to add extra lines use "\n" for example:

textView.text = "Some Random Text That Has Whitespaces At The End \n\n"

Result:

https://i.sstatic.net/QkCmc.jpg

Breathing answered 24/5, 2019 at 22:8 Comment(3)
This does not answer the questionAcross
@J.Doe Post Question: how can I show an empty newline if needed in an UITextView? Answer: to add extra lines use "\n" Did I get it wrong?Breathing
No. You point out exactly the problem with your answer: you always show a newline. You literally quoted the words 'if needed'.Across

© 2022 - 2024 — McMap. All rights reserved.