「幫我畫一條 1 像素的分割線」——這個聽起來再簡單不過的需求,實作起來卻是無數設計稿驗收時的爭論焦點。原因在於 iOS 的座標系統與實體螢幕之間,隔著一層縮放轉換。
使用 Quartz、UIKit、Core Animation 等框架時,所有座標都以 Point 為單位,系統在實際渲染時會自動完成 Point 到 Pixel 的轉換。這樣做的好處是隔離硬體差異:排版時不必關心裝置是不是 Retina 螢幕,一套座標就能通行。
但有一點必須牢記:
One point does not necessarily correspond to one physical pixel.
1 Point 的線在非 Retina 螢幕上是一個像素,在 Retina 螢幕上則可能是 2 個或 3 個,取決於裝置的 DPI。UIScreen、UIView、UIImage、CALayer 都提供了取得 scale factor 的屬性。
原生的繪製技術其實已經幫我們處理了 scale factor。例如在 drawRect: 中,UIKit 會依目前裝置自動設定正確的 scale factor,所以你畫的內容會被自動縮放到實體螢幕。多數情況下我們完全不需要操心 pixel——但有少數例外,畫 1 像素線就是最典型的一個。
看到這個問題,第一直覺大概是:用當前螢幕的縮放因子算出 1 像素對應的 Point,再把線寬設上去就好。
1.0f / [UIScreen mainScreen].scale
表面上看一切正常,但實際在裝置上跑,你會發現線條是模糊的、看起來並不是銳利的一像素。
原因是一個叫 antialiasing(反鋸齒)的技術。螢幕由許多微小的顯示單元組成,可以粗略理解為一個單元代表一個像素。如果線剛好落在某一整列顯示單元之內,就會渲染出標準的一像素黑線;但如果線落在兩列中間,系統為了讓視覺上平滑,會對線兩側的像素做淡化填充,結果變成一條兩像素寬的灰線。
Apple 官方文件的說法:
Positions defined by whole-numbered points fall at the midpoint between pixels. For example, if you draw a one-pixel-wide vertical line from (1.0, 1.0) to (1.0, 10.0), you get a fuzzy grey line. If you draw a two-pixel-wide line, you get a solid black line because it fully covers two pixels. As a rule, lines that are an odd number of physical pixels wide appear softer than lines with widths measured in even numbers of physical pixels unless you adjust their position to make them cover pixels fully.
簡單說:奇數像素寬度的線,渲染時會表現為柔和並向外擴展的線條,除非你手動調整位置讓它剛好落在整列的顯示單元上。
On a low-resolution display (with a scale factor of 1.0), a one-point-wide line is one pixel wide. To avoid antialiasing when you draw a one-point-wide horizontal or vertical line, if the line is an odd number of pixels in width, you must offset the position by 0.5 points to either side of a whole-numbered position. If the line is an even number of points in width, you must not do so. On a high-resolution display (with a scale factor of 2.0), a line that is one point wide is not antialiased at all because it occupies two full pixels (from -0.5 to +0.5). To draw a line that covers only a single physical pixel, you would need to make it 0.5 points in thickness and offset its position by 0.25 points.
翻譯成實務結論:
如果非 Retina 偏移 0.5 Point、Retina 偏移 0.25 Point,照這個規律,3 倍螢幕豈不是要偏移 0.166…?與其硬記數字,不如理解原則。
回頭看上圖:每一格代表一個像素,頂部標記的是我們排版時的座標。以左邊的非 Retina 螢幕為例,要在 (3, 0) 畫一條一像素寬的豎線時,由於渲染的最小單位是像素,而 (3, 0) 恰好落在兩像素交界,系統會同時對左右兩列做淡化填充,線看起來就變粗變淡。因此要畫出一條銳利的線,得把座標移到 (2.5, 0) 或 (3.5, 0),讓系統剛好填滿一整列。
換句話說,偏移量永遠是「半個實體像素」,只是換算成 Point 時會依 scale 不同而改變。所以 6 Plus 一樣偏移半個像素,對應的 Point 值就是:
(1.0f / [UIScreen mainScreen].scale) / 2;
收錄成兩個宏,之後到處都能用:
#define SINGLE_LINE_WIDTH (1.0f / [UIScreen mainScreen].scale) #define SINGLE_LINE_ADJUST_OFFSET ((1.0f / [UIScreen mainScreen].scale) / 2)
使用方式:
CGFloat xPos = 5;
UIView *view = [[UIView alloc] initWithFrame:
CGRectMake(xPos - SINGLE_LINE_ADJUST_OFFSET, 0, SINGLE_LINE_WIDTH, 100)];
把上面的原則套進自繪的格線視圖,做法是暴露線寬與顏色屬性,並在繪製時對奇數像素做偏移:
// SvGridView.h #import <UIKit/UIKit.h> @interface SvGridView : UIView /** 網格間距,預設 30 */ @property (nonatomic, assign) CGFloat gridSpacing; /** 網格線寬,預設 1 pixel (1.0f / [UIScreen mainScreen].scale) */ @property (nonatomic, assign) CGFloat gridLineWidth; /** 網格顏色,預設藍色 */ @property (nonatomic, strong) UIColor *gridColor; @end
實作時在 drawRect: 中依 gridSpacing 逐一畫線,並套用 SINGLE_LINE_ADJUST_OFFSET 做座標校正,就能保證任何裝置上都得到銳利的線條。使用方式同樣簡單:
SvGridView *gridView = [[SvGridView alloc] initWithFrame:self.view.bounds]; gridView.gridSpacing = 30.0f; gridView.gridLineWidth = SINGLE_LINE_WIDTH; [self.view addSubview:gridView];
說了這麼多技術細節,值得回頭想一個問題:設計師為什麼一定要 1 像素的線?
1 像素的線在非 Retina 裝置上粗細看起來剛好,但在 Retina 螢幕上會顯得特別纖細,甚至在某些背景下幾乎看不見。與其爭論該不該嚴格遵循 0.5 Point,不如把「視覺上一致的粗細」當成目標——這通常意味著在 Retina 上使用 1 Point(實體 2 像素)會比 0.5 Point 更協調。技術上要畫得出 1 像素,設計上要不要用,是兩件不同的事。
參考文件:Apple《Drawing and Printing Guide for iOS》— Graphics Drawing Overview。