diff --git a/STTweetLabel.podspec b/STTweetLabel.podspec index 3950b06..d90ef2f 100644 --- a/STTweetLabel.podspec +++ b/STTweetLabel.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "STTweetLabel" - s.version = "2.0.1" + s.version = "2.0.2" s.summary = "A custom UILabel view controller for iOS with certain words tappable like Twitter." s.homepage = "https://github.com/SebastienThiebaud/STTweetLabel" @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.author = { "Sebastien THIEBAUD" => "sthiebaud@icloud.com" } s.source = { :git => "https://github.com/SebastienThiebaud/STTweetLabel.git", - :tag => "2.0.1" + :tag => "2.0.2" } s.platform = :ios, '5.0' diff --git a/STTweetLabel/STTweetLabel.m b/STTweetLabel/STTweetLabel.m index b7a5bd8..7ff7cb7 100644 --- a/STTweetLabel/STTweetLabel.m +++ b/STTweetLabel/STTweetLabel.m @@ -33,8 +33,6 @@ - (void)drawTextInRect:(CGRect)rect { -// [super drawTextInRect:rect]; - if (_fontHashtag == nil) { _fontHashtag = self.font; @@ -65,122 +63,152 @@ // Regex to catch newline NSRegularExpression *regexNewLine = [NSRegularExpression regularExpressionWithPattern:@">newLine" options:NSRegularExpressionCaseInsensitive error:&error]; - for (NSString *word in words) + BOOL loopWord = NO; + + for (NSString *wordArray in words) { - NSLog(@"%@", word); - CGSize sizeWord = [word sizeWithFont:self.font]; + NSString *word = @""; + NSMutableString *alreadyPrintedWord = [[NSMutableString alloc] init]; - // Test if the new word must be in a new line - if (drawPoint.x + sizeWord.width > rect.size.width) + do { - drawPoint = CGPointMake(0.0, drawPoint.y + sizeWord.height); - } + word = [wordArray substringFromIndex:[alreadyPrintedWord length]]; + + CGSize sizeWord = [word sizeWithFont:self.font]; + + if (sizeWord.width <= rect.size.width) + { + loopWord = NO; + } + + // If the word is larger than the container's size + while (sizeWord.width > rect.size.width) + { + NSString *cutWord = [self cutTheString:word atPoint:drawPoint]; - NSTextCheckingResult *match = [regex firstMatchInString:word options:0 range:NSMakeRange(0, [word length])]; - - // Dissolve the word (for example a hashtag: #youtube!, we want only #youtube) - NSString *preCharacters = [word substringToIndex:match.range.location]; - NSString *wordCharacters = [word substringWithRange:match.range]; - NSString *postCharacters = [word substringFromIndex:match.range.location + match.range.length]; - - // Draw the prefix of the word (if it has a prefix) - if (![preCharacters isEqualToString:@""]) - { - // Shadow case - if (self.shadowColor != NULL) - { - [self.shadowColor set]; - [preCharacters drawAtPoint:CGPointMake(drawPoint.x + self.shadowOffset.width, drawPoint.y + self.shadowOffset.height) withFont:self.font]; + [alreadyPrintedWord appendString:cutWord]; + + word = cutWord; + sizeWord = [word sizeWithFont:self.font]; + + loopWord = YES; } - - [self.textColor set]; - CGSize sizePreCharacters = [preCharacters sizeWithFont:self.font]; - [preCharacters drawAtPoint:drawPoint withFont:self.font]; - - drawPoint = CGPointMake(drawPoint.x + sizePreCharacters.width, drawPoint.y); - } - - // Draw the touchable word - if (![wordCharacters isEqualToString:@""]) - { - // Shadow case - if (self.shadowColor != NULL) + + // Test if the new word must be in a new line + if (drawPoint.x + sizeWord.width > rect.size.width) { - [self.shadowColor set]; - [wordCharacters drawAtPoint:CGPointMake(drawPoint.x + self.shadowOffset.width, drawPoint.y + self.shadowOffset.height) withFont:self.font]; - } - - // Set the color for mention/hashtag OR weblink - if ([wordCharacters hasPrefix:@"#"] || [wordCharacters hasPrefix:@"@"]) - { - [_colorHashtag set]; - } - else if ([wordCharacters hasPrefix:@"http"]) - { - [_colorLink set]; - } - - CGSize sizeWordCharacters = [wordCharacters sizeWithFont:self.font]; - [wordCharacters drawAtPoint:drawPoint withFont:self.font]; - - // Stock the touchable zone - [touchWords addObject:wordCharacters]; - [touchLocations addObject:[NSValue valueWithCGRect:CGRectMake(drawPoint.x, drawPoint.y, sizeWordCharacters.width, sizeWordCharacters.height)]]; - - drawPoint = CGPointMake(drawPoint.x + sizeWordCharacters.width, drawPoint.y); - } - - // Draw the suffix of the word (if it has a suffix) else the word is not touchable - if (![postCharacters isEqualToString:@""]) - { - NSTextCheckingResult *matchNewLine = [regexNewLine firstMatchInString:postCharacters options:0 range:NSMakeRange(0, [postCharacters length])]; - - // If a newline is match - if (matchNewLine) - { - // Shadow case - if (self.shadowColor != NULL) - { - [self.shadowColor set]; - [[postCharacters substringToIndex:matchNewLine.range.location] drawAtPoint:CGPointMake(drawPoint.x + self.shadowOffset.width, drawPoint.y + self.shadowOffset.height) withFont:self.font]; - } - - [self.textColor set]; - - [[postCharacters substringToIndex:matchNewLine.range.location] drawAtPoint:drawPoint withFont:self.font]; drawPoint = CGPointMake(0.0, drawPoint.y + sizeWord.height); - - // Shadow case - if (self.shadowColor != NULL) - { - [self.shadowColor set]; - [[postCharacters substringFromIndex:matchNewLine.range.location + matchNewLine.range.length] drawAtPoint:CGPointMake(drawPoint.x + self.shadowOffset.width, drawPoint.y + self.shadowOffset.height) withFont:self.font]; - } - - [self.textColor set]; - - [[postCharacters substringFromIndex:matchNewLine.range.location + matchNewLine.range.length] drawAtPoint:drawPoint withFont:self.font]; - drawPoint = CGPointMake(drawPoint.x + [[postCharacters substringFromIndex:matchNewLine.range.location + matchNewLine.range.length] sizeWithFont:self.font].width, drawPoint.y); } - else + + NSTextCheckingResult *match = [regex firstMatchInString:word options:0 range:NSMakeRange(0, [word length])]; + + // Dissolve the word (for example a hashtag: #youtube!, we want only #youtube) + NSString *preCharacters = [word substringToIndex:match.range.location]; + NSString *wordCharacters = [word substringWithRange:match.range]; + NSString *postCharacters = [word substringFromIndex:match.range.location + match.range.length]; + + // Draw the prefix of the word (if it has a prefix) + if (![preCharacters isEqualToString:@""]) { // Shadow case if (self.shadowColor != NULL) { [self.shadowColor set]; - [postCharacters drawAtPoint:CGPointMake(drawPoint.x + self.shadowOffset.width, drawPoint.y + self.shadowOffset.height) withFont:self.font]; + [preCharacters drawAtPoint:CGPointMake(drawPoint.x + self.shadowOffset.width, drawPoint.y + self.shadowOffset.height) withFont:self.font]; } - + [self.textColor set]; - CGSize sizePostCharacters = [postCharacters sizeWithFont:self.font]; - [postCharacters drawAtPoint:drawPoint withFont:self.font]; - - drawPoint = CGPointMake(drawPoint.x + sizePostCharacters.width, drawPoint.y); + CGSize sizePreCharacters = [preCharacters sizeWithFont:self.font]; + [preCharacters drawAtPoint:drawPoint withFont:self.font]; + + drawPoint = CGPointMake(drawPoint.x + sizePreCharacters.width, drawPoint.y); } - } - - drawPoint = CGPointMake(drawPoint.x + sizeSpace.width, drawPoint.y); - } + + // Draw the touchable word + if (![wordCharacters isEqualToString:@""]) + { + // Shadow case + if (self.shadowColor != NULL) + { + [self.shadowColor set]; + [wordCharacters drawAtPoint:CGPointMake(drawPoint.x + self.shadowOffset.width, drawPoint.y + self.shadowOffset.height) withFont:self.font]; + } + + // Set the color for mention/hashtag OR weblink + if ([wordCharacters hasPrefix:@"#"] || [wordCharacters hasPrefix:@"@"]) + { + [_colorHashtag set]; + } + else if ([wordCharacters hasPrefix:@"http"]) + { + [_colorLink set]; + } + + CGSize sizeWordCharacters = [wordCharacters sizeWithFont:self.font]; + [wordCharacters drawAtPoint:drawPoint withFont:self.font]; + + // Stock the touchable zone + [touchWords addObject:wordCharacters]; + [touchLocations addObject:[NSValue valueWithCGRect:CGRectMake(drawPoint.x, drawPoint.y, sizeWordCharacters.width, sizeWordCharacters.height)]]; + + drawPoint = CGPointMake(drawPoint.x + sizeWordCharacters.width, drawPoint.y); + } + + // Draw the suffix of the word (if it has a suffix) else the word is not touchable + if (![postCharacters isEqualToString:@""]) + { + NSTextCheckingResult *matchNewLine = [regexNewLine firstMatchInString:postCharacters options:0 range:NSMakeRange(0, [postCharacters length])]; + + // If a newline is match + if (matchNewLine) + { + // Shadow case + if (self.shadowColor != NULL) + { + [self.shadowColor set]; + [[postCharacters substringToIndex:matchNewLine.range.location] drawAtPoint:CGPointMake(drawPoint.x + self.shadowOffset.width, drawPoint.y + self.shadowOffset.height) withFont:self.font]; + } + + [self.textColor set]; + + [[postCharacters substringToIndex:matchNewLine.range.location] drawAtPoint:drawPoint withFont:self.font]; + drawPoint = CGPointMake(0.0, drawPoint.y + sizeWord.height); + + // Shadow case + if (self.shadowColor != NULL) + { + [self.shadowColor set]; + [[postCharacters substringFromIndex:matchNewLine.range.location + matchNewLine.range.length] drawAtPoint:CGPointMake(drawPoint.x + self.shadowOffset.width, drawPoint.y + self.shadowOffset.height) withFont:self.font]; + } + + [self.textColor set]; + + [[postCharacters substringFromIndex:matchNewLine.range.location + matchNewLine.range.length] drawAtPoint:drawPoint withFont:self.font]; + drawPoint = CGPointMake(drawPoint.x + [[postCharacters substringFromIndex:matchNewLine.range.location + matchNewLine.range.length] sizeWithFont:self.font].width, drawPoint.y); + } + else + { + // Shadow case + if (self.shadowColor != NULL) + { + [self.shadowColor set]; + [postCharacters drawAtPoint:CGPointMake(drawPoint.x + self.shadowOffset.width, drawPoint.y + self.shadowOffset.height) withFont:self.font]; + } + + [self.textColor set]; + CGSize sizePostCharacters = [postCharacters sizeWithFont:self.font]; + [postCharacters drawAtPoint:drawPoint withFont:self.font]; + + drawPoint = CGPointMake(drawPoint.x + sizePostCharacters.width, drawPoint.y); + } + } + + if (!loopWord) + { + drawPoint = CGPointMake(drawPoint.x + sizeSpace.width, drawPoint.y); + } + } while (loopWord); + } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event @@ -261,4 +289,23 @@ return htmlString; } +// Cut a string to display just the beginning in the container +- (NSString *)cutTheString:(NSString *)word atPoint:(CGPoint)drawPoint +{ + NSString *substring; + + for (int i = 1; i < [word length]; i++) + { + substring = [word substringToIndex:[word length] - i]; + CGSize sizeSubstring = [substring sizeWithFont:self.font]; + + if (drawPoint.x + sizeSubstring.width <= self.frame.size.width) + { + break; + } + } + + return substring; +} + @end diff --git a/STTweetLabelExample/STTweetLabelExample.xcodeproj/project.xcworkspace/xcuserdata/sebastienthiebaud.xcuserdatad/UserInterfaceState.xcuserstate b/STTweetLabelExample/STTweetLabelExample.xcodeproj/project.xcworkspace/xcuserdata/sebastienthiebaud.xcuserdatad/UserInterfaceState.xcuserstate index 95711b2..27238d4 100644 Binary files a/STTweetLabelExample/STTweetLabelExample.xcodeproj/project.xcworkspace/xcuserdata/sebastienthiebaud.xcuserdatad/UserInterfaceState.xcuserstate and b/STTweetLabelExample/STTweetLabelExample.xcodeproj/project.xcworkspace/xcuserdata/sebastienthiebaud.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/STTweetLabelExample/STTweetLabelExample/STViewController.m b/STTweetLabelExample/STTweetLabelExample/STViewController.m index f2df147..bef519b 100755 --- a/STTweetLabelExample/STTweetLabelExample/STViewController.m +++ b/STTweetLabelExample/STTweetLabelExample/STViewController.m @@ -31,10 +31,11 @@ _tweetLabel = [[STTweetLabel alloc] initWithFrame:CGRectMake(20.0, 60.0, 280.0, 200.0)]; [_tweetLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:17.0]]; [_tweetLabel setTextColor:[UIColor blackColor]]; + [_tweetLabel setText:@"Hi.This is a new tool for @you! Developed by->@SebThiebaud for #iPhone #ObjC... ;-)\nMy GitHub page: https://t.co/pQXDoiYA"]; + +// Shadow // [_tweetLabel setShadowColor:[UIColor colorWithWhite:0.8 alpha:1.0]]; // [_tweetLabel setShadowOffset:CGSizeMake(2.0, 1.0)]; - [_tweetLabel setText:@"Hi. This is a new tool for @you! Developed by->@SebThiebaud for #iPhone #ObjC... ;-)\nMy GitHub page: https://t.co/pQXDoiYA"]; - STLinkCallbackBlock callbackBlock = ^(STLinkActionType actionType, NSString *link) {