
如何在不消除可能在我的长度中间发出的unicode字符的情况下截断给定长度的字符串?如何确定字符串中unicode字符开头的索引,以便我可以避免创建丑陋的字符串.具有A可见的一半的正方形是另一个被截断的表情符号字符的位置.
-(NSMutableAttributedString*)constructStatusAttributedStringWithRange:(CFRange)range
NSString *original = [_postDictionay objectForKey:@"message"];
NSMutableString *truncated = [NSMutableString string];
NSArray *components = [original componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
for(int x=0; x<[components count]; x++)
{
//If the truncated string is still shorter then the range desired. (leave space for ...)
if([truncated length]+[[components objectAtIndex:x] length]<range.length-3)
{
//Just checking if its the first word
if([truncated length]==0 && x==0)
{
//start off the string
[truncated appendString:[components objectAtIndex:0]];
}
else
{
//append a new word to the string
[truncated appendFormat:@" %@",[components objectAtIndex:x]];
}
}
else …Run Code Online (Sandbox Code Playgroud) 我正在为我的应用程序添加一个记事本功能,我想读取字符串的前十个字符,并使用该字符串作为表格视图中单元格的标题.
我想我可以用substringFromIndex:.
有人可以详细说明我该如何做到这一点?