๐Ÿ˜†์‹ฑ๊ธ€์ด๋ชจ์ง€ Extension ๋งŒ๋“ค๊ณ  ์ ์šฉํ•˜๊ธฐ

sangheeยท2021๋…„ 8์›” 6์ผ
0

๐ŸšฉiOS

๋ชฉ๋ก ๋ณด๊ธฐ
4/18

๐Ÿ˜†์‹ฑ๊ธ€์ด๋ชจ์ง€ ์ต์Šคํ…์…˜(Extension)

์ต์Šคํ…์…˜์€ ์Šค์œ„ํ”„ํŠธ์˜ ๊ฐ•๋ ฅํ•œ ๊ธฐ๋Šฅ ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ์ด ๊ธฐ๋Šฅ์„ ์ด์šฉํ•ด, ๋ฌธ์ž ํƒ€์ž…์— ๋ฌธ์ž๊ฐ€ ์‹ฑ๊ธ€ ์ด๋ชจ์ง€์ธ์ง€ Bool ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•œ๋‹ค. ๋‚˜๋Š” ํ”„๋กœ์ ํŠธ์˜ Utilitiesํด๋” ์•ˆ Extensions ์ฝ”๋“œ์— ์ถ”๊ฐ€ํ•˜์˜€๋‹ค.

extension String {
    var isSingleEmoji: Bool {
        if unicodeScalars.count > 2 {
            return false
        }
        
        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
                return true
            default:
                continue
            }
        }
        return false
    }
}

๋ฉ”์„ธ์ง€์— ์ ์šฉํ•˜๊ธฐ

๋ฉ”์„ธ์ง€๊ฐ€ ์‹ฑ๊ธ€์ด๋ชจ์ง€์ผ๋•Œ, ํฐํŠธ์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ํ‚ค์› ๋‹ค.

if messageText.isSingleEmoji {
    Text(messageText)
        .padding(12)
        .background(Color(.systemBlue).opacity(0.7))
        .font(.system(size: 21))
        .clipShape(MessageBubble(isFromCurrentUser: true))
        .foregroundColor(.white)
} else {
    Text(messageText)
        .padding(12)
        .background(Color(.systemBlue).opacity(0.7))
        .font(.system(size: 14))
        .clipShape(MessageBubble(isFromCurrentUser: true))
        .foregroundColor(.white)
}

๊ฒฐ๊ณผ๋ฌผ

๋ฉ”์„ธ์ง€๊ฐ€ ํ•œ๊ธ€์ž์ด๊ณ , ์ด๋ชจ์ง€์ผ๋•Œ ํฌ๊ฒŒ ์ž˜ ๋„์›Œ์ง„๋‹ค.

๊นƒํ—ˆ๋ธŒ ์ปค๋ฐ‹ ์ฃผ์†Œ

GitHub - Commit

profile
๐Ÿ‘ฉโ€๐Ÿ’ป

0๊ฐœ์˜ ๋Œ“๊ธ€