PeintureKit
一个 DSL 绘图工具包。
示例
DSL
// Use keyword "let" to declare a custom view
let Custom = Composite {
Text('Hello') {
id = 1
color = '#00FFFFFF'
textSize = 120
textColor = '#333333'
Constraint {
centerX = 'parent'
top = 60
}
}
Image {
id = 2
contentMode = 'scaleAspectFit'
src = 'https://w.wallhaven.cc/full/6k/wallhaven-6k3oox.jpg'
Constraint {
width = 'parent'
heightToWidth = (2, 0, 0.5625)
topToBottom = (1, 60)
}
}
}
/*
* There can only be one top-level declaration
*/
Custom {
color = '#F6F6F6'
Constraint {
width = 800
bottomToBottom = 2
}
}
用法
let drawer = Drawer()
let image = try drawer.drawImage(vl: dsl)
您还可以使用以下方式获取视图:
let drawer = Drawer()
let view = try drawer.drawView(vl: dsl)
默认情况下,图像加载器是“defaultImageLoader”,它阻塞并无缓存。一个阻塞图像加载器是输出图像所必需的,但不是输出要显示的视图所必需的。您也可以自定义图像加载器。
结果
小部件
通用
id // integer
color // background color, ex: '#FFFFFF'
constraint // declaration
transform // declaration
contentMode // 'scaleToFill'
'scaleAspectFit'
'scaleAspectFill'
'redraw'
'center'
'top'
'bottom'
'left'
'right'
'topLeft'
'topRight'
'bottomLeft'
'bottomRight'
alpha // float in [0, 1], 0 is transparent
shape // 'rectangle' or 'oval'
borderColor // ex: '#333333'
borderWidth // float
cornerRadii // ex: (100, 100)
cornerRadius // float
corners // subset of ['topLeft', 'topRight', 'bottomRight', 'bottomLeft']
gradient // declaration
声明
约束
The standard constraint:
<attr>To<attr> =|<=|>= (<id>, <constant>, <multiplier>)
ex:
topToBottom = (1, 100, 1)
could be omitted if constant is 0, multiplier is 1:
topToTop = 1 <=> topToTop = (1, 0, 1)
leftToRight = (1, 100) <=> leftToRight = (1, 100, 1)
The "to" attribute could be omitted, for dimemsion(width or height):
width = 'parent' <=> widthToWidth = ('parent', 0, 1)
width = 100 <=> widthToUnspecific = (-1, 100, 1)
for others:
top = 'parent' <=> topToTop = ('parent', 0, 1)
top = 100 <=> topToTop = ('parent', 100, 1)
Attrbutes:
unspecific,
width, height,
left, right, top, bottom, leading, trailing,
firstBaseline, lastBaseline, centerX, centerY
转换
pivot // ex: (0.5, 0.5)
translation // (x, y)
scale // (x, y)
rotation // angle
渐变
colors // array, at least 2 items
type // 'axial'
'radial'
orientation // ex: [(0, 0), (1, 0)]
文本
text // the main argument
textSize // float
textColor // ex: '#333333'
textStyle // 'bold'
'italic'
'normal'
textWeight // 'thin'
'regular'
'black'
'bold'
'heavy'
'light'
'medium'
'semibold'
'ultraLight'
deleteLine // boolean
underLine // boolean
图像
src // image url, also as the main argument
视图,空白
无特定参数。