Check if there is an emoji contained in a string

后端 未结 11 1214
野的像风
野的像风 2020-12-31 11:49

I am getting the text size of a string with this

textSize = [[tempDict valueForKeyPath:@\"caption.text\"] sizeWithFont:[UIFont systemFontOfSize:12] constrain         


        
11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-31 12:11

    Now with the new release of iOS 10, for the following emojis it doesn't work:

    Emojis

    The following code snippet is tried and tested for both iOS 9 and iOS 10:

    extension String {
    
        var containsEmoji: Bool {
            for scalar in unicodeScalars {
                switch scalar.value {
                case 0x1F600...0x1F64F, // Emoticons
                0x1F300...0x1F5FF, // Misc Symbols and Pictographs
                0x1F680...0x1F6FF, // Transport and Map
                0x2600...0x26FF,   // Misc symbols
                0x2700...0x27BF,   // Dingbats
                0xFE00...0xFE0F,   // Variation Selectors
                0x1F910...0x1F918, // New Emoticons
                0x1F1E6...0x1F1FF, // Flags
                0x1F980...0x1F984,
                0x1F191...0x1F19A,
                0x1F201...0x1F202,
                0x1F232...0x1F23A,
                0x1F250...0x1F251,
                0x23E9...0x23F3,
                0x23F8...0x23FA,
                0x1F170...0x1F171,
                0x1F17E,
                0xA9,
                0xAE,
                0x2122,
                0x2328,
                0x3030,
                0x1F0CF,
                0x1F18E,
                0x1F9C0:
                    return true
                default:
                    continue
                }
            }
            return false
        }
    }
    

    Create a String extension in your app as mentioned above.

    And can be used like this:

    if string.containsEmoji {
        // Do operations here
    }
    

提交回复
热议问题