Label line break not working properly in xcode
Asked Answered
B

2

0

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

Bader answered 18/6, 2020 at 6:9 Comment(7)
Please show, what you tried...just showing images will not helpful to understand your issue.Gastineau
please share your codeHip
I just added lines count to 0 and line break to word wrap.Bader
We need a minimal reproducible example, something that we can copy and paste into a playground, and reproduce your problem.Viens
when I remove the space between in and gender it completely fills in the label but when i gave the space it is not working. What I need is that the word gender must be in the top line and equality in the bottom line.Bader
Try label!.sizeToFit()Mannes
See #46200527Radiolarian
H
1

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.

Horton answered 20/6, 2020 at 11:18 Comment(0)
R
1

Use following code:

label.lineBreakStrategy = []
Retread answered 31/3, 2022 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.