1. 程式人生 > >【iOS】iOS OC擷取字串時emoji表情處理 取emoji表情的range

【iOS】iOS OC擷取字串時emoji表情處理 取emoji表情的range

做了一個類似於瀏覽器標籤的功能,取出主色調,選取標題第一個字,這裡標題是可以編輯的,遇到表情符號就出現了以下狀況:
image.png

很尷尬對不對。

Stack Overflow了一下,程式碼如下:

    nsssting *string = @"����������";
    NSRange range = [string   rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 1)];
    NSString * result = [string substringWithRange:range];
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