font-adaption.vue 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. <template>
  2. <span :style="{ color, fontSize }">{{ font }}</span>
  3. </template>
  4. <script lang="ts">
  5. import { defineComponent, computed } from 'vue';
  6. export default defineComponent({
  7. name: 'font-adaption',
  8. props: {
  9. font: {
  10. type: String,
  11. defaule: '',
  12. },
  13. color: {
  14. type: String,
  15. defaule: '#2D6783',
  16. },
  17. section: {
  18. type: Array,
  19. defaule: [[0, , '12px']], // [[区间开始长度,区间结束长度,字体大小]]
  20. },
  21. },
  22. setup(props) {
  23. const fontSize = computed(() => {
  24. return (props.section?.length && ((props.section as [[number, number | undefined, string]])?.find((ary) => (props.font as String).toString().length >= ary[0] && (ary[1] === undefined || (props.font as String).toString().length <= ary[1])) as [number, number | undefined, string])[2]) || '12px';
  25. });
  26. return {
  27. fontSize,
  28. };
  29. },
  30. });
  31. </script>
  32. <style lang="scss" scoped></style>