這個(gè)起因是因?yàn)樽罱趯懶马?xiàng)目,關(guān)于字體的通用代碼沒(méi)有導(dǎo)進(jìn)來(lái),然后UI同學(xué)在檢查頁(yè)面的時(shí)候發(fā)現(xiàn)在plus上字體小了,然后進(jìn)行了一系列的深究
- 先說(shuō)一下以前的解決方案, 判斷如果是 plus機(jī)型那就把字體大小+1(或者+2, 這個(gè)不用死扣 反正也就視覺(jué)上差不多,但是沒(méi)有理論根據(jù))
- 網(wǎng)上的文章千千萬(wàn),質(zhì)量參差不齊 請(qǐng)閱讀并實(shí)踐之后再得出結(jié)論,不要上來(lái)就 要demo 拿了代碼就直接往項(xiàng)目里面扔,有時(shí)候會(huì)死的很慘(下面會(huì)說(shuō))
- 解決方案很簡(jiǎn)單, 核心在于 UI同學(xué)給你的設(shè)計(jì)稿 是PX單位 還是PT單位
首先來(lái)看一下iOS的UIFont 到底是怎么回事
class func systemFont(ofSize fontSize: CGFloat) -> UIFont
fontSize這個(gè)參數(shù)需要的數(shù)值單位 是 points
但是! 蘋果的這個(gè) points 和 百度到的 pt(磅)這個(gè)單位 雖然叫一個(gè)名字,但不是一回事
Points Versus Pixels
In iOS there is a distinction between the coordinates you specify in your drawing code and the pixels of the underlying device. When using native drawing technologies such as Quartz, UIKit, and Core Animation, the drawing coordinate space and the view’s coordinate space are both logical coordinate spaces, with distances measured in points. These logical coordinate systems are decoupled from the device coordinate space used by the system frameworks to manage the pixels onscreen.
The system automatically maps points in the view’s coordinate space to pixels in the device coordinate space, but this mapping is not always one-to-one. This behavior leads to an important fact that you should always remember:
One point does not necessarily correspond to one physical pixel.
The purpose of using points (and the logical coordinate system) is to provide a consistent size of output that is device independent. For most purposes, the actual size of a point is irrelevant. The goal of points is to provide a relatively consistent scale that you can use in your code to specify the size and position of views and rendered content. How points are actually mapped to pixels is a detail that is handled by the system frameworks. For example, on a device with a high-resolution screen, a line that is one point wide may actually result in a line that is two physical pixels wide. The result is that if you draw the same content on two similar devices, with only one of them having a high-resolution screen, the content appears to be about the same size on both devices.
Note: In the context of PDF rendering and printing, Core Graphics defines "point" using the industry standard mapping of one point to 1/72 of an inch.
如果沒(méi)看懂啥意思, 給你轉(zhuǎn)換成一句簡(jiǎn)單的 如果是 一個(gè) 750 * 300 px的圖 你只需要知道 代碼中的尺寸是是 375*150 就好,
至于你百度搜索到的 關(guān)于pt和px的轉(zhuǎn)換相關(guān)的文章 什么 72/96 或者是 1/72 這類的 文章表述的都沒(méi)錯(cuò), 但是請(qǐng)記住 這些計(jì)算方式不適合iOS!!!
如果你去搜索 iOS字體適配相關(guān)的文章,可能映入你眼簾的 會(huì)是什么 啊 @3x的屏幕 縮放比是3, @2x屏幕的縮放比是 2, 所以 在@3x的屏幕上 你的文字 就要 用 fontSize * 1.5 就可以啦~
比如 上圖是設(shè)計(jì)稿,你可以這樣試試 將字體大小乘個(gè)1.5, 我保證 你們親愛(ài)的UI同學(xué) 分分鐘能罵你祖宗十八輩的
同學(xué),老年機(jī)啦有沒(méi)有, 難道都是 云開(kāi)發(fā)? 既然寫成文章就要負(fù)點(diǎn)責(zé)任好不啦
其實(shí),你是被繞進(jìn)去了, 想一下 全國(guó) 叫 小明的人有的是, 不是所有的小明都是你認(rèn)識(shí)的那個(gè)小明的,
所以 不要拿pt(磅)轉(zhuǎn)px的公式帶入到 iOS 字體的大小設(shè)置中
一般設(shè)計(jì)同學(xué)給的設(shè)計(jì)稿都是基于4.7寸的屏幕(即 750 x 1334), 所以只要記住一點(diǎn) 2px = 1points 就好了
那么其實(shí)適配 puls機(jī)型的字體很簡(jiǎn)單,想一下UI上是怎么做適配的?
// UI基于 4.7寸型號(hào)屏幕做的設(shè)計(jì)稿
let KAdaptedHeight = UIScreen.main.bounds.size.width / 375.0
UI適配 都是這樣做出來(lái)的吧?
那么 字體也一樣
UIFont.systemFont(ofSize: fontSize * KAdaptedHeight)
這樣就可以了,保準(zhǔn)你們可愛(ài)的UI同學(xué) 不會(huì)再來(lái)找你們字體相關(guān)的麻煩