Add maximumLineCount to ASTextNode... which corresponds to maximumNumberOfLines on the underlying container

This commit is contained in:
andyscott 2015-01-26 21:05:28 -08:00
parent f200f9504a
commit 72ea1c94df
4 changed files with 23 additions and 0 deletions

View File

@ -64,6 +64,12 @@ typedef NS_ENUM(NSUInteger, ASTextNodeHighlightStyle) {
*/
@property (nonatomic, readonly, assign, getter=isTruncated) BOOL truncated;
/**
@abstract The maximum number of lines to render of the text before truncation.
@default 0 (No limit)
*/
@property (nonatomic, assign) NSUInteger maximumLineCount;
/**
@abstract The number of lines in the text. Text must have been sized first.
*/

View File

@ -243,6 +243,7 @@ ASDISPLAYNODE_INLINE CGFloat ceilPixelValue(CGFloat f)
_renderer = [[ASTextNodeRenderer alloc] initWithAttributedString:_attributedString
truncationString:_composedTruncationString
truncationMode:_truncationMode
maximumLineCount:_maximumLineCount
constrainedSize:constrainedSize];
}
return _renderer;
@ -905,6 +906,15 @@ ASDISPLAYNODE_INLINE CGFloat ceilPixelValue(CGFloat f)
return [[self _renderer] truncationStringCharacterRange].location != NSNotFound;
}
- (void)setMaximumLineCount:(NSUInteger)maximumLineCount
{
if (_maximumLineCount != maximumLineCount) {
_maximumLineCount = maximumLineCount;
[self _invalidateRenderer];
[self setNeedsDisplay];
}
}
- (NSUInteger)lineCount
{
return [[self _renderer] lineCount];

View File

@ -54,6 +54,7 @@ typedef NS_ENUM(NSUInteger, ASTextNodeRendererMeasureOption) {
- (instancetype)initWithAttributedString:(NSAttributedString *)attributedString
truncationString:(NSAttributedString *)truncationString
truncationMode:(NSLineBreakMode)truncationMode
maximumLineCount:(NSUInteger)maximumLineCount
constrainedSize:(CGSize)constrainedSize;
#pragma mark - Drawing
/*

View File

@ -29,6 +29,7 @@ static const CGFloat ASTextNodeRendererTextCapHeightPadding = 1.3;
NSAttributedString *_attributedString;
NSAttributedString *_truncationString;
NSLineBreakMode _truncationMode;
NSUInteger _maximumLineCount;
NSRange _truncationCharacterRange;
NSRange _visibleRange;
@ -45,6 +46,7 @@ static const CGFloat ASTextNodeRendererTextCapHeightPadding = 1.3;
- (instancetype)initWithAttributedString:(NSAttributedString *)attributedString
truncationString:(NSAttributedString *)truncationString
truncationMode:(NSLineBreakMode)truncationMode
maximumLineCount:(NSUInteger)maximumLineCount
constrainedSize:(CGSize)constrainedSize
{
if (self = [super init]) {
@ -52,6 +54,8 @@ static const CGFloat ASTextNodeRendererTextCapHeightPadding = 1.3;
_truncationString = truncationString;
_truncationMode = truncationMode;
_truncationCharacterRange = NSMakeRange(NSNotFound, truncationString.length);
_maximumLineCount = maximumLineCount;
_constrainedSize = constrainedSize;
}
@ -90,6 +94,8 @@ static const CGFloat ASTextNodeRendererTextCapHeightPadding = 1.3;
_textContainer.lineFragmentPadding = 0;
// Translate our truncation mode into a line break mode on the container
_textContainer.lineBreakMode = _truncationMode;
// Set maximum number of lines
_textContainer.maximumNumberOfLines = _maximumLineCount;
[_layoutManager addTextContainer:_textContainer];