iOS UILabel lineBreakMode省略号不显示

如下代码,限制label 行数两行,lineBreakMode = .byTruncatingTail,但是多余的字不会显示省略号

            titleLabel.numberOfLines = 2
            titleLabel.lineBreakMode = .byTruncatingTail            
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = 8
            let attriStr = NSMutableAttributedString(string: tmpText)
            attriStr.addAttributeFont(UIFont(name: "Helvetica-Bold", size: 34))
            attriStr.addAttributes([NSAttributedString.Key.foregroundColor:         
             UIColor.black], range: NSRange(location: 0, length: attriStr.length))
            attriStr.addAttribute(NSAttributedString.Key.paragraphStyle, value: 
            paragraphStyle, range: NSRange(location: 0, length: attriStr.length))
            titleLabel.attributedText = attriStr

将  titleLabel.lineBreakMode = .byTruncatingTail 放到赋值之后即可

            titleLabel.numberOfLines = 2
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = 8
            let attriStr = NSMutableAttributedString(string: tmpText)
            attriStr.addAttributeFont(UIFont(name: "Helvetica-Bold", size: 34))
            attriStr.addAttributes([NSAttributedString.Key.foregroundColor:         
             UIColor.black], range: NSRange(location: 0, length: attriStr.length))
            attriStr.addAttribute(NSAttributedString.Key.paragraphStyle, value: 
            paragraphStyle, range: NSRange(location: 0, length: attriStr.length))
            titleLabel.attributedText = attriStr
            titleLabel.lineBreakMode = .byTruncatingTail 

 

你可能感兴趣的:(iOS)