Created
January 19, 2015 03:38
-
-
Save haojianzong/b59214fe714775da8a52 to your computer and use it in GitHub Desktop.
NSString routine that generates a fix length brief
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static NSInteger mYStringlength = 60; | |
- (NSString *)brief:(NSString *)text | |
{ | |
NSRange stringRange = {0, MIN([text length], mYStringlength)}; | |
stringRange = [text rangeOfComposedCharacterSequencesForRange:stringRange]; | |
NSString *shortBrief = [text substringWithRange:stringRange]; | |
NSString *firstLine =[[self.text componentsSeparatedByString: @"\n"] objectAtIndex:0]; | |
// if firstLine is shorter than our expected length | |
if (firstLine.length < mYStringlength) { | |
return firstLine; | |
} | |
// truncate desc by length if text is too long | |
if (mYStringlength < desc.length) { | |
return [NSString stringWithFormat:@"%@...", shortBrief]; | |
} else { | |
return text; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment