Last active
December 31, 2015 05:19
-
-
Save bttmly/7940429 to your computer and use it in GitHub Desktop.
SASS + CoffeeScript snippets for estimating text measure. Useful for development. Make sure you set a line-height, since it defaults to "normal" which will give you NaN. Based on http://stackoverflow.com/a/14376445/2942909
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getTextMeasure = ( $elements ) -> | |
$elements.each -> | |
numLines = $( this ).height() / $( this ).css("line-height").replace( "px", "" ) | |
textLength = $( this ).text().length | |
$( this ).data("text-measure", textLength / numLines ) | |
$textMeasure = $( ".text-measure" ) | |
$( document ).ready -> | |
getTextMeasure( $textMeasure ) | |
$( window ).resize -> | |
getText( $textMeasure ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.text-measure { | |
position: relative; | |
} | |
.text-measure:after { | |
content: attr(data-text-measure); | |
position: absolute; | |
top: 0; | |
right: 0; | |
width: 4em; | |
height: 4em; | |
line-height: 4em; | |
text-align: center; | |
background: white; | |
box-shadow: 0 0 4px rgba(0, 0, 0, 0.5); | |
color: tomato; | |
font-weight: bold; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var $textMeasure, getTextMeasure; | |
getTextMeasure = function($elements) { | |
return $elements.each(function() { | |
var numLines, textLength; | |
numLines = $(this).height() / $(this).css("line-height").replace("px", ""); | |
textLength = $(this).text().length; | |
return $(this).data("text-measure", textLength / numLines); | |
}); | |
}; | |
$textMeasure = $(".text-measure"); | |
$(document).ready(function() { | |
return getTextMeasure($textMeasure); | |
}); | |
$(window).resize(function() { | |
return getText($textMeasure); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.text-measure | |
position: relative | |
&:after | |
content: attr(data-text-measure) | |
position: absolute | |
top: 0 | |
right: 0 | |
width: 4em | |
height: 4em | |
line-height: 4em | |
text-align: center | |
background: white | |
box-shadow: 0 0 4px rgba(0, 0, 0, .5) | |
color: tomato | |
font-weight: bold | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment