Skip to content

Commit 812ef80

Browse files
committed
switch node added, nodes fixes
1 parent 39c076d commit 812ef80

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

Demos/Demos/TableViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class SampleCell: UITableViewCell {
162162

163163
let attr = NSAttributedString(string: model.details, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(12), NSParagraphStyleAttributeName: p])
164164

165-
let detailsNode = LabelNode(tag: Tags.Details, text: .Attributed(attr), numberOfLines: 4) {
165+
let detailsNode = LabelNode(tag: Tags.Details, text: .Attributed(attr), numberOfLines: 2) {
166166
let v = $0 ?? UILabel()
167167
v.textColor = UIColor.grayColor()
168168
v.backgroundColor = UIColor.lightGrayColor().colorWithAlphaComponent(0.2)

LayoutOps/LayoutOps.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
1B11ECF71DE4F915001FEBE7 /* Node.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B11ECF61DE4F915001FEBE7 /* Node.swift */; };
1212
1B11ECFE1DE57CC6001FEBE7 /* LabelNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B11ECFD1DE57CC6001FEBE7 /* LabelNode.swift */; };
1313
1B11ED001DE57D03001FEBE7 /* ImageNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B11ECFF1DE57D03001FEBE7 /* ImageNode.swift */; };
14+
1B20DAE31DEF386600FE6643 /* SwitchNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B20DAE21DEF386600FE6643 /* SwitchNode.swift */; };
1415
1B2B82981DCD1F90008B6450 /* Anchor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B2B82971DCD1F90008B6450 /* Anchor.swift */; };
1516
1B49D7101DB8D02800823FF3 /* SizeToFit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B49D7091DB8D02800823FF3 /* SizeToFit.swift */; };
1617
1B49D7111DB8D02800823FF3 /* LayoutOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B49D70A1DB8D02800823FF3 /* LayoutOperation.swift */; };
@@ -28,6 +29,7 @@
2829
1B11ECF61DE4F915001FEBE7 /* Node.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Node.swift; sourceTree = "<group>"; };
2930
1B11ECFD1DE57CC6001FEBE7 /* LabelNode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LabelNode.swift; sourceTree = "<group>"; };
3031
1B11ECFF1DE57D03001FEBE7 /* ImageNode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageNode.swift; sourceTree = "<group>"; };
32+
1B20DAE21DEF386600FE6643 /* SwitchNode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwitchNode.swift; sourceTree = "<group>"; };
3133
1B2B82971DCD1F90008B6450 /* Anchor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Anchor.swift; sourceTree = "<group>"; };
3234
1B49D7091DB8D02800823FF3 /* SizeToFit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SizeToFit.swift; sourceTree = "<group>"; };
3335
1B49D70A1DB8D02800823FF3 /* LayoutOperation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LayoutOperation.swift; sourceTree = "<group>"; };
@@ -87,6 +89,7 @@
8789
1B11ECF61DE4F915001FEBE7 /* Node.swift */,
8890
1B11ECFD1DE57CC6001FEBE7 /* LabelNode.swift */,
8991
1B11ECFF1DE57D03001FEBE7 /* ImageNode.swift */,
92+
1B20DAE21DEF386600FE6643 /* SwitchNode.swift */,
9093
);
9194
path = LayoutOps;
9295
sourceTree = "<group>";
@@ -170,6 +173,7 @@
170173
buildActionMask = 2147483647;
171174
files = (
172175
1B49D7121DB8D02800823FF3 /* Viewport.swift in Sources */,
176+
1B20DAE31DEF386600FE6643 /* SwitchNode.swift in Sources */,
173177
1B49D7101DB8D02800823FF3 /* SizeToFit.swift in Sources */,
174178
1B49D7131DB8D02800823FF3 /* Follow.swift in Sources */,
175179
1B11ECF71DE4F915001FEBE7 /* Node.swift in Sources */,

LayoutOps/LayoutOps/ImageNode.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import UIKit
77

88
public class ImageNode: AnyNode {
99

10-
private let image: UIImage
11-
public init<T: UIImageView>(tag: Taggable, image: UIImage, subnodes: [AnyNode] = [], initializer: (T?)->T) {
10+
private let image: UIImage?
11+
public init<T: UIImageView>(tag: Taggable, image: UIImage?, subnodes: [AnyNode] = [], initializer: (T?)->T) {
1212
self.image = image
1313
super.init(tag: tag, subnodes: subnodes) { (imageView: T?) -> T in
1414
let imgView = initializer(imageView)
@@ -18,6 +18,11 @@ public class ImageNode: AnyNode {
1818
}
1919

2020
public override func sizeThatFits(size: CGSize) -> CGSize {
21-
return image.size
21+
22+
struct Cache {
23+
static let imageView = UIImageView()
24+
}
25+
Cache.imageView.image = image
26+
return Cache.imageView.sizeThatFits(size)
2227
}
2328
}

LayoutOps/LayoutOps/LabelNode.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import UIKit
77

88
public enum LabelNodeString {
9-
case Regular(String, UIFont)
10-
case Attributed(NSAttributedString)
9+
case Regular(String?, UIFont)
10+
case Attributed(NSAttributedString?)
1111
}
1212

1313
public class LabelNode: AnyNode {
@@ -65,12 +65,12 @@ extension LabelNode: Baselinable {
6565
switch type {
6666
case .First:
6767
var ptr = NSRange()
68-
if attr.length > 0 {
68+
if let attr = attr where attr.length > 0 {
6969
font = attr.attribute(NSFontAttributeName, atIndex: 0, effectiveRange: &ptr) as? UIFont
7070
}
7171
case .Last:
7272
var ptr = NSRange()
73-
if attr.length > 0 {
73+
if let attr = attr where attr.length > 0 {
7474
font = attr.attribute(NSFontAttributeName, atIndex: attr.length - 1, effectiveRange: &ptr) as? UIFont
7575
}
7676
}
@@ -91,7 +91,7 @@ extension LabelNode: LayoutableWithFont {
9191
public var font: UIFont! {
9292
switch text {
9393
case .Attributed(let attr):
94-
if attr.length > 0 {
94+
if let attr = attr where attr.length > 0 {
9595
var ptr = NSRange()
9696
return attr.attribute(NSFontAttributeName, atIndex: 0, effectiveRange: &ptr) as? UIFont
9797
} else {

LayoutOps/LayoutOps/SwitchNode.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Created by Pavel Sharanda on 23.11.16.
3+
// Copyright © 2016 Pavel Sharanda. All rights reserved.
4+
//
5+
6+
import UIKit
7+
8+
public class SwitchNode: AnyNode {
9+
10+
11+
public init<T: UISwitch>(tag: Taggable, image: UIImage?, subnodes: [AnyNode] = [], initializer: (T?)->T) {
12+
13+
super.init(tag: tag, subnodes: subnodes) { (imageView: T?) -> T in
14+
return initializer(imageView)
15+
}
16+
}
17+
18+
public override func sizeThatFits(size: CGSize) -> CGSize {
19+
20+
struct Cache {
21+
static let switchView = UISwitch()
22+
}
23+
24+
return Cache.switchView.sizeThatFits(size)
25+
}
26+
}

0 commit comments

Comments
 (0)