Prefix style attributes in header/footer with style and support relative positions

This commit is contained in:
Sebastián Katzer 2019-01-24 11:46:20 +01:00
parent 22e8d95128
commit ea223c3609
2 changed files with 81 additions and 8 deletions

View File

@ -126,10 +126,64 @@
if ([self strIsNullOrEmpty:label])
return;
APPPrinterStyle *style = [[APPPrinterStyle alloc]
initWithDictionary:spec];
NSDictionary *position = spec[@"position"];
NSDictionary *style = spec[@"style"];
[label drawInRect:rect withAttributes:style.attributes];
NSDictionary *attributes = [[APPPrinterStyle alloc]
initWithDictionary:style].attributes;
if (position)
{
[label drawAtPoint:[self pointFromPositionAsDictionary:position
forLabel:label
withAttributes:attributes
inRect:rect]
withAttributes:attributes];
}
else
{
[label drawInRect:rect withAttributes:attributes];
}
}
- (CGPoint) pointFromPositionAsDictionary:(NSDictionary *)spec
forLabel:(NSString *)label
withAttributes:(NSDictionary *)attributes
inRect:(CGRect)rect
{
double dots = [APPPrinterUnit convert:spec[@"unit"]];
id top = spec[@"top"];
id left = spec[@"left"];
id right = spec[@"right"];
id bottom = spec[@"bottom"];
double x = rect.origin.x, y = rect.origin.y;
CGSize size;
if (bottom || right)
{
size = [label sizeWithAttributes:attributes];
}
if (top)
{
y = rect.origin.y + dots * [top doubleValue];
}
else if (bottom)
{
y = rect.origin.y - size.height - dots * [bottom doubleValue];
}
if (left)
{
x = rect.origin.x + dots * [left doubleValue];
}
else if (right)
{
x = rect.size.width - size.width - dots * [right doubleValue];
}
return CGPointMake(x, y);
}
- (BOOL) strIsNullOrEmpty:(NSString *)string

View File

@ -61,14 +61,33 @@ exports.getDefaults = function () {
labels: [{
text: 'Awesome Printer Plug-in',
style: {
align: 'center',
italic: true,
color: '#FF0000'
}
},{
showPageIndex: true,
style: {
align: 'right',
bold: true
}
}]
},
footer: {
unit: 'mm',
height: 3,
label: {
text: 'Copyright (c) 2013-2019 Sebastián Katzer',
style: { size: 9 },
position: {
unit: 'mm',
top: 1.5,
right: 5
}
}
}
};
};