This commit is contained in:
Thierry Nischelwitzer 2017-06-19 04:58:05 +00:00 committed by GitHub
commit b0ea25e793
2 changed files with 39 additions and 34 deletions

View file

@ -28,20 +28,20 @@ Import it in your Angular2 project like a module
Ng2FittextModule Ng2FittextModule
] ]
}) })
``` ```
2) Use it in your components 2) Use it in your components
```sh ```sh
import {Component} from '@angular/core'; import {Component} from '@angular/core';
@Component({ @Component({
selector: 'label', selector: 'label',
template: `<div #container> template: `<div>
<div [fittext]="true" [activateOnResize]="true" [container]="container">Bla bla bla...</div> <div [fittext]="true" [activateOnResize]="true" [useMaxFontSize]="false">Bla bla bla...</div>
</div>` </div>`
}) })
export class LabelComponent {} export class LabelComponent {}
``` ```
@ -49,25 +49,25 @@ Import it in your Angular2 project like a module
```sh ```sh
import {Component} from '@angular/core'; import {Component} from '@angular/core';
@Component({ @Component({
selector: 'inputbox', selector: 'inputbox',
template: `<div #container> template: `<div>
<input [fittext]="true" [activateOnResize]="true" [activateOnInputEvents]="true" [container]="container">`, <input [fittext]="true" [activateOnResize]="true" [activateOnInputEvents]="true" [useMaxFontSize]="false">`,
</div>` </div>`
}) })
export class InputBoxComponent {} export class InputBoxComponent {}
``` ```
Parameters: Parameters:
| Parameter | Description | Values | | Parameter | Description | Values |
| --- | --- | --- | | --- | --- | --- |
| fittext | is the selector of the directive | true/false | fittext | is the selector of the directive | true/false
| container | the container to fit | ElementRef
| activateOnResize | enable/disable the autofit in case of window resize | true or false (default false) | activateOnResize | enable/disable the autofit in case of window resize | true or false (default false)
| activateOnInputEvents | enbale/disable the autofit in case of input box events (keydown, keyup etc..) | true or false (default false) | activateOnInputEvents | enbale/disable the autofit in case of input box events (keydown, keyup etc..) | true or false (default false)
| useMaxFontSize | Use font-size from element as maximum font-size | enable/disable the usage of max font-size of the lement
### Development ### Development
@ -90,4 +90,4 @@ ISC
**Lorenzo I.** **Lorenzo I.**

View file

@ -3,12 +3,13 @@ import {Directive, ElementRef, Renderer, Input, AfterViewInit, HostListener, OnI
@Directive({ @Directive({
selector: '[fittext]' selector: '[fittext]'
}) })
export class Ng2FittextDirective implements AfterViewInit, OnInit { export class FittextDirective implements AfterViewInit, OnInit {
@Input('fittext') fittext: any; @Input('fittext') fittext: any;
@Input('container') container: any;
@Input('activateOnResize') activateOnResize: boolean; @Input('activateOnResize') activateOnResize: boolean;
@Input('activateOnInputEvents') activateOnInputEvents: boolean; @Input('activateOnInputEvents') activateOnInputEvents: boolean;
@Input('useMaxFontSize') useMaxFontSize: boolean;
private maxFontSize: number = 1000;
private fontSize: number = 0; private fontSize: number = 0;
private speed: number = 1.05; private speed: number = 1.05;
@ -16,16 +17,20 @@ export class Ng2FittextDirective implements AfterViewInit, OnInit {
} }
setFontSize(fontSize) { setFontSize(fontSize) {
console.log();
this.fontSize = fontSize; this.fontSize = fontSize;
return this.el.nativeElement.style.setProperty('font-size', (fontSize).toString()+'px'); return this.el.nativeElement.style.setProperty('font-size', (fontSize).toString() + 'px');
} }
calculateFontSize(fontSize, speed){ calculateFontSize(fontSize, speed) {
// TODO Do with Gauss // TODO Do with Gauss
return Math.floor(fontSize/speed); return Math.floor(fontSize / speed);
} }
checkOverflow(parent:any, children:any) { checkOverflow(children: any) {
const parent = children.parentElement;
let overflowX = children.scrollWidth - parent.clientWidth; let overflowX = children.scrollWidth - parent.clientWidth;
let overflowY = children.clientHeight - parent.clientHeight; let overflowY = children.clientHeight - parent.clientHeight;
return (overflowX > 1 || overflowY > 1); return (overflowX > 1 || overflowY > 1);
@ -33,11 +38,11 @@ export class Ng2FittextDirective implements AfterViewInit, OnInit {
@HostListener('window:resize', ['$event']) @HostListener('window:resize', ['$event'])
onResize() { onResize() {
if(this.activateOnResize && this.fittext) { if (this.activateOnResize && this.fittext) {
if(this.activateOnInputEvents && this.fittext) { if (this.activateOnInputEvents && this.fittext) {
this.setFontSize(this.container.clientHeight); this.setFontSize(this.el.nativeElement.parentElement.clientHeight);
} else{ } else {
this.setFontSize(this.container.clientWidth); this.setFontSize(this.el.nativeElement.parentElement.clientWidth);
} }
this.ngAfterViewInit(); this.ngAfterViewInit();
} }
@ -45,30 +50,30 @@ export class Ng2FittextDirective implements AfterViewInit, OnInit {
@HostListener('input', ['$event']) @HostListener('input', ['$event'])
onInputEvents() { onInputEvents() {
if(this.activateOnInputEvents && this.fittext) { if (this.activateOnInputEvents && this.fittext) {
this.setFontSize(this.container.clientHeight); this.setFontSize(this.el.nativeElement.parentElement.clientHeight);
this.ngAfterViewInit(); this.ngAfterViewInit();
} }
} }
ngOnInit() { ngOnInit() {
if (this.useMaxFontSize) {
this.maxFontSize = parseInt(window.getComputedStyle(this.el.nativeElement).fontSize, null);
}
if (this.fittext) { if (this.fittext) {
if(this.activateOnInputEvents) { this.setFontSize(this.maxFontSize);
this.setFontSize(this.container.clientHeight);
} else {
this.setFontSize(this.container.clientWidth);
}
} }
this.el.nativeElement.style.setProperty('will-change', 'content'); this.el.nativeElement.style.setProperty('will-change', 'content');
} }
ngAfterViewInit() { ngAfterViewInit() {
if (this.fittext) { if (this.fittext) {
let overflow = this.checkOverflow(this.container, this.el.nativeElement); let overflow = this.checkOverflow(this.el.nativeElement);
if(overflow) { if (overflow) {
this.setFontSize(this.calculateFontSize(this.fontSize, this.speed)); this.setFontSize(this.calculateFontSize(this.fontSize, this.speed));
this.ngAfterViewInit(); this.ngAfterViewInit();
} }
} }
} }
} }