Skip to content

Commit

Permalink
Fixed warnings in TextStyle JSON extension
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaultKlein committed May 1, 2017
1 parent 72251f5 commit f2b7a2b
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 133 deletions.
63 changes: 54 additions & 9 deletions Kumi/Core/Utility/TextStyle+JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,46 @@ private extension NSLineBreakMode {

}

private extension NSUnderlineStyle {

static func fromString(string: String) -> NSUnderlineStyle? {
switch string {
case "none":
return NSUnderlineStyle.styleNone
case "single":
return NSUnderlineStyle.styleSingle
case "thick":
return NSUnderlineStyle.styleThick
case "double":
return NSUnderlineStyle.styleDouble
case "patternSolid":
return NSUnderlineStyle.patternSolid
case "patternDot":
return NSUnderlineStyle.patternDot
case "patternDash":
return NSUnderlineStyle.patternDash
case "patternDashDot":
return NSUnderlineStyle.patternDashDot
case "patternDashDotDot":
return NSUnderlineStyle.patternDashDotDot
case "byWord":
return NSUnderlineStyle.byWord
default:
return nil
}
}
}

extension TextStyle {

init?(json: JSON) {
guard let fontName = json["fontFamily"] as? String,
guard let fontNameJSON = json["fontWeight"] as? JSON,
let normalFontName = fontNameJSON["normal"] as? String,
let textSize = json["textSize"] as? CGFloat else {
return nil
}

guard let font = UIFont(name: fontName, size: textSize) else {
guard let font = UIFont(name: normalFontName, size: textSize) else {
return nil
}

Expand All @@ -97,6 +128,14 @@ extension TextStyle {
var strikethroughColor: UIColor?
var textTransform: TextTransform = .none

if let emFontName = fontNameJSON["emphasis"] as? String {
emFont = UIFont(name: emFontName, size: textSize)
}

if let strongFontName = fontNameJSON["strong"] as? String {
strongFont = UIFont(name: strongFontName, size: textSize)
}

if let textColorJSON = json["color"] as? JSON {
textColor = UIColor(json: textColorJSON)
}
Expand Down Expand Up @@ -129,20 +168,26 @@ extension TextStyle {
paragraphSpacingBefore = paragraphSpaceBefore
}

if let textAlignmentString = json["textAlign"] as? String,
let textAlign = NSTextAlignment.fromString(string: textAlignmentString) {
textAlignment = textAlign
if let textAlignmentString = json["textAlign"] as? String {
textAlignment = NSTextAlignment.fromString(string: textAlignmentString)
}

if let lineBreakModeString = json["lineBreakMode"] as? String,
let lineBrkMode = NSLineBreakMode.fromString(string: lineBreakModeString) {
lineBreakMode = lineBrkMode
if let lineBreakModeString = json["lineBreakMode"] as? String {
lineBreakMode = NSLineBreakMode.fromString(string: lineBreakModeString)
}

if let strikethroughStyleString = json["strikethroughStyle"] as? String {
strikethroughStyle = NSUnderlineStyle.fromString(string: strikethroughStyleString)
}

if let transform = json["textTransform"] as? String {
textTransform = TextTransform(string: transform)
}

if let strikethroughColorJSON = json["strikethroughColor"] as? JSON {
strikethroughColor = UIColor(json: strikethroughColorJSON)
}

self.init(font: font,
emFont: emFont,
strongFont: strongFont,
Expand All @@ -160,5 +205,5 @@ extension TextStyle {
strikethroughColor: strikethroughColor,
textTransform: textTransform)
}

}
49 changes: 25 additions & 24 deletions KumiTests/Font/FontStyle.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
{
"fontFamily": "Helvetica",
"textSize": 18,
"fontWeight": {
"normal": "roman/regular",
"strong": "roman/bold",
"emphasis": "roman/heavy"
},
"color": {
"$ref": "#/theme/color/primary/normal"
},
"letterSpacing": 1,
"lineSpacing": 0,
"lineHeightMultiple": 1,
"minimumLineHeight": 10,
"maximumLineHeight": 25,
"paragraphSpacing": 12,
"paragraphSpacingBefore": 3,
"textAlign": "left",
"lineBreakMode": "​​byTruncatingTail",
"textTransform": "none",
"textDecorationLine": "none",
"textDecorationColor": {
"$ref": "#/theme/color/primary/normal"
}
"fontFamily": "Helvetica",
"textSize": 18,
"fontWeight": {
"normal": "Helvetica",
"strong": "Helvetica-Bold",
"emphasis": "Helvetica-Heavy"
},
"color": {
"$ref": "#/theme/color/primary/normal"
},
"letterSpacing": 1,
"lineSpacing": 0,
"lineHeightMultiple": 1,
"minimumLineHeight": 10,
"maximumLineHeight": 25,
"paragraphSpacing": 12,
"paragraphSpacingBefore": 3,
"textAlign": "left",
"lineBreakMode": "​​byTruncatingTail",
"textTransform": "none",
"textDecorationLine": "none",
"textDecorationColor": {
"$ref": "#/theme/color/primary/normal"
},
"strikethroughStyle": "none"
}
Loading

0 comments on commit f2b7a2b

Please sign in to comment.