Skip to content

Instantly share code, notes, and snippets.

@juzeon
Created January 17, 2022 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juzeon/35c47d738e658fe837734d8b42538bf2 to your computer and use it in GitHub Desktop.
Save juzeon/35c47d738e658fe837734d8b42538bf2 to your computer and use it in GitHub Desktop.
Auto font-resizing solution for vue
<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