Text.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <template>
  2. <text :id="id" :class="innerClass" :style="style" @click="onClick">
  3. <!-- #ifdef APP-NVUE -->
  4. {{ text }}
  5. <!-- #endif -->
  6. <!-- #ifndef APP-NVUE -->
  7. <slot>{{ text }}</slot>
  8. <!-- #endif -->
  9. </text>
  10. </template>
  11. <script lang="ts" setup>
  12. import { computed, getCurrentInstance } from 'vue';
  13. import { useTheme, type ThemePaddingMargin } from '../theme/ThemeDefine';
  14. import { RandomUtils } from '@imengyu/imengyu-utils';
  15. export interface TextProps {
  16. /**
  17. * 字体颜色。可以是颜色字符串或者在主题中配置的预设名称。
  18. */
  19. color?: string,
  20. /**
  21. * 文字阴影颜色。可以是颜色字符串或者在主题中配置的预设名称。
  22. */
  23. shadowColor?: string,
  24. /**
  25. * 文字对齐方式。
  26. */
  27. textAlign?: 'center'|'left'|'right'|'',
  28. /**
  29. * 字体预设。可以是主题中预设的一个名称。
  30. */
  31. fontConfig?: string,
  32. /**
  33. * 字体大小。可以是实际数值或者在主题中配置的预设名称。
  34. */
  35. fontSize?: string|number,
  36. /**
  37. * 字体名称。
  38. */
  39. fontFamily?: string,
  40. /**
  41. * 字体样式。
  42. */
  43. fontStyle?: string,
  44. /**
  45. * 字体粗细。
  46. */
  47. fontWeight?: string|number,
  48. /**
  49. * 是否是粗体
  50. */
  51. bold?: boolean,
  52. /**
  53. * 是否是斜体
  54. */
  55. italic?: boolean,
  56. /**
  57. * 是否加下划线
  58. */
  59. underline?: boolean,
  60. /**
  61. * 是否加删除线
  62. */
  63. lineThrough?: boolean,
  64. /**
  65. * 是否有阴影。
  66. */
  67. shadow?: boolean,
  68. /**
  69. * 背景颜色。可以是颜色字符串或者在主题中配置的预设名称。
  70. */
  71. backgroundColor?: string,
  72. /**
  73. * 是否可以选择
  74. */
  75. selectable?: boolean,
  76. /**
  77. * 是否允许换行
  78. * @default true
  79. */
  80. wrap?: boolean,
  81. /**
  82. * 行数限制
  83. */
  84. lines?: number,
  85. margin?: number|string|number[]|ThemePaddingMargin,
  86. padding?: number|string|number[]|ThemePaddingMargin,
  87. innerStyle?: object,
  88. innerClass?: object|string,
  89. /**
  90. * 最大宽度
  91. */
  92. maxWidth?: number|string,
  93. /**
  94. * 自动工具文字长短设置大小,在 autoSize 为 true 时有效。
  95. *
  96. * 这个是一个小功能,目的是为了在某些情况下(例如金额显示),容器宽度一定但是文字长短不定,
  97. * 此时需要自动缩放大小,文字越长,字号越小。
  98. *
  99. * 计算公式是 (1 - (text.length - minLen) / (maxLen - minLen)) * (maxSize - minSize) + minSize
  100. */
  101. autoSize?: {
  102. /**
  103. * 小程序无法获取模板内容,需要额外提供字符串
  104. */
  105. text?: string,
  106. /**
  107. * 最长文字长度,用于自动大小公式计算
  108. */
  109. maxLen: number;
  110. /**
  111. * 最短文字长度,如果输入文字长度小于这个值,则不会进行自动缩放
  112. */
  113. minLen: number;
  114. /**
  115. * 最大文字字号,用于自动大小公式计算
  116. */
  117. maxSize: number;
  118. /**
  119. * 最小文字字号,用于自动大小公式计算
  120. */
  121. minSize: number;
  122. },
  123. text?: string|number,
  124. /**
  125. * 是否可以点击
  126. */
  127. touchable?: boolean,
  128. }
  129. /**
  130. * 组件说明:文字封装,支持点击事件,颜色,阴影。
  131. */
  132. const props = withDefaults(defineProps<TextProps>(), {
  133. shadowColor: '#000',
  134. bold: false,
  135. italic: false,
  136. underline: false,
  137. lineThrough: false,
  138. shadow: false,
  139. touchable: false,
  140. wrap: true,
  141. });
  142. const emit = defineEmits([ 'click' ])
  143. const id = `text-${RandomUtils.genNonDuplicateID(12)}`;
  144. const instance = getCurrentInstance();
  145. const { resolveThemeColor, resolveThemeSize, getText } = useTheme();
  146. function getAutoSize() {
  147. //自动缩放大小,文字越长,字号越小
  148. const autoSizeOption = props.autoSize;
  149. if (autoSizeOption) {
  150. const text = '' + autoSizeOption.text;
  151. if (text.length < autoSizeOption.minLen)
  152. return props.fontSize;
  153. return (1 - (text.length - autoSizeOption.minLen) / (autoSizeOption.maxLen - autoSizeOption.minLen))
  154. * (autoSizeOption.maxSize - autoSizeOption.minSize) + autoSizeOption.minSize;
  155. }
  156. }
  157. function onClick(e: any) {
  158. if (props.touchable) {
  159. emit("click", e)
  160. }
  161. }
  162. const style = computed(() => {
  163. const o : Record<string, any> = {
  164. }
  165. const style = props.fontConfig ? getText(props.fontConfig) : undefined;
  166. const color = props.color ?? style?.color as string;
  167. const backgroundColor = props.backgroundColor ?? style?.backgroundColor as string;
  168. const fontSize = props.fontSize ?? style?.fontSize as string;
  169. const fontWeight = props.fontWeight ?? style?.fontWeight as string;
  170. const fontFamily = props.fontFamily ?? style?.fontFamily as string;
  171. const fontStyle = props.fontStyle ?? style?.fontStyle as string;
  172. if (color) o.color = resolveThemeColor(color, "text");
  173. if (backgroundColor) o.background = resolveThemeColor(backgroundColor);
  174. if (fontSize) o.fontSize = resolveThemeSize(fontSize);
  175. if (fontWeight) o.fontWeight = fontWeight;
  176. if (fontStyle) o.fontStyle = fontStyle;
  177. if (!props.wrap) o.whiteSpace = 'nowrap';
  178. if (fontFamily) o.fontFamily = fontFamily;
  179. if (props.textAlign) {
  180. o.textAlign = props.textAlign;
  181. }
  182. if (props.selectable) o.userSelect = props.selectable;
  183. if (props.lines) {
  184. o.display = '-webkit-box';
  185. o['-webkit-line-clamp'] = props.lines;
  186. o['-webkit-box-orient'] = 'vertical';
  187. o.overflow = 'hidden';
  188. o.textOverflow = 'ellipsis';
  189. }
  190. if (props.bold)
  191. o.fontWeight = 'bold';
  192. if (props.italic)
  193. o.fontStyle = 'italic';
  194. if ((props.underline || props.lineThrough) && !o.textDecoration)
  195. o.textDecoration = '';
  196. if (props.underline)
  197. o.textDecoration += ' underline';
  198. if (props.lineThrough)
  199. o.textDecoration += ' line-through';
  200. if (props.maxWidth)
  201. o.maxWidth = resolveThemeSize(props.maxWidth);
  202. if (props.shadow && props.shadowColor) {
  203. o.textShadow = '1px 1px 2px ' + resolveThemeColor(props.shadowColor, 'text');
  204. }
  205. if (props.autoSize) {
  206. const s = getAutoSize();
  207. if (s)
  208. o.fontSize = s + 'rpx';
  209. }
  210. const rs = {
  211. ...o,
  212. ...props.innerStyle
  213. };
  214. return rs;
  215. })
  216. async function measureTextWidth() {
  217. return await new Promise<number>((resolve) => {
  218. uni.createSelectorQuery()
  219. // #ifdef MP
  220. .in(instance)
  221. // #endif
  222. .select(`#${id}`)
  223. .boundingClientRect((data) => {
  224. resolve((data as UniApp.NodeInfo)?.width ?? 0)
  225. })
  226. .exec();
  227. })
  228. }
  229. defineExpose({
  230. measureTextWidth,
  231. })
  232. defineOptions({
  233. options: {
  234. virtualHost: true,
  235. styleIsolation: "shared",
  236. },
  237. });
  238. </script>
  239. <style lang="scss">
  240. .nana-text-can-press {
  241. cursor: pointer;
  242. opacity: 0.8;
  243. }
  244. </style>