UILabel text truncation vs. line breaks in text
Asked Answered
S

2

1

I have a UILabel that is put as titleView in the navigation bar. I want it to have 2 lines, where the first line can be truncated and the second is center aligned.

In code it looks more less like this:

    let label = UILabel()
    let text = NSAttributedString(string: "Long long long text\nsecond line")
    label.attributedText = text
    label.textAlignment = .Center
    label.numberOfLines = 0
    label.lineBreakMode = .ByTruncatingTail
    label.sizeToFit()

    self.navigationItem.titleView = label

The effect in case of the first line text is not exceeding available space is like this:

enter image description here

It's pretty good, but when the first line text is longer than:

let text = NSAttributedString(string: "Very very very very very long text\nsecond line")

enter image description here

I want to achieve like below.

enter image description here

How it can be done? I experimented with numberOfLines and lineBreakMode but it's not worked.

Spillway answered 24/5, 2016 at 9:18 Comment(3)
Why do not you use UIView with two UILabels inside?Epiphyte
Using two labels make a lot of problems in positioning them, believe me. Two labels was my first shot but it's very problematic so I decided to try with one label instead.Spillway
lineBreakMode is the property applied for the whole content, not applicable for every single line. So you must have to go with two UILabels. What are all the issues you've faced in two labels?Epiphyte
C
1

change your line breakmode to ByTruncatingMiddle instead of ByTruncatingTail. Something like below,

    label.lineBreakMode = .ByTruncatingMiddle

Hope this will help :)

Climber answered 24/5, 2016 at 9:28 Comment(7)
No, it doesn't work too. Effect is the same as for ByTruncatingTail.Spillway
try to comment label.sizeToFit() with ByTruncatingMiddle linebreakmodeClimber
commenting out sizeToFit causes that the label is no longer visible because its frame is not setSpillway
try to set frame of label to self.navigationItem.titleView.frame and then comment sizetofitClimber
you can use minimumFontSize to resize font to adjust in label if you have not problem if font size is reduce little bit.Climber
I can't set the frame of titleView manually because I don't know what is the available space between back button and rightBarButtonItem.Spillway
using minimumFontSize is not solution for me, I want it to be fixed sizeSpillway
L
1

Navigation Tittle with sub-Tittle (Multiline Navigation Tittle)

Use NSMutableAttributedString with UITextView instead of UILabel

(because, if tittle is large then UILabel lineBreakMode with .byTruncatingTail is not working for first line in UILabel)

func multilineNavigation(title:String,subTitle:String) {

    DispatchQueue.main.async {
        let titleAttributedStr = NSMutableAttributedString(string: title, attributes: [NSAttributedStringKey.foregroundColor: UIColor.orange,NSAttributedStringKey.font: UIFont(name: "Helvetica Neue", size: 17.0) ?? UIFont()])
        let subTitleAttributedStr = NSMutableAttributedString(string: "\n\(subTitle)", attributes: [NSAttributedStringKey.foregroundColor: UIColor.green,NSAttributedStringKey.font: UIFont(name: "Helvetica Neue", size: 12.0) ?? UIFont()])
        titleAttributedStr.append(subTitleAttributedStr)

        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 1
        paragraphStyle.lineBreakMode = .byTruncatingTail

        titleAttributedStr.addAttribute(.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, titleAttributedStr.length))

        let textView = UITextView()
        textView.attributedText = titleAttributedStr
        textView.backgroundColor = .clear
        textView.isUserInteractionEnabled = false
        textView.textContainerInset = .zero
        textView.textAlignment = .center
        textView.frame = CGRect(x: 0, y: 0, width: textView.intrinsicContentSize.width, height: 44)

        self.navigationItem.titleView = textView
    }
}
Leddy answered 28/6, 2019 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.