The biggest advantage of using YYLabel is that it can be drawn asynchronously to keep the interface as smooth as possible, but by default, when the text exceeds the specified number of lines, it cannot be displayed in the style of ellipsis.

Set up YYLabel lineBreakMode = NSLineBreakByTruncatingTail not have? After all, YYLabel is not UILabel, the underlying implementation is different, there are two solutions:

Plan a

Set up a new textLayout reset every time YYLabel a YYLabel lineBreakMode NSLineBreakByTruncatingTail.

self.titleLabel.textLayout = titleLayout;
self.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
Copy the code

PS: The premise of using this solution is that the ignoreCommonProperties of YYLabel is NO. If ignoreCommonProperties is set to YES, properties such as text, font, textColor, attributedText, and lineBreakMode are not available. This is to improve performance. Make control properties static whenever possible)

Option 2 (recommended)

YYKit such a powerful library is impossible not to deal with this situation, after review found that by setting the YYTextLayout YYTextContainer to achieve: set the truncationType to YYTextTruncationTypeEnd.

UIFont *font = WTVPUGCProfilePlayView.videoTitleFont;
NSDictionary *attDic = @{NSFontAttributeName: font, NSForegroundColorAttributeName: WTVPUGCProfilePlayView.videoTitleColor};
NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:videoTitle attributes:attDic];

YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(WTVPUGCProfilePlayView.videoTitleMaxWidth, 999)];
container.maximumNumberOfRows = WTVPUGCProfilePlayView.videoTitleMaxRows; // Max 2 rows
container.truncationType = YYTextTruncationTypeEnd; // Set the end to an ellipsis. The default is just truncation
    
YYTextLayout *videoTitleLayout = [YYTextLayout layoutWithContainer:container text:attStr];
Copy the code

YYTextContainer
truncationToken

container.truncationToken = [[NSAttributedString alloc] initWithString:@ "..." attributes:attDic]; 
Copy the code

Since this is rich text, you can not only set the text, but also customize some images, buttons, etc., but the ellipsis font is synchronized.

Today YYKit is still very powerful and practical 👍.