Created
January 17, 2022 04:11
-
-
Save juzeon/35c47d738e658fe837734d8b42538bf2 to your computer and use it in GitHub Desktop.
Auto font-resizing solution for vue
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
<template> | |
<div :style="'height: '+containerHeight" ref="container" class="d-flex justify-center align-center" | |
v-intersect="resizeFont"> | |
<div ref="text" v-html="textHtml" style="word-break: break-all;line-height: normal;"> | |
</div> | |
</div> | |
</template> | |
<script> | |
export default { | |
name: "AutoFont", | |
props: ['containerHeight', 'defaultFontSize', 'textHtml'], | |
mounted() { | |
this.resizeFont() | |
}, | |
updated() { | |
this.resizeFont() | |
}, | |
methods: { | |
resizeFont() { | |
let containerEl, textEl | |
containerEl = this.$refs.container | |
textEl = this.$refs.text | |
textEl.style.fontSize = this.defaultFontSize + 'px' | |
let fontSize = this.defaultFontSize | |
for (let i = 0; i < 100; i++) { | |
if (containerEl.clientHeight < textEl.clientHeight) { | |
fontSize-- | |
textEl.style.fontSize = fontSize + 'px' | |
} else { | |
break | |
} | |
} | |
}, | |
} | |
} | |
</script> | |
<style scoped> | |
img{ | |
max-height: 250px; | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment