以前一直以為,要顯示各國國旗,除了讓UI給圖片,沒有其他辦法,
最近發現了手機中自帶國旗,在系統表情鍵盤中就有各國國旗,如果對UI要求不是很高的話,可以直接使用國旗emoji
上代碼
- (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,
[self emojiFlagForISOCountryCode:@"CN"];
這里因為沒有臺灣的國旗,所以做了下判斷,如果是臺灣就和中國一樣顯示

參考鏈接:
https://en.wikipedia.org/wiki/ISO_3166-1#Notes
https://stackoverflow.com/questions/30402435/swift-turn-a-country-code-into-a-emoji-flag-via-unicode
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/172690.html
標籤:iOS
