1

I am using UILabel to display text. Sometimes, it creates orphan and widow problem in the copy of text, the alignment of the text is set to 'justified', also the hyphenation factor is set to 1.0.

How can I solve the widow & orphan problem?

I want justified text with hyphens for word-wrap and want to remove orphan and widow.

I had set attributed text to label and the size of label is set depending on the device width.

image

Here is the code for attributed text:

let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = NSTextAlignment.Justified
    paragraphStyle.hyphenationFactor = 1.0;

let strTwo = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";

let attrlblTwo = NSAttributedString(string: strTwo,
                                             attributes: [
                                                NSParagraphStyleAttributeName: paragraphStyle,
                                                NSBaselineOffsetAttributeName: NSNumber(float: 0),NSKernAttributeName: 0, NSFontAttributeName : lblTwo.font
        ])
    self.lblTwo.attributedText = attrlblTwo
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
DB_2016
  • 11
  • 2
  • This has probably changed in iOS 11 so it is done without you asking for it. My problem is that I NEED orphans since I am not justifying and it just looks stupid to have two short lines. (See https://stackoverflow.com/questions/46200027/uilabel-wrong-word-wrap-in-ios-11) – David Dunham Dec 13 '17 at 17:01

2 Answers2

2

To get rid of orphans, replace the last space in the text with a non-breaking space. The non-breaking space character is U+00A0. You can find it by selecting "Emoji & Symbols" from the edit menu, click on the widget in the upper right to expand the Character Viewer to full size and then type space in the search field. You'll see a few "missing" characters, these are the space characters. Click on them until you find the "No-Break Space", then double click it to insert it into your code. Character Viewer with Non Breaking Space highlighted

Cortis Clark
  • 169
  • 1
  • 5
0

Have you ever tried doing this using storyboards? I've experienced this from a project before, having a UILabel on storyboard, setting Text to Atrributed and setting hyphenation to 1 works for me. Did test on different paragraph lengths and iOS devices.

Lysdexia
  • 453
  • 8
  • 22
  • yes i had already tried this with storyboard before. and the same thing i am doing programmatically. – DB_2016 Mar 17 '17 at 10:17
  • @DB_2016 have you tried this post? http://stackoverflow.com/questions/33228302/how-can-i-prevent-orphans-in-a-label-in-swift – Lysdexia Mar 22 '17 at 03:20