2014-01-08 46 views
2

我是iOS開發新手,想知道如何添加一個類似於tweetbot 3的夜間主題並清除。從我的研究中,我還沒有真正能夠找到任何關於主題iOS應用程序的內容。爲我的應用程序創建一個夜晚主題

我會在另一個主題的故事板中重製應用程序嗎?

謝謝。

+0

查看下面的鏈接: http:// stackoverflow。com/questions/8919334/how-to-create-multiple-themes-skin-for-iphone-apps http://stackoverflow.com/questions/7939981/how-to-switch-skins-or-design -themes-in-ios-app – PunjabiCoder

回答

15

要增加別人的話,有一個關於主題代碼的WWDC視頻。我已經採取了更進一步的步驟,並創建了一個用於主題的輕量級框架,我使用了幾個應用程序。要點如下。

每次創建標籤,按鈕等(或者當它們將出現在屏幕上時,如果您使用界面構建器),則將它們傳遞給設置其外觀和感覺的Theme實例。如果幾個UI組件一起工作,請使用Facade設計模式將它們合併爲一個對象(例如,如果您有一個在特定位置具有客戶包裝器,標籤和圖像的按鈕,則將它們包裝到一個叫做的類 - 比如說 - WrappedButton)。

我有時會發現很容易在UML溝通,所以......

Theme UML Diagram

的主題協議可能是這個樣子。

@protocol Theme <NSObject> 

- (void)themeHeadingLabel:(UILabel *)headingLabel; 
- (void)themeBodyLabel:(UILabel *)bodyLabel; 

- (void)themeNavigationButton:(UIButton *)navigationButton; 
- (void)themeActionButton:(UIButton *)actionButton; 

@end 

順便提及,我通常把代碼在那裏,以允許按鈕,標籤等,對在iOS7到文本大小的改變作出響應(從設置應用程序)。因此,也有可能是這樣的方法,

- (void)respondToTextSizeChangeForHeadingLabel:(UILabel *)headingLabel; 
- (void)respondToTextSizeChangeForBodyLabel:(UILabel *)bodyLabel; 

// and do the same for buttons 

然後,當然,你就會有該協議的一個或多個實現。這是你的主題將會生活的地方。這裏有一些可能看起來像什麼的片段。

#import "Theme.h" 

@interface LightTheme : NSObject <Theme> 

@end 

@implementation LightTheme 

- (void)themeHeadingLabel:(UILabel *)headingLabel 
{ 
    headingLabel.backgroundColor = [UIColor lightTextColor]; 
    headingLabel.textColor = [UIColor darkTextColor]; 

    headingLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]; 
} 

// the rest of your theming 

@end 

而且你可以有一個黑暗的主題,其實現看起來是這樣的。

@implementation DarkTheme 

- (void)themeHeadingLabel:(UILabel *)headingLabel 
{ 
    headingLabel.backgroundColor = [UIColor darkGrayColor]; 
    headingLabel.textColor = [UIColor lightTextColor]; 

    headingLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]; 
} 

// the rest of your theming 

@end 

我一直包在一個ThemeManager幫我保持主題的軌道。這可以看起來像這樣。現在

#import "Theme.h" 

@interface ThemeManager : NSObject 

+ (id <Theme>)theme; 

@end 


#import "LightTheme.h" 
#import "DarkTheme.h" 

@implementation ThemeManager 

+ (id <Theme>)theme 
{ 
    // here you'd return either your light or dark theme, depending on your program's logic 
} 

@end 

,以將其結合在一起,你可以直接使用它或工廠。

UILabel* headingLabel = [[UILabel alloc] init]; 
headingLabel.text = @"My Heading"; 

[[ThemeManager theme] themeHeadingLabel:myHeading]; 

// use the label 

或者作爲工廠,實施將是這個樣子

- (UILabel *)buildLabelWith:(NSString *)text 
{ 
    UILabel* headingLabel = [[UILabel alloc] init]; 
    headingLabel.text = text; 

    [[ThemeManager theme] themeHeadingLabel:myHeading]; 

    return headingLabel; 
} 

希望有所幫助。如果您有任何問題,請告訴我。

+0

這是完美的!非常感謝! – user3173553

+0

很好的解釋,很好地使用模式。 –

相關問題