UITabBarItem 除了可以設定標題與圖片,也能透過文字屬性字典來自訂字體大小與顏色。這在需要讓 TabBar 標題符合品牌視覺時很實用。
常見做法是使用 setTitleTextAttributes:forState:,並在字典中放入 NSFontAttributeName 與 NSForegroundColorAttributeName。
NSDictionary *attributes = @{
NSFontAttributeName: [UIFont systemFontOfSize:18],
NSForegroundColorAttributeName: [UIColor yellowColor]
};
[item5 setTitleTextAttributes:attributes forState:UIControlStateHighlighted];若希望 TabBar 圖片保持原始顏色,不被系統自動渲染成模板色,可以在建立 UIImage 時使用 imageWithRenderingMode:,並指定 UIImageRenderingModeAlwaysOriginal。
這樣一來,就能同時控制文字樣式與圖片顯示效果,讓 TabBar 呈現更一致的外觀。