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

本文介绍了一种在iOS应用中处理表情符号的方法。通过使用`rangeOfComposedCharacterSequencesForRange`方法,可以有效地获取组合字符序列的正确范围,解决了在编辑标题时遇到表情符号导致的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值