您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發基礎

換個姿勢寫 UI 樣式:用 StyleSheet 讓樣式可複用、可跨類別共享

編輯:iOS 開發基礎

組件 GitHub 地址見文末。

據說終端開發者會有七成以上時間在和 UI 打交道。仔細想想確實如此:調整樣式、addSubView、處理 layout,不知不覺就佔掉大半工時。某天回頭一看,程式碼裡滿是這種東西:

self.label.layer.cornerRadius = 3;
self.label.textColor = [UIColor darkTextColor];
self.label.font = [UIFont systemFontOfSize:13];
self.label.backgroundColor = [UIColor greenColor];
self.label.layer.borderWidth = 2;
self.label.layer.borderColor = [UIColor redColor].CGColor;

self.label2.layer.cornerRadius = 3;
self.label2.textColor = [UIColor darkTextColor];
// ...幾乎一模一樣的一整串

self.button.layer.cornerRadius = 3;
self.button.backgroundColor = [UIColor greenColor];
// ...

self.aView.layer.cornerRadius = 3;
// ...

大量近乎相同的程式碼散落在整個 App 裡。為了實現一個樣式要設定一堆屬性,而很多畫面的樣式其實完全一致,於是同樣的工作被反覆執行。時間就這樣流走了。

身為一個懶人,自然會問:有沒有一種少寫點程式碼的方式?寫子類可以,但會有類別汙染的問題,只為一個共用樣式就開一個子類,成本不太划算。寫一批樣式渲染函式?聽起來不錯,但仔細一想工作量也不小,而且不夠通用。於是作者花了幾天時間寫出 StyleSheet 這個庫,目標就是簡化 UI 樣式的撰寫。

原始寫法的問題有兩個:一是程式碼繁瑣、大量重複;二是樣式無法共享,每個 View 都得重新賦值一次。而 StyleSheet 的設計目標正好對應:

  • 用更少的程式碼描述 View 樣式
  • 讓樣式能在 View 之間共享——不只同類別的實例之間,甚至跨類別共享

改用 StyleSheet 之後,上面那段程式碼變成:

self.label.style = DZLabelStyleMake(
    style.backgroundColor = [UIColor greenColor];
    style.cornerRedius = 3;
    style.borderColor = [UIColor redColor];
    style.borderWidth = 2;
    style.textStyle.textColor = [UIColor darkTextColor];
    style.textStyle.font = [UIFont systemFontOfSize:13];
);

self.label2.style = self.label.style;
self.aView.style = self.label.style;
[self.button.style copyAttributesWithStyle:self.label.style];

設計思路

設計时刻意淡化了「被渲染的 View 是什麼型別」這個概念:任何型別的 Style 都能對任何 View 進行渲染,前提是該 View 支援 Style 所指稱的屬性。例如用為 Button 設計的 DZButtonStateStyle 去渲染一個 UILabel,由於 UILabel 不支援其中的屬性,結果自然無效;但用 DZButtonStyle(繼承自 DZViewStyle)渲染 UILabel 時,它會沿著繼承鏈取用父類的屬性,正好對應 UIView 所支援的那些設定。

使用方式

直接渲染

DZLabelStyle *style = DZLabelStyleMake(
    style.backgroundColor = [UIColor greenColor];
    style.cornerRedius = 3;
    style.borderColor = [UIColor redColor];
    style.borderWidth = 2;
    style.textStyle.textColor = [UIColor darkTextColor];
    style.textStyle.font = [UIFont systemFontOfSize:13];
);
[style decorateView:self.label];

直接渲染的好處是不必再產生額外物件,同一份 style 套用到多個 View 時特別方便。

賦值渲染

庫中為 UIKit 常用元件擴充了 style 屬性,直接賦值即會觸發一次渲染;第一次讀取時會自動產生一個 zeroStyle 作為預設。

self.label.style = style;

// 或者直接賦值
self.label.style = DZLabelStyleMake(
    style.backgroundColor = [UIColor greenColor];
    style.cornerRedius = 3;
    style.borderColor = [UIColor redColor];
    style.borderWidth = 2;
    style.textStyle.textColor = [UIColor darkTextColor];
    style.textStyle.font = [UIFont systemFontOfSize:13];
);

賦值時會把 Style 複製一份後與 View 綁定,因此後續修改 Style 屬性,對應 View 的樣式會立刻反映。

共享通用樣式

用原本的寫法做樣式共享非常困難,基本上全靠人力維護,程式碼裡摻雜大量獨立而分散的樣式設定。改用 StyleSheet 之後:

先在標頭檔宣告:

// xxx.h
EXTERN_SHARE_LABEL_STYLE(Content)

再於實作檔定義:

// xxx.m
IMP_SHARE_LABEL_STYLE(Content,
    style.backgroundColor = [UIColor clearColor];
    style.cornerRedius = 2;
    style.textStyle.textColor = [UIColor redColor];
)

使用方式一:

self.label.style = DZStyleContent();

使用方式二(推薦):若不需要進一步調整樣式,可以直接渲染而不必複製:

[DZStyleContent() decorateView:self.label];

接下來,可以試著換這種方式來寫 UI 樣式了。

組件 GitHub 地址見原文。

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