Permalink
Browse files

auto sizing now takes borders and padding into account

  • Loading branch information...
nvh committed Mar 9, 2017
1 parent 31c72c4 commit fc554956de5de7c5bfc906434c68937c925257e4
Showing with 46 additions and 4 deletions.
  1. +11 −4 framer/TextLayer.coffee
  2. +35 −0 test/tests/TextLayerTest.coffee
View
@@ -21,6 +21,8 @@ class exports.TextLayer extends Layer
"textDecoration"
"direction"
"font"
"borderWidth"
"padding"
]
@_textStyleProperties = _.pull(_.clone(TextLayer._textProperties), "text").concat(["color", "shadowX", "shadowY", "shadowBlur", "shadowColor"])
@@ -74,18 +76,23 @@ class exports.TextLayer extends Layer
autoSize: =>
constraints =
max: true
borderOffset = @borderWidth * 2
parentBorder = (@parent?.borderWidth ? 0) * 2
if @explicitWidth
constraints.width = @width
else
constraints.width = if @parent? then @parent.width else Screen.width
constraints.width = if @parent? then @parent.width - borderOffset - parentBorder - @padding.left - @padding.right else Screen.width
style = _.pick @style, @constructor._textProperties
size = Utils.textSize(@text, style, constraints)
newWidth = Math.ceil(size.width)
newHeight = Math.ceil(size.height)
@disableExplicitUpdating = true
@width = newWidth if @width isnt newWidth and not @explicitWidth
@height = newHeight if @height isnt newHeight and not @explicitHeight
if not @explicitWidth
newWidth += borderOffset
@width = newWidth if @width isnt newWidth
if not @explicitHeight
newHeight += borderOffset
@height = newHeight if @height isnt newHeight
@disableExplicitUpdating = false
@@ -106,6 +106,41 @@ describe "TextLayer", ->
done()
text.text = longText
it "should take padding into account", ->
text = new TextLayer
fontFamily: "Courier, Liberation Mono"
text: shortText
padding: 10
text.size.should.eql width: 332, height: 70
it "should take border width into account", ->
text = new TextLayer
fontFamily: "Courier, Liberation Mono"
text: shortText
borderWidth: 5
text.size.should.eql width: 322, height: 60
it "should autosize with the right width when inside a parent", ->
text = new TextLayer
fontFamily: "Courier, Liberation Mono"
text: shortText
borderWidth: 5
padding: 3
parent = new Layer
text.parent = parent
text.size.should.eql width: 100, height: 116
it "should autosize with the right width when inside a parent with a border", ->
text = new TextLayer
fontFamily: "Courier, Liberation Mono"
text: shortText
borderWidth: 5
padding: 3
parent = new Layer
borderWidth: 10
text.parent = parent
text.size.should.eql width: 80, height: 116
describe "Padding", ->
it "should have no padding initially", ->
text = new TextLayer

0 comments on commit fc55495

Please sign in to comment.