隨著 iOS 版本演進,系統對原生 emoji 的支援愈來愈完整。emoji 是 Unicode 中專為表情符號規劃的一段編碼;此外還存在另一套獨立的 SBUnicode 編碼,兩者在 Apple 系統中都有良好支援。UI 元件會自動把編碼轉義成表情圖示,例如:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; label.font = [UIFont systemFontOfSize:25]; label.text = @"\uE056"; [self.view addSubview:label];
執行後螢幕上就會出現一個笑臉。
為了跨平台(iOS、Android、Web)一致,需要一個共同標準,最合適的選擇就是 Unicode。做法是把表情文字編碼成 Unicode 後再傳輸,如此各平台顯示才不會各行其是。
具體實作有兩條路。第一條是自備一套表情切圖,與 Unicode 碼一一對應,轉碼時逐一替換——好處是所有平台的表情完全統一。第二條是 iOS 專用:既然 SBUnicode 碼排列規律鮮明,直接遍歷其範圍即可批量產生表情,省下大量圖片素材。
iOS 可用的表情 Unicode 範圍為:
0xE001~0xE05A, 0xE101~0xE15A, 0xE201~0xE253, 0xE401~0xE44C, 0xE501~0xE537
用一組區間陣列遍歷,就能把資料源填滿:
int emojiRangeArray[10] = {0xE001,0xE05A,0xE101,0xE15A,0xE201,0xE253,0xE401,0xE44C,0xE501,0xE537};
for (int j = 0; j < 10; j += 2) {
int startIndex = emojiRangeArray[j];
int endIndex = emojiRangeArray[j + 1];
for (int i = startIndex; i <= endIndex; i++) {
[dataArray addObject:[NSString stringWithFormat:@"%C", (unichar)i]];
}
}
版面配置用 UICollectionView 最省事,開啟分頁效果後,一行就能滑動換頁:
// 為了承載分頁控制器,先建一個背景 view
bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
// 分頁控制器
pageControlBottom = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 170, [UIScreen mainScreen].bounds.size.width, 20)];
[bgView addSubview:pageControlBottom];
// collectionView 版面
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; // 水平捲動
layout.itemSize = CGSizeMake(30, 30); // 每格 30x30
// 計算左右邊距,讓每頁 7 格置中
float xOffset = (kscreenWidth - 7 * 30 - 10 * 6) / 2;
layout.sectionInset = UIEdgeInsetsMake(10, xOffset, 10, xOffset);
layout.minimumLineSpacing = 10;
layout.minimumInteritemSpacing = 5;
scrollView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 160)
collectionViewLayout:layout];
scrollView.pagingEnabled = YES; // 開啟分頁
scrollView.delegate = self;
scrollView.dataSource = self;
scrollView.backgroundColor = bgView.backgroundColor;
[bgView addSubview:scrollView];
對應的 collectionView 回呼,重點是「每頁 28 格」這個切分規則:
// 每頁 28 個表情
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (((dataArray.count/28)+(dataArray.count%28==0?0:1)) != section+1) {
return 28;
}else{
return dataArray.count - 28*((dataArray.count/28)+(dataArray.count%28==0?0:1)-1);
}
}
// 頁數
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return (dataArray.count/28)+(dataArray.count%28==0?0:1);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"biaoqing" forIndexPath:indexPath];
for (int i = cell.contentView.subviews.count; i > 0; i--) {
[cell.contentView.subviews[i-1] removeFromSuperview];
}
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
label.font = [UIFont systemFontOfSize:25];
label.text = dataArray[indexPath.row + indexPath.section * 28];
[cell.contentView addSubview:label];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *str = dataArray[indexPath.section * 28 + indexPath.row];
// 在此手動把表情符號插入 textField
}
// 翻頁後更新分頁控制器
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat contenOffset = scrollView.contentOffset.x;
int page = contenOffset / scrollView.frame.size.width
+ ((int)contenOffset % (int)scrollView.frame.size.width == 0 ? 0 : 1);
pageControlBottom.currentPage = page;
}
UITextField 與 UITextView 都有這兩個成員:
@property (nullable, readwrite, strong) UIView *inputView; - (void)reloadInputViews;
inputView 決定成為第一回應者時要彈出的附加檢視;若不設定或設為 nil,就會彈出系統鍵盤。而 reloadInputViews 能刷新這個附加檢視。兩者搭配,鍵盤切換變得非常輕鬆:
-(void)imageViewTap{
if (![_publishContent isFirstResponder]) {
return;
}
if (isEmoji == NO) {
isEmoji = YES;
_textView.inputView = bgView; // 呼出表情鍵盤
[_textView reloadInputViews];
}else{
isEmoji = NO;
_textView.inputView = nil; // 回到系統鍵盤
[_textView reloadInputViews];
}
}
補充一個實測結果:上述 SBUnicode 碼在模擬器上能正常顯示,真機卻無法辨識。若要在真機上開發,建議把表情符號全數寫入 plist 檔,改以讀檔方式建立資料源。