Skip to content

Instantly share code, notes, and snippets.

@bttmly
Last active December 31, 2015 05:19
Show Gist options
  • Save bttmly/7940429 to your computer and use it in GitHub Desktop.
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
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 )
.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;
}
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);
});
.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