Getting UILabel to produce an ellipsis rather than shrinking the font

前端 未结 4 1006
无人及你
无人及你 2020-12-03 04:49

When I dynamically change the text of a UILabel I would prefer to get an ellipsis (dot, dot, dot) rather then have the text be automatically resized. How does one do this?

相关标签:
4条回答
  • 2020-12-03 05:01

    I had an issue producing ellipsis after I styled a UILabel and needed to use UILabel.attributedText instead of UILabel.text. There is a line break mode on the paragraph style that will overwrite the UILabel.lineBreakMode when using attributed text. You'll need to set the lineBreakMode to .byTruncatingTail on the attributed string's paragraph style if you want to achieve ellipsis.

    e.g.

        let attributedString = NSMutableAttributedString(string: "example long string", attributes: [...])
        let textRange = NSRange(location: 0, length: textString.length)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineBreakMode = .byTruncatingTail
        textString.addAttribute(.paragraphStyle, value: paragraphStyle, range: textRange)
        uiLabel.attributedText = attributedString
    
    0 讨论(0)
  • 2020-12-03 05:03

    Swift solution:

    label.lineBreakMode = .ByTruncatingTail
    

    Swift 3:

    label.lineBreakMode = .byTruncatingTail
    
    0 讨论(0)
  • 2020-12-03 05:23

    Set the following properties:

    Objective C

    label.adjustsFontSizeToFitWidth = NO;
    label.lineBreakMode = NSLineBreakByTruncatingTail;
    

    Swift

    label.adjustsFontSizeToFitWidth = false
    label.lineBreakMode = .byTruncatingTail
    

    You can also set these properties in interface builder.

    0 讨论(0)
  • 2020-12-03 05:24

    I have achieved by following steps:

    1.Increase height of label as I did so that 2 lines fit.

    2.select label, go to attributes inspector

    3.select Line Breaks = Word Wrap

    4.also increase lines, like 2,3

    changing line break in attributes inspector Worked for me

    0 讨论(0)
提交回复
热议问题