Skip to content

Commit

Permalink
Merge pull request Khan#46 from saniul/text-alignment-resizing
Browse files Browse the repository at this point in the history
[FIN] TextLayer.textAlignment should affect the way the text layer grows
  • Loading branch information
andymatuschak committed Mar 6, 2015
2 parents 54b52fa + 55c3664 commit 5300f79
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions Prototope/TextLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,21 @@ public class TextLayer: Layer {
}
set {
label.text = newValue
updateSizePreservingOrigin()
updateSize()
}
}

public var fontName: String = "Futura" {
didSet {
updateFont()
updateSizePreservingOrigin()
updateSize()
}
}

public var fontSize: Double = 16 {
didSet {
updateFont()
updateSizePreservingOrigin()
updateSize()
}
}

Expand All @@ -92,7 +92,7 @@ public class TextLayer: Layer {
}
set {
label.numberOfLines = newValue ? 0 : 1
updateSizePreservingOrigin() // Adjust width/height as necessary for new wrapping mode.
updateSize() // Adjust width/height as necessary for new wrapping mode.
}
}

Expand Down Expand Up @@ -122,7 +122,7 @@ public class TextLayer: Layer {
didSet {
// Respect the new width; resize height so as not to truncate.
if wraps {
updateSizePreservingOrigin()
updateSize()
}
}
}
Expand All @@ -140,14 +140,33 @@ public class TextLayer: Layer {

private func updateFont() {
label.font = UIFont(name: fontName, size: CGFloat(fontSize))!
updateSizePreservingOrigin()
updateSize()
}

private func updateSizePreservingOrigin() {
private func updateSize() {
let prePoint = self.sizeUpdatePivotPoint()
label.sizeToFit()
}

private var label: UILabel {
let postPoint = self.sizeUpdatePivotPoint()
let delta = postPoint - prePoint
self.position -= delta
}

func sizeUpdatePivotPoint() -> Point {
switch self.textAlignment {
case .Natural: // assume left for .Natural
fallthrough
case .Justified:
fallthrough
case .Left:
return Point(x: self.frame.minX, y: self.frame.minY)
case .Right:
return Point(x: self.frame.maxX, y: self.frame.minY)
case .Center:
return Point(x: self.frame.midX, y: self.frame.minY)
}
}

private var label: UILabel {
return self.view as! UILabel
}

Expand Down

0 comments on commit 5300f79

Please sign in to comment.