Skip to content

Instantly share code, notes, and snippets.

@haojianzong
Created January 19, 2015 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haojianzong/b59214fe714775da8a52 to your computer and use it in GitHub Desktop.
Save haojianzong/b59214fe714775da8a52 to your computer and use it in GitHub Desktop.
NSString routine that generates a fix length brief
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