Skip to content

Commit

Permalink
Merge pull request Cocoanetics#1064 from seedotlee/develop-attachment
Browse files Browse the repository at this point in the history
DTTextAttachmentHTMLElement to fit 100% width
  • Loading branch information
odrobnik authored Apr 19, 2017
2 parents b7c615e + 9373d3f commit f3d3b6c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Core/Source/DTTextAttachmentHTMLElement.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
_textAttachment.originalSize = _size;
}

NSString *widthString = [styles objectForKey:@"width"];

if (widthString.length > 1 && [widthString hasSuffix:@"%"])
{
CGFloat scale = (CGFloat)([[widthString substringToIndex:widthString.length - 1] floatValue] / 100.0);

_size.width = _maxDisplaySize.width * scale;
}

// update the display size
[_textAttachment setDisplaySize:_size withMaxDisplaySize:_maxDisplaySize];
}
Expand Down
26 changes: 26 additions & 0 deletions Test/Source/DTHTMLElementTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,30 @@ - (void)testAttachmentWithDisplayNone
XCTAssertNil(attributedString, @"Text attachment should be invisible");
}

- (void)testAttachmentWithPercentWidth
{
NSDictionary *styles1 = @{@"width" : @"100%"};
NSDictionary *styles2 = @{@"width" : @"80%"};
NSDictionary *styles3 = @{@"width" : @"110%"};

CGSize maxImageSize = CGSizeMake(500,500);

NSMutableDictionary *options = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSValue valueWithCGSize:maxImageSize], DTMaxImageSize,
[NSNumber numberWithFloat:16.0],DTDefaultFontSize,
nil];

DTHTMLElement *attachment = [DTHTMLElement elementWithName:@"img" attributes:nil options:options];

[attachment applyStyleDictionary:styles1];
XCTAssertEqual(attachment.textAttachment.displaySize.width, 500, @"Text attachment width incorrect");

[attachment applyStyleDictionary:styles2];
XCTAssertEqual(attachment.textAttachment.displaySize.width, 400, @"Text attachment width incorrect");

[attachment applyStyleDictionary:styles3];
XCTAssertEqual(attachment.textAttachment.displaySize.width, 500, @"Text attachment width incorrect");

}

@end

0 comments on commit f3d3b6c

Please sign in to comment.