diff --git a/plugin.xml b/plugin.xml index e1ec912..fc28e5e 100644 --- a/plugin.xml +++ b/plugin.xml @@ -78,6 +78,9 @@ + + + diff --git a/src/ios/APPPrinterRenderer.m b/src/ios/APPPrinterRenderer.m index 5fd9af4..4386b25 100644 --- a/src/ios/APPPrinterRenderer.m +++ b/src/ios/APPPrinterRenderer.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 diff --git a/src/ios/APPPrinterStyle.h b/src/ios/APPPrinterStyle.h new file mode 100644 index 0000000..b71283b --- /dev/null +++ b/src/ios/APPPrinterStyle.h @@ -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 *)attributes; + +@end diff --git a/src/ios/APPPrinterStyle.m b/src/ios/APPPrinterStyle.m new file mode 100644 index 0000000..91fd3fa --- /dev/null +++ b/src/ios/APPPrinterStyle.m @@ -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 *)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