Merge pull request #432 from tonklon/master

ASTextNode expose exclusion paths
This commit is contained in:
appleguy 2015-05-16 22:19:47 -10:00
commit 33c5a72002
6 changed files with 81 additions and 0 deletions

View File

@ -76,6 +76,8 @@ typedef NS_ENUM(NSUInteger, ASTextNodeHighlightStyle) {
*/ */
@property (nonatomic, readonly, assign) NSUInteger lineCount; @property (nonatomic, readonly, assign) NSUInteger lineCount;
@property (nonatomic, strong) NSArray *exclusionPaths;
#pragma mark - Placeholders #pragma mark - Placeholders
/** /**

View File

@ -87,6 +87,8 @@ ASDISPLAYNODE_INLINE CGFloat ceilPixelValue(CGFloat f)
CGFloat _shadowOpacity; CGFloat _shadowOpacity;
CGFloat _shadowRadius; CGFloat _shadowRadius;
NSArray *_exclusionPaths;
NSAttributedString *_composedTruncationString; NSAttributedString *_composedTruncationString;
NSString *_highlightedLinkAttributeName; NSString *_highlightedLinkAttributeName;
@ -281,6 +283,7 @@ ASDISPLAYNODE_INLINE CGFloat ceilPixelValue(CGFloat f)
truncationString:_composedTruncationString truncationString:_composedTruncationString
truncationMode:_truncationMode truncationMode:_truncationMode
maximumLineCount:_maximumLineCount maximumLineCount:_maximumLineCount
exclusionPaths:_exclusionPaths
constrainedSize:constrainedSize]; constrainedSize:constrainedSize];
} }
return _renderer; return _renderer;
@ -349,6 +352,23 @@ ASDISPLAYNODE_INLINE CGFloat ceilPixelValue(CGFloat f)
} }
} }
#pragma mark - Text Layout
- (void)setExclusionPaths:(NSArray *)exclusionPaths
{
if ((_exclusionPaths == nil && exclusionPaths != nil) || (![_exclusionPaths isEqualToArray:exclusionPaths])) {
_exclusionPaths = exclusionPaths;
[self _invalidateRenderer];
[self invalidateCalculatedSize];
[self setNeedsDisplay];
}
}
- (NSArray *)exclusionPaths
{
return _exclusionPaths;
}
#pragma mark - Drawing #pragma mark - Drawing
+ (void)drawRect:(CGRect)bounds withParameters:(ASTextNodeDrawParameters *)parameters isCancelled:(asdisplaynode_iscancelled_block_t)isCancelledBlock isRasterizing:(BOOL)isRasterizing + (void)drawRect:(CGRect)bounds withParameters:(ASTextNodeDrawParameters *)parameters isCancelled:(asdisplaynode_iscancelled_block_t)isCancelledBlock isRasterizing:(BOOL)isRasterizing

View File

@ -45,6 +45,12 @@ typedef NS_ENUM(NSUInteger, ASTextNodeRendererMeasureOption) {
*/ */
@interface ASTextNodeRenderer : NSObject @interface ASTextNodeRenderer : NSObject
- (instancetype)initWithAttributedString:(NSAttributedString *)attributedString
truncationString:(NSAttributedString *)truncationString
truncationMode:(NSLineBreakMode)truncationMode
maximumLineCount:(NSUInteger)maximumLineCount
exclusionPaths:(NSArray *)exclusionPaths
constrainedSize:(CGSize)constrainedSize;
/* /*
* Designated Initializer * Designated Initializer
* *

View File

@ -39,6 +39,8 @@ static const CGFloat ASTextNodeRendererTextCapHeightPadding = 1.3;
NSLayoutManager *_layoutManager; NSLayoutManager *_layoutManager;
NSTextStorage *_textStorage; NSTextStorage *_textStorage;
NSTextContainer *_textContainer; NSTextContainer *_textContainer;
NSArray *_exclusionPaths;
} }
#pragma mark - Initialization #pragma mark - Initialization
@ -47,6 +49,7 @@ static const CGFloat ASTextNodeRendererTextCapHeightPadding = 1.3;
truncationString:(NSAttributedString *)truncationString truncationString:(NSAttributedString *)truncationString
truncationMode:(NSLineBreakMode)truncationMode truncationMode:(NSLineBreakMode)truncationMode
maximumLineCount:(NSUInteger)maximumLineCount maximumLineCount:(NSUInteger)maximumLineCount
exclusionPaths:(NSArray *)exclusionPaths
constrainedSize:(CGSize)constrainedSize constrainedSize:(CGSize)constrainedSize
{ {
if (self = [super init]) { if (self = [super init]) {
@ -57,11 +60,22 @@ static const CGFloat ASTextNodeRendererTextCapHeightPadding = 1.3;
_maximumLineCount = maximumLineCount; _maximumLineCount = maximumLineCount;
_exclusionPaths = exclusionPaths;
_constrainedSize = constrainedSize; _constrainedSize = constrainedSize;
} }
return self; return self;
} }
- (instancetype)initWithAttributedString:(NSAttributedString *)attributedString
truncationString:(NSAttributedString *)truncationString
truncationMode:(NSLineBreakMode)truncationMode
maximumLineCount:(NSUInteger)maximumLineCount
constrainedSize:(CGSize)constrainedSize
{
return [self initWithAttributedString:attributedString truncationString:truncationString truncationMode:truncationMode maximumLineCount:maximumLineCount exclusionPaths:nil constrainedSize:constrainedSize];
}
/* /*
* Use this method to lazily construct the TextKit components. * Use this method to lazily construct the TextKit components.
*/ */
@ -97,6 +111,8 @@ static const CGFloat ASTextNodeRendererTextCapHeightPadding = 1.3;
// Set maximum number of lines // Set maximum number of lines
_textContainer.maximumNumberOfLines = _maximumLineCount; _textContainer.maximumNumberOfLines = _maximumLineCount;
_textContainer.exclusionPaths = _exclusionPaths;
[_layoutManager addTextContainer:_textContainer]; [_layoutManager addTextContainer:_textContainer];
ASDN::StaticMutexUnlocker gu(mutex); ASDN::StaticMutexUnlocker gu(mutex);

View File

@ -22,6 +22,7 @@
@property (nonatomic, readwrite, assign) CGFloat lineSpacing; @property (nonatomic, readwrite, assign) CGFloat lineSpacing;
@property (nonatomic, readwrite, assign) CGSize constrainedSize; @property (nonatomic, readwrite, assign) CGSize constrainedSize;
@property (nonatomic, readwrite) NSArray *exclusionPaths;
@end @end
@ -43,6 +44,8 @@
_attributedString = [[NSAttributedString alloc] initWithString:@"Lorem ipsum" attributes:attributes]; _attributedString = [[NSAttributedString alloc] initWithString:@"Lorem ipsum" attributes:attributes];
_truncationString = [[NSAttributedString alloc] initWithString:@"More"]; _truncationString = [[NSAttributedString alloc] initWithString:@"More"];
_exclusionPaths = nil;
_constrainedSize = CGSizeMake(FLT_MAX, FLT_MAX); _constrainedSize = CGSizeMake(FLT_MAX, FLT_MAX);
} }
@ -52,6 +55,7 @@
truncationString:_truncationString truncationString:_truncationString
truncationMode:_truncationMode truncationMode:_truncationMode
maximumLineCount:_maximumLineCount maximumLineCount:_maximumLineCount
exclusionPaths:_exclusionPaths
constrainedSize:_constrainedSize]; constrainedSize:_constrainedSize];
} }
@ -141,4 +145,18 @@
}]; }];
} }
- (void)testExclusionPaths
{
_constrainedSize = CGSizeMake(200, CGFLOAT_MAX);
[self setUpRenderer];
CGSize sizeWithoutExclusionPath = [_renderer size];
CGRect exclusionRect = CGRectMake(20, 0, 180, _lineSpacing * 2.0);
_exclusionPaths = @[[UIBezierPath bezierPathWithRect:exclusionRect]];
[self setUpRenderer];
CGSize sizeWithExclusionPath = [_renderer size];
XCTAssertEqualWithAccuracy(sizeWithoutExclusionPath.height + exclusionRect.size.height, sizeWithExclusionPath.height, 0.5, @"Using an exclusion path so the the text can not fit into the first two lines should increment the size of the text by the heigth of the exclusion path");
}
@end @end

