An Angular2+ directive that autoscale the font size of an element until it fit the upper level container dimension.
Find a file
dependabot[bot] 3deb9fc7fa Bump mixin-deep from 1.3.1 to 1.3.2
Bumps [mixin-deep](https://github.com/jonschlinkert/mixin-deep) from 1.3.1 to 1.3.2.
- [Release notes](https://github.com/jonschlinkert/mixin-deep/releases)
- [Commits](https://github.com/jonschlinkert/mixin-deep/compare/1.3.1...1.3.2)

Signed-off-by: dependabot[bot] <support@github.com>
2019-11-05 09:24:17 +00:00
.idea Version upgrade 1.2.8 2019-11-05 10:22:39 +01:00
examples/dev Fix: added deprecated useMaxFontSize to prevent broken builds 2019-05-06 12:48:31 +02:00
src Output new font size when it changes 2019-10-21 13:03:54 +02:00
.gitignore Added angular-librarian for manage library 2017-11-14 11:07:08 +01:00
.npmignore Added angular-librarian for manage library 2017-11-14 11:07:08 +01:00
index.ts Added angular-librarian for manage library 2017-11-14 11:07:08 +01:00
LICENSE Create LICENSE 2019-05-09 11:58:13 +02:00
package-lock.json Bump mixin-deep from 1.3.1 to 1.3.2 2019-11-05 09:24:17 +00:00
package.json Version upgrade 1.2.8 2019-11-05 10:22:39 +01:00
publishPackage.json Version upgrade 1.2.8 2019-11-05 10:22:39 +01:00
README.md Version upgrade 1.2.8 2019-11-05 10:22:39 +01:00
tsconfig.json Fix: Issues #14,#15,#19,#20. Ref: Deleted angular-librarian and added scripts in package.json to manage publishing; Version upgrade 2019-02-05 01:42:22 +01:00
tslint.json Added angular-librarian for manage library 2017-11-14 11:07:08 +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";
    @NgModule({
      imports: [
        Ng2FittextModule
      ]
    })
    
    
  2. Use it in your components

    Fit with specified container (can be the parent or a deeper ancestor)

    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 {}
    

    Fit with the parent element (this works if you have a variable number of element between element and parent)

    import {Component} from '@angular/core';
    
    @Component({
      selector: 'label',
      template: `<div>
                    <div [fittext]="true" [activateOnResize]="true">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" [container]="container" [activateOnInputEvents]="true">`,
                </div>`
    })
    
    export class InputBoxComponent {}
    

    NEW! Support for maxFontSize!

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

    Input Parameters:

Parameter Description Values
fittext is the selector of the directive true/false
container the container to fit (if not present it fit default to parent container) 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)
maxFontSize maximal font size number, default is 1000
!deprecated! useMaxFontSize use max font size if is true deprecated!
minFontSize minimal font size number, default is 7
modelToWatch pass model to watch, when this model changes -> font size is automatically recalculated any type of model

fontSizeChanged

Output Parameters:

Parameter Description Values
fontSizeChanged current font size string

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

MIT

Lorenzo I.