Muk*_*ami 6 ios swift sketch-3
我的设计师给我发送了草图文件,标签上写着“线高:22”。我如何在xcode接口构建器中实现此目的。有什么方法可以使用代码或UI构建器来定义此线高。
@bbjay 确实让我走上了正轨。
如果要获得Sketch的准确结果,公式为:
paragraphStyle .lineSpacing = sketchLineHeight - font.lineHeight
Run Code Online (Sandbox Code Playgroud)
只要给出了字体 sketchFontSize
我发现以下公式对我很有效。它将表单 Sketch 线高转换为 iOS 线间距:
lineSpacing = sketchLineHeight - sketchFontSize - (font.lineHeight - font.pointSize)
在代码中,对于您的情况,这将是:
let font = UIFont.systemFont(ofSize: 18) // or whatever font you use
textLabel.font = font
let attributedString = NSMutableAttributedString(string: "your text")
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 22 - 18 - (font.lineHeight - font.pointSize)
attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attributedString.length))
textLabel.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud)
行高来自 CSS,因此您的设计师必须具有网页设计师背景。在移动平台上,我们不指定行高,而是指定行间距。
一般来说,NSMutableParagraphStyle提供修改 iOS 多行标签的功能。
NSMutableParagraphStyle有一个名为maximumLineHeight的属性,但这只会将最大行高设置为某个值,如果标签的包含超过某个值。
要在 IB 中进行设置,您需要添加标签,并将Text属性更改为Attributed。然后单击段落样式图标,并设置标签的行距。看看设计,它是大约 2 点的行距,你需要什么。您可以要求您的设计师为您提供行距属性,也可以通过随机尝试不同的值来尝试找到正确的行距值。
| 归档时间: |
|
| 查看次数: |
3392 次 |
| 最近记录: |