您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

用字典設定 UITabBarItem 字體與顏色

編輯:iOS 相關資訊

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 呈現更一致的外觀。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved