美文网首页程序员专题_iOS开发相关
【iOS】iOS OC截取字符串时emoji表情处理 取emoj

【iOS】iOS OC截取字符串时emoji表情处理 取emoj

作者: x_code | 来源:发表于2018-03-27 11:55 被阅读454次

做了一个类似于浏览器标签的功能,取出主色调,选取标题第一个字,这里标题是可以编辑的,遇到表情符号就出现了以下状况:


image.png

很尴尬对不对。

Stack Overflow了一下,代码如下:

    nsssting *string = @"😯😯😯😯😯";
    NSRange range = [string   rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 1)];
    NSString * result = [string substringWithRange:range];

这里用到了rangeOfComposedCharacterSequencesForRange方法,把解释翻译一下:

Returns the range in the string of the composed character sequences for a given range.
This method provides a convenient way to grow a range to include all composed character sequences it overlaps.
返回给定范围内组合字符序列的字符串范围。此方法提供了一种方便的方法来扩展,使其包含所有与其重叠的字符序列。
@parameters  range
             A range in the receiver. The range must not exceed the bounds of the receiver.
接收机中的一个量程。范围不得超过接收机的范围。
@parameters  return
             The range in the receiver that includes the composed character sequences in range.
接收机中包括组合字符序列的范围

效果如下:


image.png

相关文章

网友评论

    本文标题:【iOS】iOS OC截取字符串时emoji表情处理 取emoj

    本文链接:https://www.haomeiwen.com/subject/rrfbqftx.html