Search code examples
iosobjective-cswiftxcodeuilabel

Label line break not working properly in xcode


I have a label which shows the pink area on the screen.enter image description here However, the word "gender " is not shown in the top line even if it has enough space. Why is it not showing up in the first line itself? What I need
enter image description here

label.text = @"Do you believe in gender equality";

Label enter image description here


Solution

  • The UILabel is working as intended by preventing an orphaned word so it’s more readable. This was introduced in iOS 11. Apple must disable it for iMessage because they probably intend this behaviour for long articles of text, not text messages.

    I've seen fixes including

    • call sizeToFit on the label after the text has been set
    • setting UserDefaults.standard.set(false, forKey: "NSAllowsDefaultLineBreakStrategy") // Using this private tricky shortcut may leads appstore rejection.

    But none of these are working on iOS 13, simulator (Not tested on devices).


    Fix

    One tricky solution is - append some spaces or two tabs("\t\t") to the text and set programatically.