Skip to content

Commit

Permalink
Add option to preserve original line-height
Browse files Browse the repository at this point in the history
This comes in handy when you have several aligned elements of different lengths, that will be resized to different font-sizes.
Setting back the line-height to the original font-size helps keeping such elements aligned.
  • Loading branch information
trumbitta committed Jun 20, 2015
1 parent 6d60c52 commit 9ea6771
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/ng-FitText.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
'delay': 250,
'loadDelay': 10,
'min': undefined,
'max': undefined
'max': undefined,
'preserveLineHeight': undefined
})

.directive('fittext', ['$timeout', 'config', 'fitTextConfig', function($timeout, config, fitTextConfig) {
Expand All @@ -31,16 +32,18 @@
angular.extend(config, fitTextConfig.config);

element[0].style.display = 'inline-block';
element[0].style.lineHeight = '1';

var parent = element.parent();
var compressor = attrs.fittext || 1;
var loadDelay = attrs.fittextLoadDelay || config.loadDelay;
var nl = element[0].querySelectorAll('[fittext-nl],[data-fittext-nl]').length || 1;
var minFontSize = attrs.fittextMin || config.min || Number.NEGATIVE_INFINITY;
var maxFontSize = attrs.fittextMax || config.max || Number.POSITIVE_INFINITY;
var preserveLineHeight = attrs.fittextPreserveLineHeight !== undefined ? attrs.fittextPreserveLineHeight : config.preserveLineHeight;
var originalFontSize = window.getComputedStyle(element[0],null).getPropertyValue('font-size');

var resizer = function() {
element[0].style.lineHeight = '1';
element[0].style.fontSize = '10px';
var ratio = element[0].offsetHeight / element[0].offsetWidth / nl;
element[0].style.fontSize = Math.max(
Expand All @@ -49,6 +52,9 @@
),
parseFloat(minFontSize)
) + 'px';
if ( preserveLineHeight !== undefined ) {
element[0].style.lineHeight = originalFontSize;
}
};

$timeout( function() { resizer() }, loadDelay);
Expand All @@ -73,4 +79,4 @@
return this;
});

})(window, document, angular);
})(window, document, angular);

0 comments on commit 9ea6771

Please sign in to comment.