Skip to content

Commit

Permalink
Add info label delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
vadymmarkov committed Jan 29, 2016
1 parent 6ef8b11 commit c7d52ad
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
8 changes: 4 additions & 4 deletions Lightbox.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
D573A2F31C5B5C7B006053DD /* LayoutConfigurable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D573A2F21C5B5C7B006053DD /* LayoutConfigurable.swift */; };
D573A2F51C5B5CA4006053DD /* LightboxTransition.swift in Sources */ = {isa = PBXBuildFile; fileRef = D573A2F41C5B5CA4006053DD /* LightboxTransition.swift */; };
D573A2F71C5B5E55006053DD /* UIView+Gradient.swift in Sources */ = {isa = PBXBuildFile; fileRef = D573A2F61C5B5E55006053DD /* UIView+Gradient.swift */; };
D573A2F91C5B6B92006053DD /* Expandable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D573A2F81C5B6B92006053DD /* Expandable.swift */; };
D573A2FB1C5B6F5B006053DD /* Expandable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D573A2FA1C5B6F5B006053DD /* Expandable.swift */; };
D58A18CB1C5ABF8F000024BB /* FooterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D58A18CA1C5ABF8F000024BB /* FooterView.swift */; };
/* End PBXBuildFile section */

Expand All @@ -34,7 +34,7 @@
D573A2F21C5B5C7B006053DD /* LayoutConfigurable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LayoutConfigurable.swift; sourceTree = "<group>"; };
D573A2F41C5B5CA4006053DD /* LightboxTransition.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LightboxTransition.swift; sourceTree = "<group>"; };
D573A2F61C5B5E55006053DD /* UIView+Gradient.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+Gradient.swift"; sourceTree = "<group>"; };
D573A2F81C5B6B92006053DD /* Expandable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Expandable.swift; sourceTree = "<group>"; };
D573A2FA1C5B6F5B006053DD /* Expandable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Expandable.swift; sourceTree = "<group>"; };
D58A18CA1C5ABF8F000024BB /* FooterView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FooterView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -103,10 +103,10 @@
D573A2F11C5B5C72006053DD /* Library */ = {
isa = PBXGroup;
children = (
D573A2FA1C5B6F5B006053DD /* Expandable.swift */,
D573A2F41C5B5CA4006053DD /* LightboxTransition.swift */,
D573A2F21C5B5C7B006053DD /* LayoutConfigurable.swift */,
D573A2F61C5B5E55006053DD /* UIView+Gradient.swift */,
D573A2F81C5B6B92006053DD /* Expandable.swift */,
);
path = Library;
sourceTree = "<group>";
Expand Down Expand Up @@ -213,7 +213,7 @@
D523B0BD1C43AA8B001AD1EC /* LightboxModel.swift in Sources */,
D573A2F31C5B5C7B006053DD /* LayoutConfigurable.swift in Sources */,
D523B0BE1C43AA8B001AD1EC /* LightboxController.swift in Sources */,
D573A2F91C5B6B92006053DD /* Expandable.swift in Sources */,
D573A2FB1C5B6F5B006053DD /* Expandable.swift in Sources */,
D54DFCBE1C5AAAD600ADEA0E /* InfoLabel.swift in Sources */,
D58A18CB1C5ABF8F000024BB /* FooterView.swift in Sources */,
D573A2F01C5B5605006053DD /* HeaderView.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Source/LightboxController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public class LightboxController: UIViewController {
transitionManager.scrollView = scrollView
transitioningDelegate = transitionManager

[scrollView, overlayView, footerView].forEach { view.addSubview($0) }
[scrollView, overlayView, headerView, footerView].forEach { view.addSubview($0) }

currentPage = 0
configureLayout(screenBounds.size)
Expand Down
39 changes: 24 additions & 15 deletions Source/Views/FooterView.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import UIKit

class FooterView: UIView, Expandable {
class FooterView: UIView {

lazy var infoLabel: InfoLabel = { [unowned self] in
let label = InfoLabel(model: self.model,
text: self.model.text)
label.hidden = !self.model.infoLabel.enabled
label.textColor = .whiteColor()
label.userInteractionEnabled = true

return label
}()
Expand All @@ -31,7 +32,7 @@ class FooterView: UIView, Expandable {

var expanded = false {
didSet {
resetFrame()
resetSubviewFrames()
}
}

Expand All @@ -53,28 +54,17 @@ class FooterView: UIView, Expandable {

// MARK: - Helpers

func resetFrame() {
infoLabel.expanded = expanded
configureLayout()
}

func updatePage(page: Int, _ numberOfPages: Int) {
let text = "\(page)/\(numberOfPages)"

pageLabel.attributedText = NSAttributedString(string: text,
attributes: model.pageIndicator.textAttributes)
pageLabel.sizeToFit()
}
}

// MARK: - LayoutConfigurable

extension FooterView: LayoutConfigurable {

func configureLayout() {
infoLabel.frame = CGRect(x: 17, y: 0, width: frame.width - 17 * 2, height: 35)
infoLabel.resetFrame()
// MARK: - Layout

func resetSubviewFrames() {
frame.size.height = infoLabel.frame.height + 40 + 0.5

pageLabel.frame.origin = CGPoint(
Expand All @@ -87,3 +77,22 @@ extension FooterView: LayoutConfigurable {
infoLabel.frame.origin.y = separatorView.frame.minY - infoLabel.frame.height - 20
}
}

// MARK: - LayoutConfigurable

extension FooterView: LayoutConfigurable {

func configureLayout() {
infoLabel.frame = CGRect(x: 17, y: 0, width: frame.width - 17 * 2, height: 35)
infoLabel.expanded = expanded

resetSubviewFrames()
}
}

extension FooterView: InfoLabelDelegate {

func infoLabelDidUpdateState(infoLabel: InfoLabel) {
expanded = infoLabel.expanded
}
}
25 changes: 22 additions & 3 deletions Source/Views/InfoLabel.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import UIKit

class InfoLabel: UILabel, Expandable {
protocol InfoLabelDelegate: class {

func infoLabelDidUpdateState(infoLabel: InfoLabel)
}

class InfoLabel: UILabel {

lazy var tapGestureRecognizer: UITapGestureRecognizer = {
let gesture = UITapGestureRecognizer()
gesture.addTarget(self, action: "labelDidTap:")

return gesture
}()

let model: LightboxModel
let numberOfVisibleLines = 2
var fullText: String
weak var delegate: InfoLabelDelegate?

var ellipsis: String {
return "... \(model.infoLabel.ellipsisText)"
Expand All @@ -15,8 +29,6 @@ class InfoLabel: UILabel, Expandable {
}
}

var fullText: String

var truncatedText: String {
var truncatedText = fullText

Expand Down Expand Up @@ -50,6 +62,8 @@ class InfoLabel: UILabel, Expandable {
numberOfLines = 0
updateText(text)
self.expanded = expanded

addGestureRecognizer(tapGestureRecognizer)
}

required init?(coder aDecoder: NSCoder) {
Expand All @@ -64,6 +78,11 @@ class InfoLabel: UILabel, Expandable {

// MARK: - Actions

func labelDidTap(tapGestureRecognizer: UITapGestureRecognizer) {
expanded = !expanded
delegate?.infoLabelDidUpdateState(self)
}

private func expand() {
frame.size.height = heightForString(fullText)
updateText(fullText)
Expand Down

0 comments on commit c7d52ad

Please sign in to comment.