Merge pull request #265 from andyscott/ASTextNode-max-line-count

Add maximumLineCount to ASTextNode
This commit is contained in:
Nadine Salter 2015-01-27 14:41:25 -08:00
commit 9f6ce8e16e
5 changed files with 38 additions and 1 deletions

View File

@ -64,6 +64,12 @@ typedef NS_ENUM(NSUInteger, ASTextNodeHighlightStyle) {
*/ */
@property (nonatomic, readonly, assign, getter=isTruncated) BOOL truncated; @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. @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 _renderer = [[ASTextNodeRenderer alloc] initWithAttributedString:_attributedString
truncationString:_composedTruncationString truncationString:_composedTruncationString
truncationMode:_truncationMode truncationMode:_truncationMode
maximumLineCount:_maximumLineCount
constrainedSize:constrainedSize]; constrainedSize:constrainedSize];
} }
return _renderer; return _renderer;
@ -905,6 +906,15 @@ ASDISPLAYNODE_INLINE CGFloat ceilPixelValue(CGFloat f)
return [[self _renderer] truncationStringCharacterRange].location != NSNotFound; return [[self _renderer] truncationStringCharacterRange].location != NSNotFound;
} }
- (void)setMaximumLineCount:(NSUInteger)maximumLineCount
{
if (_maximumLineCount != maximumLineCount) {
_maximumLineCount = maximumLineCount;
[self _invalidateRenderer];
[self setNeedsDisplay];
}
}
- (NSUInteger)lineCount - (NSUInteger)lineCount
{ {
return [[self _renderer] lineCount]; return [[self _renderer] lineCount];

View File

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

View File

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

View File

@ -18,6 +18,7 @@
@property (nonatomic, copy, readwrite) NSAttributedString *attributedString; @property (nonatomic, copy, readwrite) NSAttributedString *attributedString;
@property (nonatomic, copy, readwrite) NSAttributedString *truncationString; @property (nonatomic, copy, readwrite) NSAttributedString *truncationString;
@property (nonatomic, readwrite, assign) NSLineBreakMode truncationMode; @property (nonatomic, readwrite, assign) NSLineBreakMode truncationMode;
@property (nonatomic, readwrite, assign) NSUInteger maximumLineCount;
@property (nonatomic, readwrite, assign) CGFloat lineSpacing; @property (nonatomic, readwrite, assign) CGFloat lineSpacing;
@property (nonatomic, readwrite, assign) CGSize constrainedSize; @property (nonatomic, readwrite, assign) CGSize constrainedSize;
@ -50,6 +51,7 @@
_renderer = [[ASTextNodeRenderer alloc] initWithAttributedString:_attributedString _renderer = [[ASTextNodeRenderer alloc] initWithAttributedString:_attributedString
truncationString:_truncationString truncationString:_truncationString
truncationMode:_truncationMode truncationMode:_truncationMode
maximumLineCount:_maximumLineCount
constrainedSize:_constrainedSize]; constrainedSize:_constrainedSize];
} }
@ -71,6 +73,19 @@
XCTAssertTrue(numberOfLines == 1 , @"If constrained height (%f) is float max, then there should only be one line of text. Size %@", _constrainedSize.width, NSStringFromCGSize(size)); XCTAssertTrue(numberOfLines == 1 , @"If constrained height (%f) is float max, then there should only be one line of text. Size %@", _constrainedSize.width, NSStringFromCGSize(size));
} }
- (void)testMaximumLineCount
{
NSArray *lines = [NSArray arrayWithObjects:@"Hello!", @"world!", @"foo", @"bar", @"baz", nil];
_maximumLineCount = 2;
for (int i = 0; i <= [lines count]; i++) {
NSString *line = [[lines subarrayWithRange:NSMakeRange(0, i)] componentsJoinedByString:@"\n"];
_attributedString = [[NSAttributedString alloc] initWithString:line];
[self setUpRenderer];
[_renderer size];
XCTAssertTrue(_renderer.lineCount <= _maximumLineCount, @"The line count %tu after rendering should be no larger than the maximum line count %tu", _renderer.lineCount, _maximumLineCount);
}
}
- (void)testNoTruncationIfEnoughSpace - (void)testNoTruncationIfEnoughSpace
{ {
[self setUpRenderer]; [self setUpRenderer];
@ -124,7 +139,6 @@
[_renderer enumerateTextIndexesAtPosition:CGPointZero usingBlock:^(NSUInteger characterIndex, CGRect glyphBoundingRect, BOOL *stop) { [_renderer enumerateTextIndexesAtPosition:CGPointZero usingBlock:^(NSUInteger characterIndex, CGRect glyphBoundingRect, BOOL *stop) {
XCTFail(@"Shouldn't be any text indexes to enumerate"); XCTFail(@"Shouldn't be any text indexes to enumerate");
}]; }];
} }
@end @end