将草图线高度转换为ios线'高度倍数'属性

时间:2017-06-16 08:58:34

标签: ios swift sketch-3

enter image description here

我的设计师发给我的草图文件说“线高:22'用于标签。我怎样才能在xcode界面构建器中实现这一点。 有没有办法使用代码或UI构建器定义此行高。

4 个答案:

答案 0 :(得分:3)

行高来自CSS,因此您的设计师必须拥有网页设计师背景。在移动平台上,我们不指定行高,而是line spacing

通常NSMutableParagraphStyle提供修改iOS多行标签的功能。

NSMutableParagraphStyle有一个名为maximumLineHeight的属性,但如果标签的包含超过某个值,则只会将最大行高设置为某个值。

要在IB中进行设置,您需要添加标签,并将Text属性更改为Attributed。然后单击段落样式图标,并设置标签的行间距。看看设计,它是大约2个线间距,你需要的。您可以要求设计师为您提供行间距属性,也可以尝试通过随机尝试不同的值来找到正确的行间距值。

enter image description here

答案 1 :(得分:2)

@bbjay确实使我走上了正轨。

如果要获得Sketch的准确结果,公式为:

paragraphStyle .lineSpacing = sketchLineHeight - font.lineHeight

提供了字体sketchFontSize

答案 2 :(得分:1)

在故事板中,使用UILabel的Atributed样式。以下是2.5行高的示例

enter image description here

答案 3 :(得分:1)

我发现以下公式很适合我。 它将表格草图行高转换为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
相关问题