Support style attributes for simple text formatter

This commit is contained in:
Sebastián Katzer 2019-01-23 16:42:27 +01:00
parent e1405e914b
commit 22e8d95128
3 changed files with 29 additions and 14 deletions

View File

@ -21,10 +21,8 @@
@interface APPPrinterLayout : NSObject @interface APPPrinterLayout : NSObject
+ (UIPrintFormatter *) configureFormatter:(UIPrintFormatter *)formatter withLayoutFromDictionary:(NSDictionary *)layout; + (UIPrintFormatter *) configureFormatter:(UIPrintFormatter *)formatter withLayoutFromDictionary:(nullable NSDictionary *)layoutSpec withStyleFromDictionary:(nullable NSDictionary *)styleSpec;
// The index of the page for where to apply the layout.
@property(nonatomic) NSInteger pageIndex;
// The margins for each printed page. // The margins for each printed page.
@property(nonatomic) UIEdgeInsets contentInsets; @property(nonatomic) UIEdgeInsets contentInsets;
// The maximum height of the content area. // The maximum height of the content area.

View File

@ -20,6 +20,7 @@
*/ */
#include "APPPrinterLayout.h" #include "APPPrinterLayout.h"
#include "APPPrinterStyle.h"
#include "APPPrinterUnit.h" #include "APPPrinterUnit.h"
@implementation APPPrinterLayout @implementation APPPrinterLayout
@ -27,10 +28,12 @@
#pragma mark - #pragma mark -
#pragma mark Init #pragma mark Init
- (id) initWithDictionary:(NSDictionary *)spec - (id) initWithDictionary:(nullable NSDictionary *)spec
{ {
self = [self init]; self = [self init];
if (!spec) return self;
NSDictionary* insests = spec[@"padding"]; NSDictionary* insests = spec[@"padding"];
double maxWidth = [spec[@"maxWidth"] doubleValue]; double maxWidth = [spec[@"maxWidth"] doubleValue];
double maxHeight = [spec[@"maxHeight"] doubleValue]; double maxHeight = [spec[@"maxHeight"] doubleValue];
@ -45,20 +48,26 @@
_maximumContentHeight = dots * maxHeight; _maximumContentHeight = dots * maxHeight;
_pageIndex = [spec[@"page"] integerValue];
return self; return self;
} }
#pragma mark - #pragma mark -
#pragma mark Public #pragma mark Public
+ (UIPrintFormatter *) configureFormatter:(UIPrintFormatter *)formatter withLayoutFromDictionary:(NSDictionary *)spec + (UIPrintFormatter *) configureFormatter:(UIPrintFormatter *)formatter
withLayoutFromDictionary:(NSDictionary *)layoutSpec
withStyleFromDictionary:(nullable NSDictionary *)styleSpec
{ {
id layout = [[self alloc] initWithDictionary:spec]; id layout = [[self alloc] initWithDictionary:layoutSpec];
[layout configureFormatter:formatter]; [layout configureFormatter:formatter];
if (styleSpec && ![formatter isKindOfClass:UIMarkupTextPrintFormatter.class])
{
[layout configureTextFormatter:(UISimpleTextPrintFormatter *)formatter
withStyleFromDictionary:styleSpec];
}
return formatter; return formatter;
} }
@ -77,4 +86,15 @@
formatter.perPageContentInsets = _contentInsets; formatter.perPageContentInsets = _contentInsets;
} }
- (void) configureTextFormatter:(UISimpleTextPrintFormatter *)formatter
withStyleFromDictionary:(NSDictionary *)spec
{
APPPrinterStyle *style = [[APPPrinterStyle alloc]
initWithDictionary:spec];
formatter.font = style.font;
formatter.color = style.color;
formatter.textAlignment = style.textAlignment;
}
@end @end

View File

@ -37,18 +37,15 @@
- (instancetype) initWithDictionary:(NSDictionary *)spec formatter:(UIPrintFormatter *)formatter - (instancetype) initWithDictionary:(NSDictionary *)spec formatter:(UIPrintFormatter *)formatter
{ {
NSDictionary* layout = spec[@"layout"];
NSDictionary* header = spec[@"header"]; NSDictionary* header = spec[@"header"];
NSDictionary* footer = spec[@"footer"]; NSDictionary* footer = spec[@"footer"];
double dots = 0; double dots = 0;
self = [self init]; self = [self init];
if (layout) [APPPrinterLayout configureFormatter:formatter
{ withLayoutFromDictionary:spec[@"layout"]
[APPPrinterLayout configureFormatter:formatter withStyleFromDictionary:spec[@"style"]];
withLayoutFromDictionary:layout];
}
[self addPrintFormatter:formatter startingAtPageAtIndex:0]; [self addPrintFormatter:formatter startingAtPageAtIndex:0];