Skip to content

Commit

Permalink
Merge pull request #53 from acadet/master
Browse files Browse the repository at this point in the history
Computes Murmur's height dynamically
  • Loading branch information
RamonGilabert committed Jan 28, 2016
2 parents 77314a3 + 3418f9a commit dc4fe41
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions Source/WhistleFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ public func Whistle(murmur: Murmur) {

public class WhistleFactory: UIViewController {

public struct Dimensions {
public static let height: CGFloat = 20
}

public lazy var whistleWindow: UIWindow = UIWindow()

public lazy var titleLabelHeight = CGFloat(20.0)

public lazy var titleLabel: UILabel = {
let label = UILabel()
Expand Down Expand Up @@ -74,10 +72,29 @@ public class WhistleFactory: UIViewController {
}

public func setupFrames() {
titleLabel.sizeToFit()
let labelWidth = UIScreen.mainScreen().bounds.width
let defaultHeight = titleLabelHeight

if let text = titleLabel.text {
let neededDimensions =
NSString(string: text).boundingRectWithSize(
CGSize(width: labelWidth, height: CGFloat.infinity),
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: [NSFontAttributeName: titleLabel.font],
context: nil
)
titleLabelHeight = CGFloat(neededDimensions.size.height)
titleLabel.numberOfLines = 0 // Allows unwrapping

if titleLabelHeight < defaultHeight {
titleLabelHeight = defaultHeight
}
} else {
titleLabel.sizeToFit()
}

whistleWindow.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width,
height: Dimensions.height)
whistleWindow.frame = CGRect(x: 0, y: 0, width: labelWidth,
height: titleLabelHeight)
view.frame = whistleWindow.bounds
titleLabel.frame = view.bounds
}
Expand All @@ -88,7 +105,7 @@ public class WhistleFactory: UIViewController {
hideTimer.invalidate()

let initialOrigin = whistleWindow.frame.origin.y
whistleWindow.frame.origin.y = initialOrigin - Dimensions.height
whistleWindow.frame.origin.y = initialOrigin - titleLabelHeight
whistleWindow.makeKeyAndVisible()
UIView.animateWithDuration(0.2, animations: {
self.whistleWindow.frame.origin.y = initialOrigin
Expand All @@ -98,7 +115,7 @@ public class WhistleFactory: UIViewController {
}

public func hide() {
let finalOrigin = view.frame.origin.y - Dimensions.height
let finalOrigin = view.frame.origin.y - titleLabelHeight
UIView.animateWithDuration(0.2, animations: {
self.whistleWindow.frame.origin.y = finalOrigin
}, completion: { _ in
Expand Down

0 comments on commit dc4fe41

Please sign in to comment.