7

I have a UILabel and a UITextView, where my intention is for them both to display the same text, and for the appearance to be the same. I'm having some issue with that right now. I've set up a demonstration in a Swift Playground:

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        let label = UILabel()
        label.frame = CGRect(x: 100, y: 100, width: 247, height: 39)
        label.numberOfLines = 0
        label.font = UIFont.systemFont(ofSize: 16)
        label.text = "I’m on my way back to London today"
        label.textColor = .black
        label.backgroundColor = .red
        label.lineBreakMode = .byWordWrapping
        view.addSubview(label)

        let textView = UITextView()
        textView.frame = CGRect(x: 100, y: 200, width: 247, height: 39)
        textView.font = label.font
        textView.text = label.text
        textView.textColor = label.textColor
        textView.backgroundColor = label.backgroundColor
        textView.textContainer.lineBreakMode = label.lineBreakMode
        textView.textContainerInset = UIEdgeInsets.zero
        textView.textContainer.lineFragmentPadding = 0
        view.addSubview(textView)

        self.view = view
    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

enter image description here

As you can see, they're both setup in the same way, but the linebreak occurs in a different place on the UITextView. I've set the lineBreakMode to the same value for both. I've also removed the textContainerInsets and lineFragmentPadding on the UITextView, so the text is positioned with the same padding within both views.

Andrew
  • 7,693
  • 11
  • 43
  • 81

1 Answers1

9

It was a change to UILabel in IOS 11 to fix orphaned words. No way to shut it off although I wish there was. The only way to do it would be to use a non editable/nonscrollable textview or a CATextLayer. To get the sizing attributes of a UILabel I am afraid you have to do that manually.

See this as the problem was similar. Although not in that answer I did find that UITextView wraps the old way. I am using a UITextView now and I manually adjust the font size in textDidChange. I hold the original font so I can always resize.

Andriy
  • 2,767
  • 2
  • 21
  • 29
agibson007
  • 4,173
  • 2
  • 19
  • 24
  • Wow, great answer. Could you perhaps elaborate a bit about this orphaned words issue? – meaning-matters Dec 30 '17 at 12:34
  • 2
    I am not a type/font guy but in design it is seen as undesirable to have a single word on a line by itself. So Apple change iOS UILabel to pull down extra words to make it more even. The problem I have with this is that none of the string sizing, core text methods, or anything else can help solve how it works. An example of what is probably going on is something like https://github.com/StockX/EvenlyWrappedLabel. I just wish there was a way to disable it or for me that the core text methods would at least be able to tell me what text was shifted. Good luck. – agibson007 Dec 30 '17 at 14:04
  • 2
    Thanks for the good explanation. I think this is a bad choice from Apple! – meaning-matters Dec 30 '17 at 14:33
  • Indeed a bad choice without even giving developers an option to disable it. – Sasanka Panguluri Mar 28 '20 at 04:24