0

I have a (dynamically sized) UILabel with numberOfLines = 2.

Depending on the text coming from BE, the text is wrapped nicely or not.

Does anyone have any tips on how to prevent (long) words to be separated in 2 lines and having only one character in the new line?

Current behaviour:

Thisisalongwor
d

Wanted behaviour:

Thisisalong
word

Basically: I'd like to set the minimum number of characters per line (when wrapping items from the first line to the second).

Thanks!

Ockie
  • 259
  • 1
  • 12

1 Answers1

1

Here is one approach...

Use CoreText functions to get an array of the wrapped-lines from the label. If the last line has at least 1 character, but fewer than 4 characters, and the full text is greater than 4 characters, insert a line-feed 4-characters from the end of the text and update the label.

So, based on a default UILabel - 17-pt System font - with a fixed-width of 123-pts and wrapping set to Character Wrap, it looks like this:

enter image description here

After running the fixLabelWrap(...) function it looks like this:

enter image description here

Sample code:

class CharWrapViewController: UIViewController {

    @IBOutlet var theLabel: UILabel!

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        fixLabelWrap(theLabel)
    }

    func fixLabelWrap(_ label: UILabel) -> Void {

        // get the text from the label
        var text = theLabel.text ?? ""

        // get an array of the char-wrapped text
        let lines = getLinesArrayOfString(in: theLabel)

        // if it is more than one line
        if lines.count > 1 {
            // get the last line
            if let lastLine = lines.last {
                // if the last line has at least 1 char, is less than 4 chars, and
                // the full text is greater than 4 chars
                if lastLine.count > 0 && lastLine.count < 4 && text.count > 4 {
                    // insert a line-feed 4 chars before the end
                    text.insert("\n", at: text.index(text.endIndex, offsetBy: -4))
                    // update the text in the label
                    theLabel.text = text
                }
            }
        }

    }

    func getLinesArrayOfString(in label: UILabel) -> [String] {

        /// An empty string's array
        var linesArray = [String]()

        guard let text = label.text, let attStr = label.attributedText else { return linesArray }

        let rect = label.frame

        let frameSetter: CTFramesetter = CTFramesetterCreateWithAttributedString(attStr as CFAttributedString)
        let path: CGMutablePath = CGMutablePath()
        path.addRect(CGRect(x: 0, y: 0, width: rect.size.width, height: 100000), transform: .identity)

        let frame: CTFrame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, nil)
        guard let lines = CTFrameGetLines(frame) as? [Any] else {return linesArray}

        for line in lines {
            let lineRef = line as! CTLine
            let lineRange: CFRange = CTLineGetStringRange(lineRef)
            let range = NSRange(location: lineRange.location, length: lineRange.length)
            let lineString: String = (text as NSString).substring(with: range)
            linesArray.append(lineString)
        }

        return linesArray
    }

}

Note: the getLinesArrayOfString(...) function is a slightly modified version of the post found here: https://stackoverflow.com/a/14413484/6257435

DonMag
  • 44,662
  • 5
  • 32
  • 56