From d37be523101c2b7668741eb138b7d344ab3ac9b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Petr=C3=ADk?= Date: Tue, 7 Nov 2017 07:29:30 +0100 Subject: [PATCH 01/12] Update constraints --- .../StyleKitDemo/Base.lproj/Main.storyboard | 139 +++++++++--------- 1 file changed, 68 insertions(+), 71 deletions(-) diff --git a/StyleKitDemo/StyleKitDemo/Base.lproj/Main.storyboard b/StyleKitDemo/StyleKitDemo/Base.lproj/Main.storyboard index 517b961..800dace 100644 --- a/StyleKitDemo/StyleKitDemo/Base.lproj/Main.storyboard +++ b/StyleKitDemo/StyleKitDemo/Base.lproj/Main.storyboard @@ -1,9 +1,14 @@ - - + + + + + - + + + @@ -11,21 +16,22 @@ - + - + - + - + - + - + - + - + - + - + - + @@ -246,7 +257,7 @@ - + @@ -284,11 +295,11 @@ - + - + @@ -297,7 +308,7 @@ - + @@ -308,7 +319,7 @@ - + @@ -319,14 +330,14 @@ - + - + @@ -348,7 +359,7 @@ - + From 28c46c0e760cbd3bbfc9871a5524089321c2586c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Petr=C3=ADk?= Date: Tue, 7 Nov 2017 14:21:44 +0100 Subject: [PATCH 09/12] Implement ParagraphStyleHelper --- StyleKit.xcodeproj/project.pbxproj | 4 + StyleKit/Helpers/ParagraphStyleHelper.swift | 98 +++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 StyleKit/Helpers/ParagraphStyleHelper.swift diff --git a/StyleKit.xcodeproj/project.pbxproj b/StyleKit.xcodeproj/project.pbxproj index 1b8a6f9..52d6066 100644 --- a/StyleKit.xcodeproj/project.pbxproj +++ b/StyleKit.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ 059932DC1D59E0DF0085E522 /* SKTryCatch.m in Sources */ = {isa = PBXBuildFile; fileRef = 059932DB1D59E0DF0085E522 /* SKTryCatch.m */; }; 059932DD1D59E1950085E522 /* SKTryCatch.h in Headers */ = {isa = PBXBuildFile; fileRef = 059932D81D59E09D0085E522 /* SKTryCatch.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 3B7872691FB1C3BB00A87FB2 /* ParagraphStyleHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B7872681FB1C3BB00A87FB2 /* ParagraphStyleHelper.swift */; }; 992723731D5B36D700B74CDD /* UIAppearance+Swift.h in Headers */ = {isa = PBXBuildFile; fileRef = 992723711D5B36D700B74CDD /* UIAppearance+Swift.h */; settings = {ATTRIBUTES = (Public, ); }; }; 992723741D5B36D700B74CDD /* UIAppearance+Swift.m in Sources */ = {isa = PBXBuildFile; fileRef = 992723721D5B36D700B74CDD /* UIAppearance+Swift.m */; }; 996775991D5A2E8E005DA08A /* StyleParsable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 996775981D5A2E8E005DA08A /* StyleParsable.swift */; }; @@ -56,6 +57,7 @@ /* Begin PBXFileReference section */ 059932D81D59E09D0085E522 /* SKTryCatch.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SKTryCatch.h; sourceTree = ""; }; 059932DB1D59E0DF0085E522 /* SKTryCatch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SKTryCatch.m; sourceTree = ""; }; + 3B7872681FB1C3BB00A87FB2 /* ParagraphStyleHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParagraphStyleHelper.swift; sourceTree = ""; }; 992723711D5B36D700B74CDD /* UIAppearance+Swift.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIAppearance+Swift.h"; sourceTree = ""; }; 992723721D5B36D700B74CDD /* UIAppearance+Swift.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIAppearance+Swift.m"; sourceTree = ""; }; 996775981D5A2E8E005DA08A /* StyleParsable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StyleParsable.swift; sourceTree = ""; }; @@ -178,6 +180,7 @@ 9967759E1D5A3691005DA08A /* ColorHelper.swift */, 996775A01D5A38E8005DA08A /* ControlStateHelper.swift */, 9967759C1D5A3607005DA08A /* FontHelper.swift */, + 3B7872681FB1C3BB00A87FB2 /* ParagraphStyleHelper.swift */, ); path = Helpers; sourceTree = ""; @@ -378,6 +381,7 @@ A1E4676E1D59494C003B1AB2 /* SKLogger.swift in Sources */, A10FD6BF1D55357400341EDD /* FileLoader.swift in Sources */, 059932DC1D59E0DF0085E522 /* SKTryCatch.m in Sources */, + 3B7872691FB1C3BB00A87FB2 /* ParagraphStyleHelper.swift in Sources */, A10FD6C81D556A9C00341EDD /* Stylist.swift in Sources */, A104CE431D580D6A00626F2A /* ColorExtensions.swift in Sources */, 9967759F1D5A3691005DA08A /* ColorHelper.swift in Sources */, diff --git a/StyleKit/Helpers/ParagraphStyleHelper.swift b/StyleKit/Helpers/ParagraphStyleHelper.swift new file mode 100644 index 0000000..4dc3b4f --- /dev/null +++ b/StyleKit/Helpers/ParagraphStyleHelper.swift @@ -0,0 +1,98 @@ +// +// ParagraphStyleHelper.swift +// StyleKit +// +// Created by Jakub Petrík on 11/7/17. +// Copyright © 2017 Bernard Gatt. All rights reserved. +// + +import Foundation + +public enum ParagraphStyleHelper { + public static func parseParagraphStyle(_ name: String, value: [String: Any]) -> NSParagraphStyle? { + guard name == "NSParagraphStyle" else { return nil } + + let style = NSMutableParagraphStyle() + if let alignmentValue = value["alignment"] as? String { + style.alignment = parseTextAlignment(alignmentValue) + } + if let indent = value["firstLineHeadIndent"] as? Double { + style.firstLineHeadIndent = CGFloat(indent) + } + if let indent = value["headIndent"] as? Double { + style.headIndent = CGFloat(indent) + } + if let indent = value["tailIndent"] as? Double { + style.tailIndent = CGFloat(indent) + } + if let breakMode = value["lineBreakMode"] as? String { + style.lineBreakMode = parseLineBreakMode(breakMode) + } + if let maxLineHeight = value["maximumLineHeight"] as? Double { + style.maximumLineHeight = CGFloat(maxLineHeight) + } + if let minLineHeight = value["minimumLineHeight"] as? Double { + style.minimumLineHeight = CGFloat(minLineHeight) + } + if let spacing = value["lineSpacing"] as? Double { + style.lineSpacing = CGFloat(spacing) + } + if let spacing = value["paragraphSpacing"] as? Double { + style.paragraphSpacing = CGFloat(spacing) + } + if let spacing = value["paragraphSpacingBefore"] as? Double { + style.paragraphSpacingBefore = CGFloat(spacing) + } + if let writingDirection = value["baseWritingDirection"] as? String { + style.baseWritingDirection = parseWritingDirection(writingDirection) + } + if let height = value["lineHeightMultiple"] as? Double { + style.lineHeightMultiple = CGFloat(height) + } + + return style.copy() as? NSParagraphStyle + } + + private static func parseTextAlignment(_ value: String) -> NSTextAlignment { + switch value { + case "left": + return .left + case "right": + return .right + case "center": + return .center + case "justified": + return .justified + default: + return .natural + } + } + + private static func parseLineBreakMode(_ value: String) -> NSLineBreakMode { + switch value { + case "byCharWrapping": + return .byCharWrapping + case "byClipping": + return .byClipping + case "byTruncatingHead": + return .byTruncatingHead + case "byTruncatingTail": + return .byTruncatingTail + case "byTruncatingMiddle": + return .byTruncatingMiddle + default: + return .byWordWrapping + } + } + + private static func parseWritingDirection(_ value: String) -> NSWritingDirection { + switch value { + case "leftToRight": + return .leftToRight + case "rightToLeft": + return .rightToLeft + default: + return .natural + } + } +} From 915a9276f04c8c71865416a064bfc12f07509d88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Petr=C3=ADk?= Date: Tue, 7 Nov 2017 14:23:07 +0100 Subject: [PATCH 10/12] Use style instead of name in getStyle --- StyleKit/Stylist.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/StyleKit/Stylist.swift b/StyleKit/Stylist.swift index 15a7d49..428fe73 100644 --- a/StyleKit/Stylist.swift +++ b/StyleKit/Stylist.swift @@ -92,11 +92,11 @@ class Stylist { if let styles = object as? Stylist.Style { var stylesToApply = Stylist.Style() for (style, value) in styles { - stylesToApply[style] = styleParser.getStyle(forName: name, value: self.getValue(value)) + stylesToApply[style] = styleParser.getStyle(forName: style, value: getValue(value)) } - callAppearanceSelector(selectorName, valueOne: stylesToApply as AnyObject?, valueTwo: state) + callAppearanceSelector(selectorName, valueOne: stylesToApply as AnyObject, valueTwo: state) } else { - let value = styleParser.getStyle(forName: name, value: self.getValue(object)) + let value = styleParser.getStyle(forName: name, value: getValue(object)) callAppearanceSelector(selectorName, valueOne: value, valueTwo: state) } } From a5bb1c3558b6035ce58d0a5de5a6accba7a90e7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Petr=C3=ADk?= Date: Tue, 7 Nov 2017 14:23:38 +0100 Subject: [PATCH 11/12] Try to parse paragraph style --- StyleKit/StyleParser.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/StyleKit/StyleParser.swift b/StyleKit/StyleParser.swift index 9ed3bb5..a027039 100644 --- a/StyleKit/StyleParser.swift +++ b/StyleKit/StyleParser.swift @@ -9,6 +9,10 @@ class StyleParser: StyleParsable { return font } else if let color = ColorHelper.parseColor(value) { return color + } + } else if let value = value as? [String: Any] { + if let paragraphStyle = ParagraphStyleHelper.parseParagraphStyle(name, value: value) { + return paragraphStyle } } return value From 232f4ac33fb45572ee8c3ba9c00056ab54b16a53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Petr=C3=ADk?= Date: Tue, 7 Nov 2017 14:23:53 +0100 Subject: [PATCH 12/12] Add tests --- StyleKitTests/HelperTests.swift | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/StyleKitTests/HelperTests.swift b/StyleKitTests/HelperTests.swift index d5f7158..8dd6d34 100644 --- a/StyleKitTests/HelperTests.swift +++ b/StyleKitTests/HelperTests.swift @@ -69,5 +69,45 @@ class HelperTests: XCTestCase { "Font helper should return nil") } + + func testParagraphStyleHelper() { + let expected = NSMutableParagraphStyle() + expected.alignment = .center + expected.firstLineHeadIndent = 2 + expected.headIndent = 3 + expected.tailIndent = 4 + expected.lineBreakMode = .byClipping + expected.maximumLineHeight = 10 + expected.minimumLineHeight = 12 + expected.lineSpacing = 8 + expected.paragraphSpacing = 9 + expected.paragraphSpacingBefore = 13 + expected.baseWritingDirection = .rightToLeft + expected.lineHeightMultiple = 44 + + let jsonString: String = """ + { + "alignment": "center", + "firstLineHeadIndent": 2, + "headIndent": 3, + "tailIndent": 4, + "lineBreakMode": "byClipping", + "maximumLineHeight": 10, + "minimumLineHeight": 12, + "lineSpacing": 8, + "paragraphSpacing": 9, + "paragraphSpacingBefore": 13, + "baseWritingDirection": "rightToLeft", + "lineHeightMultiple": 44 + } + """ + + let json = try! JSONSerialization.jsonObject(with: jsonString.data(using: .utf8)!, options: .allowFragments) as! [String: Any] + + + let actual = ParagraphStyleHelper.parseParagraphStyle(NSStringFromClass(NSParagraphStyle.self), value: json) + + XCTAssertEqual(expected, actual) + } }