Commit 55d70ff5 by Daniel Dahan

GridView working

parent acf28e9a
...@@ -53,6 +53,7 @@ class ViewController: UIViewController { ...@@ -53,6 +53,7 @@ class ViewController: UIViewController {
private func prepareHorizontalGridViewExample() { private func prepareHorizontalGridViewExample() {
var image: UIImage? = UIImage(named: "ic_flash_auto_white")?.imageWithRenderingMode(.AlwaysTemplate) var image: UIImage? = UIImage(named: "ic_flash_auto_white")?.imageWithRenderingMode(.AlwaysTemplate)
let btn1: FlatButton = FlatButton() let btn1: FlatButton = FlatButton()
btn1.grid = .Grid2
btn1.pulseColor = MaterialColor.blueGrey.darken4 btn1.pulseColor = MaterialColor.blueGrey.darken4
btn1.tintColor = MaterialColor.blueGrey.darken4 btn1.tintColor = MaterialColor.blueGrey.darken4
btn1.backgroundColor = MaterialColor.grey.lighten3 btn1.backgroundColor = MaterialColor.grey.lighten3
...@@ -61,6 +62,7 @@ class ViewController: UIViewController { ...@@ -61,6 +62,7 @@ class ViewController: UIViewController {
image = UIImage(named: "ic_flash_off_white")?.imageWithRenderingMode(.AlwaysTemplate) image = UIImage(named: "ic_flash_off_white")?.imageWithRenderingMode(.AlwaysTemplate)
let btn2: FlatButton = FlatButton() let btn2: FlatButton = FlatButton()
// btn2.grid = .Grid3
btn2.pulseColor = MaterialColor.blueGrey.darken4 btn2.pulseColor = MaterialColor.blueGrey.darken4
btn2.tintColor = MaterialColor.blueGrey.darken4 btn2.tintColor = MaterialColor.blueGrey.darken4
btn2.backgroundColor = MaterialColor.grey.lighten3 btn2.backgroundColor = MaterialColor.grey.lighten3
...@@ -75,21 +77,25 @@ class ViewController: UIViewController { ...@@ -75,21 +77,25 @@ class ViewController: UIViewController {
btn3.setImage(image, forState: .Normal) btn3.setImage(image, forState: .Normal)
btn3.setImage(image, forState: .Highlighted) btn3.setImage(image, forState: .Highlighted)
let label1: UILabel = UILabel() let label1: MaterialLabel = MaterialLabel()
label1.text = "A" label1.text = "A"
label1.backgroundColor = MaterialColor.blue.base label1.backgroundColor = MaterialColor.blue.base
let label2: UILabel = UILabel() let label2: MaterialLabel = MaterialLabel()
label2.text = "B" label2.text = "B"
label2.grid = .Grid2
label2.backgroundColor = MaterialColor.blue.base label2.backgroundColor = MaterialColor.blue.base
let gridView: GridView = GridView(frame: CGRectMake(0, 100, view.bounds.width, 40)) let gridView: GridView = GridView()
gridView.grid = .Grid5 gridView.grid = .Grid5
gridView.spacing = 16 gridView.spacing = 32
gridView.views = [btn1, btn2, btn3, label1, label2] // gridView.unifiedHeight = 40
gridView.views = [btn1, btn2, label2]
view.addSubview(gridView) view.addSubview(gridView)
gridView.translatesAutoresizingMaskIntoConstraints = false
MaterialLayout.alignFromTop(view, child: gridView)
MaterialLayout.alignToParentHorizontally(view, child: gridView)
} }
} }
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
import UIKit import UIKit
public enum Grid : Int { public enum Grid : Int {
case Grid1 = 1
case Grid2 = 2 case Grid2 = 2
case Grid3 = 3 case Grid3 = 3
case Grid4 = 4 case Grid4 = 4
...@@ -49,9 +50,21 @@ public enum GridLayout { ...@@ -49,9 +50,21 @@ public enum GridLayout {
case Vertical case Vertical
} }
public protocol GridCell {
/// Grid spacing.
var grid: Grid { get set }
}
public class GridView : MaterialView { public class GridView : MaterialView {
/// A single height for all views.
public var unifiedHeight: CGFloat = 0 {
didSet {
reloadLayout()
}
}
/// The grid size. /// The grid size.
public var grid: Grid { public override var grid: Grid {
didSet { didSet {
reloadLayout() reloadLayout()
} }
...@@ -72,7 +85,7 @@ public class GridView : MaterialView { ...@@ -72,7 +85,7 @@ public class GridView : MaterialView {
} }
/// An Array of UIButtons. /// An Array of UIButtons.
public var views: Array<UIView>? { public var views: Array<GridCell>? {
didSet { didSet {
reloadLayout() reloadLayout()
} }
...@@ -83,15 +96,20 @@ public class GridView : MaterialView { ...@@ -83,15 +96,20 @@ public class GridView : MaterialView {
} }
public required init?(coder aDecoder: NSCoder) { public required init?(coder aDecoder: NSCoder) {
grid = .Grid12
spacing = 0 spacing = 0
super.init(coder: aDecoder) super.init(coder: aDecoder)
grid = .Grid12
} }
public override init(frame: CGRect) { public override init(frame: CGRect) {
grid = .Grid12
spacing = 0 spacing = 0
super.init(frame: frame) super.init(frame: frame)
grid = .Grid12
}
public override func layoutSubviews() {
super.layoutSubviews()
reloadLayout()
} }
/// Reload the button layout. /// Reload the button layout.
...@@ -100,12 +118,20 @@ public class GridView : MaterialView { ...@@ -100,12 +118,20 @@ public class GridView : MaterialView {
v.removeFromSuperview() v.removeFromSuperview()
} }
let w: CGFloat = (width - spacing) / CGFloat(grid.rawValue) let g: Int = grid.rawValue
if let v: Array<UIView> = views { let w: CGFloat = (width - spacing) / CGFloat(g)
if let v: Array<GridCell> = views {
var n: Int = 0
for var i: Int = 0, l: Int = v.count; i < l; ++i { for var i: Int = 0, l: Int = v.count; i < l; ++i {
let view: UIView = v[i] let cell: GridCell = v[i]
view.frame = CGRectMake(CGFloat(i) * w + spacing, 0, w - spacing, height) let view: UIView = cell as! UIView
let m: Int = cell.grid.rawValue
if .Horizontal == layout {
view.frame = CGRectMake(CGFloat(i + n) * w + spacing, 0, (w * CGFloat(m)) - spacing, 0 < unifiedHeight ? unifiedHeight : view.intrinsicContentSize().height)
}
view.removeFromSuperview()
addSubview(view) addSubview(view)
n += m - 1
} }
} }
} }
......
...@@ -31,7 +31,10 @@ ...@@ -31,7 +31,10 @@
import UIKit import UIKit
@objc(MaterialButton) @objc(MaterialButton)
public class MaterialButton : UIButton { public class MaterialButton : UIButton, GridCell {
/// Grid space.
public var grid: Grid = .Grid1
/** /**
A CAShapeLayer used to manage elements that would be affected by A CAShapeLayer used to manage elements that would be affected by
the clipToBounds property of the backing layer. For example, this the clipToBounds property of the backing layer. For example, this
......
...@@ -30,122 +30,7 @@ ...@@ -30,122 +30,7 @@
import UIKit import UIKit
public class MaterialLabel : UILabel { public class MaterialLabel : UILabel, GridCell {
/** /// Grid space.
:name: layerClass public var grid: Grid = .Grid1
*/
public override class func layerClass() -> AnyClass {
return MaterialTextLayer.self
}
/**
:name: textLayer
*/
public var textLayer: MaterialTextLayer {
return layer as! MaterialTextLayer
}
/**
:name: text
*/
public override var text: String? {
didSet {
textLayer.text = text
}
}
/**
:name: textColor
*/
public override var textColor: UIColor? {
didSet {
textLayer.textColor = textColor
}
}
/**
:name: font
*/
public override var font: UIFont! {
didSet {
textLayer.fontType = font
}
}
/**
:name: textAlignment
*/
public override var textAlignment: NSTextAlignment {
didSet {
textLayer.textAlignment = textAlignment
}
}
/**
:name: wrapped
*/
public var wrapped: Bool {
didSet {
textLayer.wrapped = wrapped
}
}
/**
:name: contentsScale
*/
public var contentsScale: CGFloat {
didSet {
textLayer.contentsScale = contentsScale
}
}
/**
:name: lineBreakMode
*/
public override var lineBreakMode: NSLineBreakMode {
didSet {
textLayer.lineBreakMode = lineBreakMode
}
}
/**
:name: init
*/
public required init?(coder aDecoder: NSCoder) {
wrapped = true
contentsScale = UIScreen.mainScreen().scale
super.init(coder: aDecoder)
}
/**
:name: init
*/
public override init(frame: CGRect) {
wrapped = true
contentsScale = UIScreen.mainScreen().scale
super.init(frame: frame)
prepareView()
}
/**
:name: init
*/
public convenience init() {
self.init(frame: CGRectNull)
prepareView()
}
/**
:name: stringSize
*/
public func stringSize(constrainedToWidth width: Double) -> CGSize {
return textLayer.stringSize(constrainedToWidth: width)
}
/**
:name: prepareView
*/
public func prepareView() {
textAlignment = .Left
}
} }
\ No newline at end of file
...@@ -31,7 +31,10 @@ ...@@ -31,7 +31,10 @@
import UIKit import UIKit
@objc(MaterialView) @objc(MaterialView)
public class MaterialView : UIView { public class MaterialView : UIView, GridCell {
/// Grid space.
public var grid: Grid = .Grid1
/** /**
A CAShapeLayer used to manage elements that would be affected by A CAShapeLayer used to manage elements that would be affected by
the clipToBounds property of the backing layer. For example, this the clipToBounds property of the backing layer. For example, this
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment