Skip to content
This repository has been archived by the owner on Feb 7, 2020. It is now read-only.

Commit

Permalink
Merge pull request #83 from cruffenach/master
Browse files Browse the repository at this point in the history
Enhancements and Fixes Regarding Attributed Text
  • Loading branch information
Sebastien Thiebaud committed Mar 3, 2014
2 parents 50f4197 + 0bc2eb8 commit a519f77
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -43,8 +43,8 @@ Blocks are easy. All you need to do is add a few lines of code:

## Methods

- `-[STTweetLabel setAttributes:(NSDictionary *)attributes]`: Dictionary with attributes for all text. (Important: You need to specify `NSForegroundColorAttributeName` and `NSFontAttributeName` mandatory).
- `-[STTweetLabel setAttributes:(NSDictionary *)attributes hotWord:(STTweetHotWord)hotWord]`: Dictionary with attributes for specific STTweetHotWord. (Important: You need to specify `NSForegroundColorAttributeName` and `NSFontAttributeName` mandatory).
- `-[STTweetLabel setAttributes:(NSDictionary *)attributes]`: Dictionary with attributes for all text.
- `-[STTweetLabel setAttributes:(NSDictionary *)attributes hotWord:(STTweetHotWord)hotWord]`: Dictionary with attributes for specific STTweetHotWord.
- `-[STTweetLabel suggestedFrameSizeToFitEntireStringConstraintedToWidth:(CGFloat)width`: Returns the CGSize calculated for the text submitted.

## Credits
Expand Down
33 changes: 29 additions & 4 deletions STTweetLabel/STTweetLabel.m
Expand Up @@ -260,17 +260,35 @@ - (void)setValidProtocols:(NSArray *)validProtocols {
}

- (void)setAttributes:(NSDictionary *)attributes {
if ([attributes objectForKey:NSFontAttributeName] == nil || [attributes objectForKey:NSForegroundColorAttributeName] == nil)
[NSException raise:NSInvalidArgumentException format:@"Attributes dictionary must contains NSFontAttributeName and NSForegroundColorAttributeName"];
if (!attributes[NSFontAttributeName]) {
NSMutableDictionary *copy = [attributes mutableCopy];
copy[NSFontAttributeName] = self.font;
attributes = [NSDictionary dictionaryWithDictionary:copy];
}

if (!attributes[NSForegroundColorAttributeName]) {
NSMutableDictionary *copy = [attributes mutableCopy];
copy[NSForegroundColorAttributeName] = self.textColor;
attributes = [NSDictionary dictionaryWithDictionary:copy];
}

_attributesText = attributes;

[self determineHotWords];
}

- (void)setAttributes:(NSDictionary *)attributes hotWord:(STTweetHotWord)hotWord {
if ([attributes objectForKey:NSFontAttributeName] == nil || [attributes objectForKey:NSForegroundColorAttributeName] == nil)
[NSException raise:NSInvalidArgumentException format:@"Attributes dictionary must contains NSFontAttributeName and NSForegroundColorAttributeName"];
if (!attributes[NSFontAttributeName]) {
NSMutableDictionary *copy = [attributes mutableCopy];
copy[NSFontAttributeName] = self.font;
attributes = [NSDictionary dictionaryWithDictionary:copy];
}

if (!attributes[NSForegroundColorAttributeName]) {
NSMutableDictionary *copy = [attributes mutableCopy];
copy[NSForegroundColorAttributeName] = self.textColor;
attributes = [NSDictionary dictionaryWithDictionary:copy];
}

switch (hotWord) {
case STTweetHandle:
Expand Down Expand Up @@ -308,6 +326,13 @@ - (void)setDetectionBlock:(void (^)(STTweetHotWord, NSString *, NSString *, NSRa
}
}

- (void)setAttributedText:(NSAttributedString *)attributedText {
self.text = attributedText.string;
if (self.text.length > 0) {
[self setAttributes:[attributedText attributesAtIndex:0 effectiveRange:NULL]];
}
}

#pragma mark -
#pragma mark Getters

Expand Down
14 changes: 0 additions & 14 deletions STTweetLabelExample/STTweetLabel Tests/STTweetLabel_Tests.m
Expand Up @@ -106,20 +106,6 @@ - (void)test_setAndGetAttributesForLink_setAttributes_attributes
XCTAssertEqualObjects(attributes, [_tweetLabel attributesForHotWord:STTweetLink], @"Link attributes should be %@ but %@ was returned instead.", attributes, [_tweetLabel attributesForHotWord:STTweetLink]);
}

- (void)test_setAndGetAttributesForText_setInvalidAttributes_exceptionThrown
{
NSDictionary *attributes = @{NSForegroundColorAttributeName: [UIColor redColor]};

XCTAssertThrowsSpecificNamed([_tweetLabel setAttributes:attributes], NSException, NSInvalidArgumentException, @"Attributes dictionary must contains NSFontAttributeName and NSForegroundColorAttributeName");
}

- (void)test_setAndGetAttributesForHandleHashtagLink_setInvalidAttributes_exceptionThrown
{
NSDictionary *attributes = @{NSForegroundColorAttributeName: [UIColor redColor]};

XCTAssertThrowsSpecificNamed([_tweetLabel setAttributes:attributes hotWord:0], NSException, NSInvalidArgumentException, @"Attributes dictionary must contains NSFontAttributeName and NSForegroundColorAttributeName");
}

- (void)test_setAndGetAttributesForText_setValidAttributes_exceptionNotThrown
{
NSDictionary *attributes = @{NSForegroundColorAttributeName: [UIColor redColor], NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14.0]};
Expand Down

0 comments on commit a519f77

Please sign in to comment.