From 9d15ae732f428b5aa7c5e09a681d455305346497 Mon Sep 17 00:00:00 2001 From: Lorenzo Iovino Date: Tue, 14 Mar 2017 18:33:01 +0100 Subject: [PATCH] Resolved long word break fit --- README.md | 1 - src/fittext.directive.ts | 9 ++++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ec3ba46..5d9f4ef 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,6 @@ For sure is not a good implementation, maybe is not the best way to do it, but, ### Todos - Write tests - - Resolve the problem with long word checking the overflowY - Find a better algorithm to find the font-size who fits better the container License diff --git a/src/fittext.directive.ts b/src/fittext.directive.ts index e703b2d..561ab4a 100644 --- a/src/fittext.directive.ts +++ b/src/fittext.directive.ts @@ -14,8 +14,8 @@ export class FittextDirective implements AfterViewInit { constructor(public el: ElementRef, public renderer: Renderer) { } - checkOverflowX(parent:any, children:any) { - return children.clientHeight > parent.clientHeight; + checkOverflow(parent:any, children:any) { + return (children.clientHeight > parent.clientHeight || children.scrollWidth > parent.clientWidth); } @HostListener('window:resize', ['$event']) @@ -32,8 +32,11 @@ export class FittextDirective implements AfterViewInit { this.fontSize = this.container.clientWidth; this.el.nativeElement.style.setProperty('font-size', (this.fontSize).toString()+'px'); } - let overflow = this.checkOverflowX(this.container, this.el.nativeElement); + let overflow = this.checkOverflow(this.container, this.el.nativeElement); if(overflow) { + if(this.fontSize < 2) { + return; + } this.fontSize = Math.floor(this.fontSize/this.speed); this.el.nativeElement.style.setProperty('font-size', (this.fontSize).toString()+'px'); this.ngAfterViewInit();