View File

@ -177,4 +177,23 @@
XCTAssertFalse(delegate.tappedLinkValue, @"Expected the delegate to be told that the value %@ was tapped, instead it thinks the tapped attribute value is %@", linkAttributeValue, delegate.tappedLinkValue); XCTAssertFalse(delegate.tappedLinkValue, @"Expected the delegate to be told that the value %@ was tapped, instead it thinks the tapped attribute value is %@", linkAttributeValue, delegate.tappedLinkValue);
} }
#pragma mark exclusion Paths
- (void)testSettingExclusionPaths
{
NSArray *exclusionPaths = @[[UIBezierPath bezierPathWithRect:CGRectMake(10, 20, 30, 40)]];
_textNode.exclusionPaths = exclusionPaths;
XCTAssertTrue([_textNode.exclusionPaths isEqualToArray:exclusionPaths], @"Failed to set exclusion paths");
}
- (void)testAddingExclusionPathsShouldInvalidateAndIncreaseTheSize
{
CGSize constrainedSize = CGSizeMake(100, CGFLOAT_MAX);
CGSize sizeWithoutExclusionPaths = [_textNode measure:constrainedSize];
_textNode.exclusionPaths = @[[UIBezierPath bezierPathWithRect:CGRectMake(50, 20, 30, 40)]];
CGSize sizeWithExclusionPaths = [_textNode measure:constrainedSize];
XCTAssertGreaterThan(sizeWithExclusionPaths.height, sizeWithoutExclusionPaths.height, @"Setting exclusions paths should invalidate the calculated size and return a greater size");
}
@end @end