相关疑难解决方法(0)

在字或边界处截断包含表情符号或unicode字符的字符串

问题

如何在不消除可能在我的长度中间发出的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)

unicode objective-c nsstring ios emoji

10
推荐指数
1
解决办法
4305
查看次数

获取字符串中的前十个字符

我正在为我的应用程序添加一个记事本功能,我想读取字符串的前十个字符,并使用该字符串作为表格视图中单元格的标题.

我想我可以用substringFromIndex:.

有人可以详细说明我该如何做到这一点?

string cocoa-touch nsstring

3
推荐指数
1
解决办法
2497
查看次数

标签 统计

nsstring ×2

cocoa-touch ×1

emoji ×1

ios ×1

objective-c ×1

string ×1

unicode ×1