swiftuilabel

Truncate trail with AttributedString


I have a UILabel in a cell, and I am setting attributed text in it. It attributed text is too large than it should come with ... at the end. How to do that ?


Solution

  • func addTitle(title: String) {
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineBreakMode = .byTruncatingTail
        let attributedTitle = NSAttributedString(string: title, attributes: [NSParagraphStyleAttributeName: paragraphStyle])
        self.titleLabel.attributtedString = attributedTitle
    }