Cleanup APPPrinterRenderer by introducing APPPrinterStyle
This commit is contained in:
parent
69541fb13e
commit
e1405e914b
@ -78,6 +78,9 @@
|
||||
<header-file src="src/ios/APPPrinterRenderer.h" />
|
||||
<source-file src="src/ios/APPPrinterRenderer.m" />
|
||||
|
||||
<header-file src="src/ios/APPPrinterStyle.h" />
|
||||
<source-file src="src/ios/APPPrinterStyle.m" />
|
||||
|
||||
<header-file src="src/ios/APPPrinterUnit.h" />
|
||||
<source-file src="src/ios/APPPrinterUnit.m" />
|
||||
|
||||
|
@ -21,6 +21,7 @@
|
||||
|
||||
#include "APPPrinterLayout.h"
|
||||
#include "APPPrinterRenderer.h"
|
||||
#include "APPPrinterStyle.h"
|
||||
#include "APPPrinterUnit.h"
|
||||
|
||||
@interface APPPrinterRenderer ()
|
||||
@ -117,7 +118,6 @@
|
||||
inRect:(CGRect)rect
|
||||
{
|
||||
NSString *label = spec[@"text"];
|
||||
NSString *align = spec[@"align"];
|
||||
BOOL showIndex = [spec[@"showPageIndex"] boolValue];
|
||||
|
||||
if (showIndex)
|
||||
@ -129,82 +129,10 @@
|
||||
if ([self strIsNullOrEmpty:label])
|
||||
return;
|
||||
|
||||
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle]
|
||||
mutableCopy];
|
||||
APPPrinterStyle *style = [[APPPrinterStyle alloc]
|
||||
initWithDictionary:spec];
|
||||
|
||||
if ([align isEqualToString:@"left"])
|
||||
{
|
||||
style.alignment = NSTextAlignmentLeft;
|
||||
}
|
||||
else if ([align isEqualToString:@"right"])
|
||||
{
|
||||
style.alignment = NSTextAlignmentRight;
|
||||
}
|
||||
else if ([align isEqualToString:@"center"])
|
||||
{
|
||||
style.alignment = NSTextAlignmentCenter;
|
||||
}
|
||||
else if ([align isEqualToString:@"justified"])
|
||||
{
|
||||
style.alignment = NSTextAlignmentJustified;
|
||||
}
|
||||
|
||||
NSString *hex = spec[@"color"];
|
||||
UIColor *color;
|
||||
|
||||
if ([self strIsNullOrEmpty:hex])
|
||||
{
|
||||
color = UIColor.darkTextColor;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ([hex characterAtIndex:0] == '#')
|
||||
{
|
||||
hex = [hex substringFromIndex:1];
|
||||
}
|
||||
|
||||
color = [self colorFromHexString:hex];
|
||||
}
|
||||
|
||||
if (!color)
|
||||
{
|
||||
color = UIColor.darkTextColor;
|
||||
}
|
||||
|
||||
double size = [spec[@"size"] doubleValue];
|
||||
|
||||
if (size <= 0.0)
|
||||
{
|
||||
size = UIFont.smallSystemFontSize;
|
||||
}
|
||||
|
||||
UIFont *font = [UIFont fontWithName:spec[@"font"] size:size];
|
||||
|
||||
if (!font)
|
||||
{
|
||||
font = [UIFont systemFontOfSize:size];
|
||||
}
|
||||
|
||||
UIFontDescriptorSymbolicTraits traits = 0;
|
||||
|
||||
if ([spec[@"bold"] boolValue])
|
||||
{
|
||||
traits = traits | UIFontDescriptorTraitBold;
|
||||
}
|
||||
|
||||
if ([spec[@"italic"] boolValue])
|
||||
{
|
||||
traits = traits | UIFontDescriptorTraitItalic;
|
||||
}
|
||||
|
||||
font = [UIFont fontWithDescriptor:[font.fontDescriptor fontDescriptorWithSymbolicTraits:traits]
|
||||
size:size];
|
||||
|
||||
NSDictionary *attrs = @{ NSFontAttributeName: font,
|
||||
NSParagraphStyleAttributeName: style,
|
||||
NSForegroundColorAttributeName: color };
|
||||
|
||||
[label drawInRect:rect withAttributes:attrs];
|
||||
[label drawInRect:rect withAttributes:style.attributes];
|
||||
}
|
||||
|
||||
- (BOOL) strIsNullOrEmpty:(NSString *)string
|
||||
@ -212,20 +140,4 @@
|
||||
return [string isEqual:[NSNull null]] || string.length == 0;
|
||||
}
|
||||
|
||||
- (UIColor *)colorFromHexString:(NSString *)hex
|
||||
{
|
||||
unsigned rgb = 0;
|
||||
|
||||
NSScanner *scanner = [NSScanner scannerWithString:hex];
|
||||
|
||||
[scanner setCharactersToBeSkipped:
|
||||
[NSCharacterSet characterSetWithCharactersInString:@"#"]];
|
||||
|
||||
[scanner scanHexInt:&rgb];
|
||||
|
||||
return [UIColor colorWithRed:((rgb & 0xFF0000) >> 16)/255.0
|
||||
green:((rgb & 0xFF00) >> 8)/255.0
|
||||
blue:(rgb & 0xFF)/255.0 alpha:1.0];
|
||||
}
|
||||
|
||||
@end
|
||||
|
35
src/ios/APPPrinterStyle.h
Normal file
35
src/ios/APPPrinterStyle.h
Normal file
@ -0,0 +1,35 @@
|
||||
|
||||
/*
|
||||
Copyright 2013 appPlant GmbH
|
||||
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you under the Apache License, Version 2.0 (the
|
||||
"License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
*/
|
||||
|
||||
@interface APPPrinterStyle : NSObject
|
||||
|
||||
- (instancetype) initWithDictionary:(NSDictionary *)spec;
|
||||
|
||||
- (UIFont *)font;
|
||||
|
||||
- (UIColor *)color;
|
||||
|
||||
- (NSTextAlignment) textAlignment;
|
||||
|
||||
- (NSDictionary<NSAttributedStringKey, id> *)attributes;
|
||||
|
||||
@end
|
169
src/ios/APPPrinterStyle.m
Normal file
169
src/ios/APPPrinterStyle.m
Normal file
@ -0,0 +1,169 @@
|
||||
|
||||
/*
|
||||
Copyright 2013 appPlant GmbH
|
||||
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you under the Apache License, Version 2.0 (the
|
||||
"License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
*/
|
||||
|
||||
#include "APPPrinterStyle.h"
|
||||
|
||||
@interface APPPrinterStyle ()
|
||||
|
||||
@property (nonatomic, retain) NSDictionary *settings;
|
||||
|
||||
@end
|
||||
|
||||
@implementation APPPrinterStyle
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Public
|
||||
|
||||
- (instancetype) initWithDictionary:(NSDictionary *)settings
|
||||
{
|
||||
self = [self init];
|
||||
|
||||
_settings = settings;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSDictionary<NSAttributedStringKey, id> *)attributes
|
||||
{
|
||||
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle]
|
||||
mutableCopy];
|
||||
|
||||
style.alignment = self.textAlignment;
|
||||
|
||||
return @{ NSFontAttributeName: self.font,
|
||||
NSParagraphStyleAttributeName: style,
|
||||
NSForegroundColorAttributeName: self.color };
|
||||
}
|
||||
|
||||
- (UIFont *)font
|
||||
{
|
||||
double size = [_settings[@"size"] doubleValue];
|
||||
|
||||
if (size <= 0.0)
|
||||
{
|
||||
size = UIFont.smallSystemFontSize;
|
||||
}
|
||||
|
||||
UIFont *font = [UIFont fontWithName:_settings[@"font"] size:size];
|
||||
|
||||
if (!font)
|
||||
{
|
||||
font = [UIFont systemFontOfSize:size];
|
||||
}
|
||||
|
||||
UIFontDescriptorSymbolicTraits traits = 0;
|
||||
|
||||
if ([_settings[@"bold"] boolValue])
|
||||
{
|
||||
traits = traits | UIFontDescriptorTraitBold;
|
||||
}
|
||||
|
||||
if ([_settings[@"italic"] boolValue])
|
||||
{
|
||||
traits = traits | UIFontDescriptorTraitItalic;
|
||||
}
|
||||
|
||||
UIFontDescriptor *descriptor = [font.fontDescriptor
|
||||
fontDescriptorWithSymbolicTraits:traits];
|
||||
|
||||
return [UIFont fontWithDescriptor:descriptor size:size];
|
||||
}
|
||||
|
||||
- (UIColor *)color
|
||||
{
|
||||
NSString *hex = _settings[@"color"];
|
||||
UIColor *color;
|
||||
|
||||
if ([self strIsNullOrEmpty:hex])
|
||||
{
|
||||
color = UIColor.darkTextColor;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ([hex characterAtIndex:0] == '#')
|
||||
{
|
||||
hex = [hex substringFromIndex:1];
|
||||
}
|
||||
|
||||
color = [self colorFromHexString:hex];
|
||||
}
|
||||
|
||||
if (!color)
|
||||
{
|
||||
color = UIColor.darkTextColor;
|
||||
}
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
- (NSTextAlignment) textAlignment
|
||||
{
|
||||
NSString *align = _settings[@"align"];
|
||||
|
||||
if ([align isEqualToString:@"left"])
|
||||
{
|
||||
return NSTextAlignmentLeft;
|
||||
}
|
||||
|
||||
if ([align isEqualToString:@"right"])
|
||||
{
|
||||
return NSTextAlignmentRight;
|
||||
}
|
||||
|
||||
if ([align isEqualToString:@"center"])
|
||||
{
|
||||
return NSTextAlignmentCenter;
|
||||
}
|
||||
|
||||
if ([align isEqualToString:@"justified"])
|
||||
{
|
||||
return NSTextAlignmentJustified;
|
||||
}
|
||||
|
||||
return NSTextAlignmentNatural;
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Private
|
||||
|
||||
- (BOOL) strIsNullOrEmpty:(NSString *)string
|
||||
{
|
||||
return [string isEqual:[NSNull null]] || string.length == 0;
|
||||
}
|
||||
|
||||
- (UIColor *)colorFromHexString:(NSString *)hex
|
||||
{
|
||||
unsigned rgb = 0;
|
||||
|
||||
NSScanner *scanner = [NSScanner scannerWithString:hex];
|
||||
|
||||
[scanner setCharactersToBeSkipped:
|
||||
[NSCharacterSet characterSetWithCharactersInString:@"#"]];
|
||||
|
||||
[scanner scanHexInt:&rgb];
|
||||
|
||||
return [UIColor colorWithRed:((rgb & 0xFF0000) >> 16)/255.0
|
||||
green:((rgb & 0xFF00) >> 8)/255.0
|
||||
blue:(rgb & 0xFF)/255.0 alpha:1.0];
|
||||
}
|
||||
|
||||
@end
|
Loading…
Reference in New Issue
Block a user