Skip to content

Instantly share code, notes, and snippets.

@MacDada
Created December 3, 2012 17:37
Show Gist options
  • Save MacDada/4196618 to your computer and use it in GitHub Desktop.
Save MacDada/4196618 to your computer and use it in GitHub Desktop.
/**
* Mierzy wysokość tekstu na podstawie specyfikacji css: font.
* Przykład: MMG.getTextHeight("italic bold 13em Arial");
*
* @param string font CSSowa specyfikacja czcionki
* @return integer Wysokość linii tekstu w pikselach
*/
MMG.getTextHeight = function(font) {
var text = $('<span style="font: ' + font + '">Hg</span>');
var block = $('<div style="display: inline-block; width: 1px; height: 0px;"></div>');
var div = $('<div></div>');
div.append(text, block);
var body = $('body');
body.append(div);
try {
var result = {};
block.css({ verticalAlign: 'baseline' });
result.ascent = block.offset().top - text.offset().top;
block.css({ verticalAlign: 'bottom' });
result.height = block.offset().top - text.offset().top;
result.descent = result.height - result.height;
} finally {
div.remove();
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment