简体   繁体   English

如何根据WatchKit中的手表大小缩放字体大小?

[英]How can I scale font size based on watch size in WatchKit?

I have an Apple Watch app that I would like to scale the font size for depending on the screen size. 我有一个Apple Watch应用,我想根据屏幕尺寸缩放字体大小。 Currently, the only way I found to scale the font size is based on setting the device variation for each size in the storyboard. 目前,我发现缩放字体大小的唯一方法是基于为故事板上的每种大小设置设备变化。 However, I was wondering if there was a way around this. 但是,我想知道是否有解决办法。 I'm hoping for a way to scale the font size based the on screen size automatically. 我希望有一种方法可以自动根据屏幕大小缩放字体大小。 Any tips or suggestions are appreciated. 任何提示或建议,不胜感激。

You should multiply the font size with the current width of the screen and divide the product with a reference screen size of your choice (in this example, it's the 38mm size, which has the width of 136 logical pixels). 您应该将字体大小乘以当前屏幕宽度,然后将乘积除以您选择的参考屏幕大小(在本示例中为38mm大小,其宽度为136逻辑像素)。

let label = WKInterfaceLabel()

let fontSize: CGFloat = 12
let scaledFontSize = fontSize * WKInterfaceDevice.current.screenBounds.width / 136

let font = UIFont.systemFont(ofSize: scaledFontSize)
let attrString = NSAttributedString(string: "Example Text", attributes: [.font: font])
label.setAttributedText(attrString)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM