An Angular2+ directive that autoscale the font size of an element until it fit the upper level container dimension.
Find a file
Lorenzo Iovino c308b800df up
2017-05-22 12:31:08 +02:00
src up 2017-05-22 12:31:08 +02:00
.gitignore first commit - the initial version of ng2-fittext, not best but good 2017-03-14 13:46:18 +01:00
.npmignore Added file .ts in the publish 2017-03-17 01:03:12 +01:00
index.ts Exposed as module 2017-03-16 23:03:48 +01:00
ng2fittext.ts Exposed as module 2017-03-16 23:03:48 +01:00
package.json version 1.0.15 2017-05-22 12:29:41 +02:00
README.md Release version 1.0.14 2017-05-19 13:29:05 +02:00
tsconfig.json Exposed as module 2017-03-16 23:03:48 +01:00

ng2-fittext

An Angular2 directive written in pure typescript (and without jquery!), for autoscale the font size of an element to fit an upper level container.

How many times your font doesn't scale automatically to fit the size of the container? Always, if you don't say it to do that! How you can say it? ng2-fittext!

Demo

http://plnkr.co/edit/v0TQaYepV4E2Heur02j5?p=preview

Installation

Install the library

$ npm install --save ng2-fittext

Usage

Import it in your Angular2 project like a module

  1. Declare it in your module

    import {Ng2FittextModule} from "ng2-fittext/ng2fittext";
    @NgModule({
      imports: [
        Ng2FittextModule
      ]
    })
    
    
  2. Use it in your components

    import {Component} from '@angular/core';
    
    @Component({
      selector: 'label',
      template: `<div #container>
                    <div [fittext]="true" [activateOnResize]="true" [container]="container">Bla bla bla...</div>
                </div>`
    })
    
    export class LabelComponent {}
    

    NEW! Support for autoresize input box!

    import {Component} from '@angular/core';
    
    @Component({
      selector: 'inputbox',
      template: `<div #container>
                    <input [fittext]="true" [activateOnResize]="true" [activateOnInputEvents]="true" [container]="container">`,
                </div>`
    })
    
    export class InputBoxComponent {}
    

    Parameters:

Parameter Description Values
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)
activateOnInputEvents enbale/disable the autofit in case of input box events (keydown, keyup etc..) true or false (default false)

Development

Want to contribute? Great! Simply, clone the repository!

I created this library because I always spended too much time for solve this problem and because i didn't find nothing on the web (13/03/2017) that do this without jquery and easily integrable in angular2. For sure is not a good implementation, maybe is not the best way to do it, but, it do the job.

Todos

  • Write tests
  • Find a better algorithm to find the font-size who fits better the container

License

ISC

Lorenzo I.