この方法で最後の単語を分割せずに NSString の長さをカットしようとしています:
// cut a string by words
- (NSString* )stringCutByWords:(NSString *)string toLength:(int)length;
{
// search backwards in the string for the beginning of the last word
while ([string characterAtIndex:length] != ' ' && length > 0) {
length--;
}
// if the last word was the first word of the string search for the end of the word
if (length <= 0){
while ([string characterAtIndex:length] != ' ' && length > string.length-1) {
length++;
}
}
// define the range you're interested in
NSRange stringRange = {0, length};
// adjust the range to include dependent chars
stringRange = [string rangeOfComposedCharacterSequencesForRange:stringRange];
// Now you can create the short string
string = [string substringWithRange:stringRange];
return [NSString stringWithFormat:@"%@...",string];
}
今私の質問は次のとおりです:私が見たことのないobjective-cまたはcocoa-touchに組み込みの方法はありますか、それともこのソリューションにあまり満足していないため、これを行うための「より良い」方法はありますか?
ご挨拶と助けてくれてありがとう C4rmel