Skip to content

Commit

Permalink
Improved word-splitting algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Mar 21, 2013
1 parent a12dceb commit 036a6f7
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions FXLabel/FXLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,17 @@ - (BOOL)FXLabel_isPunctuation
@"", //en-dash
@"", //em-dash
@";",
@":",
nil] containsObject:self];
}

- (NSArray *)FXLabel_characters
{
NSUInteger length = [self length];
NSMutableArray *characters = [NSMutableArray arrayWithCapacity:length];
for (NSUInteger i = 0; i < length; i++)
{
NSRange range = [self rangeOfComposedCharacterSequenceAtIndex:i];
[characters addObject:[self substringWithRange:range]];
i += range.length - 1;
}
NSMutableArray *characters = [NSMutableArray array];
[self enumerateSubstringsInRange:NSMakeRange(0, [self length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {

[characters addObject:substring];
}];
return characters;
}

Expand All @@ -92,7 +90,7 @@ - (NSArray *)FXLabel_linesWithFont:(UIFont *)font
{
//split text into individual characters
NSArray *characters = [self FXLabel_characters];

//calculate lines
while (index < [characters count])
{
Expand Down Expand Up @@ -151,12 +149,18 @@ - (NSArray *)FXLabel_linesWithFont:(UIFont *)font
}
else
{
//split text into words
NSString *text = [self stringByReplacingOccurrencesOfString:@"\t" withString:@" "];
text = [text stringByReplacingOccurrencesOfString:@"\n" withString:@" \n "];
NSArray *words = [text componentsSeparatedByString:@" "];
words = [words filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]];
//TODO: handle hyphenation

//split text into words
NSMutableArray *words = [NSMutableArray array];
[self enumerateSubstringsInRange:NSMakeRange(0, [self length]) options:NSStringEnumerationByLines usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {

[words addObjectsFromArray:[substring componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];

[words addObject:@"\n"];
}];
[words removeLastObject];

//calculate lines
while (index < [words count])
{
Expand Down

0 comments on commit 036a6f7

Please sign in to comment.