-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
152 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { DOCUMENT } from '@angular/common'; | ||
import { | ||
Directive, | ||
ElementRef, | ||
Renderer2, | ||
AfterViewInit, | ||
Inject, | ||
} from '@angular/core'; | ||
|
||
@Directive({ | ||
selector: '[appAdjustFontSize]', | ||
}) | ||
export class AdjustFontSizeDirective implements AfterViewInit { | ||
constructor( | ||
@Inject(DOCUMENT) private document: Document, | ||
private elementRef: ElementRef, | ||
private renderer: Renderer2 | ||
) {} | ||
|
||
ngAfterViewInit(): void { | ||
const title = this.elementRef.nativeElement as HTMLElement; | ||
if (title) { | ||
this.adjustFontSize(title); | ||
} else { | ||
console.log('Titles are not yet initialized'); | ||
} | ||
} | ||
|
||
adjustFontSize(element: HTMLElement) { | ||
console.log('I am here !'); | ||
const defaultView = this.document.defaultView; | ||
|
||
if (defaultView) { | ||
const computedStyle = defaultView.getComputedStyle(element); | ||
|
||
if (computedStyle) { | ||
const lineHeight = parseFloat(computedStyle.lineHeight); | ||
const maxLines = 3; | ||
|
||
const textHeight = element.scrollHeight; | ||
const numberOfLines = Math.ceil(textHeight / lineHeight); | ||
|
||
if (numberOfLines > maxLines) { | ||
const fontSize = parseFloat(computedStyle.fontSize); | ||
const newFontSize = (fontSize * maxLines) / numberOfLines; | ||
|
||
element.style.fontSize = newFontSize + 'px'; | ||
} else { | ||
var textLength = element.textContent?.length ?? 0; | ||
var fontSizeMax = 2.8; | ||
var fontSizeMin = 1.5; | ||
var fontSize = fontSizeMax; | ||
if (textLength > 6) { | ||
fontSize = fontSizeMax - 0.05 * textLength; | ||
if (fontSize < fontSizeMin) { | ||
fontSize = fontSizeMin; | ||
} | ||
} | ||
var fontSizeFinal = fontSize + 'rem'; | ||
element.style.fontSize = fontSizeFinal; | ||
} | ||
} | ||
} | ||
} | ||
} |