Tip:為什麼設置文字的某個狀態時需要將其內容封裝為NSDictionary?這是因為字體所關聯的一系列屬性,如字體風格 字體顏色等。
設置圖片等多狀態的代碼風格,以下是一種,常見的其他控件設置多狀態一般也是以方法名set開頭(以Statexxx結尾),假如只有一種狀態,那麼直接使用 點( . )語法設置即可。
#pragma 處理字體點擊效果 - (void)dealWithItemFont:(UITabBarItem *)messageItem { NSMutableDictionary *dic=[NSMutableDictionary dictionary]; [dic setValue:[UIColor grayColor] forKey:NSForegroundColorAttributeName]; [messageItem setTitleTextAttributes:dic forState:UIControlStateNormal ]; NSMutableDictionary *hightlineDic=[NSMutableDictionary dictionary]; [hightlineDic setValue:[UIColor orangeColor] forKey:NSForegroundColorAttributeName]; [messageItem setTitleTextAttributes:hightlineDic forState:UIControlStateHighlighted]; } #pragma 處理圖片點擊效果 - (UITabBarItem *)dealWithItemImage:(NSString *)normalImageStr selectedImageStr:(NSString *)selectedImageStr { UIImage *normalImage=[UIImage imageNamed:normalImageStr]; UIImage *selectedImage=[UIImage imageNamed:selectedImageStr]; selectedImage=[selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; UITabBarItem *messageItem=[[UITabBarItem alloc] init]; [messageItem setImage:normalImage]; [messageItem setSelectedImage:selectedImage]; return messageItem; }