之前一直觉得,要显示各国国旗,除了让UI给图片,没有其余办法。swift
最近发现了手机中自带国旗,在系统表情键盘中就有各国国旗,若是对UI要求不是很高的话,能够直接使用国旗emojispa
上代码code
- (NSString *)emojiFlagForISOCountryCode:(NSString *)countryCode { NSString *code = countryCode; if ([countryCode isEqualToString:@"TW"]) { code = @"CN"; } NSAssert(code.length == 2, @"Expecting ISO country code"); int base = 127462 -65; wchar_t bytes[2] = { base +[code characterAtIndex:0], base +[code characterAtIndex:1] }; return [[NSString alloc] initWithBytes:bytes length:code.length *sizeof(wchar_t) encoding:NSUTF32LittleEndianStringEncoding]; }
countryCode传国家代码,若是中国CN,blog
[self emojiFlagForISOCountryCode:@"CN"];图片
这里由于没有台湾的国旗,因此作了下判断,若是是台湾就和中国同样显示
ip
参考连接:unicode
https://en.wikipedia.org/wiki/ISO_3166-1#Notesit
https://stackoverflow.com/questions/30402435/swift-turn-a-country-code-into-a-emoji-flag-via-unicodeio