Text 文本
17:06说明
把常用文本处理收敛到一个组件内:主题色、加粗、块状、多行省略,以及价格、手机号、姓名、日期、超链接等 mode 格式化。mode="phone" 配合 call 可点击拨号,mode="link" 渲染为可点击链接;open-type 开放能力仅微信小程序生效。
兼容性
| 平台 | 支持情况 |
|---|---|
| App | ⚠️ 部分支持(不支持 open-type 开放能力;decoration 仅支持 underline / line-through) |
| H5 | ⚠️ 部分支持(不支持 open-type 开放能力) |
| 微信小程序 | ✅ 支持 |
示例
基础用法
text 传入文本,默认按普通文本渲染。
查看源码
vue
<template>
<ux-text text="我用十年青春,赴你最后之约"></ux-text>
</template>主题色 type
type 切换主题色:primary / success / error / warning。
查看源码
vue
<template>
<ux-text type="primary" text="这是一段主题色文本"></ux-text>
<ux-text type="success" text="这是一段成功色文本"></ux-text>
<ux-text type="error" text="这是一段错误色文本"></ux-text>
<ux-text type="warning" text="这是一段警告色文本"></ux-text>
</template>自定义颜色 color / 字号 size
color 与 size 覆盖文字颜色与字号(size 为数值时单位 px)。
查看源码
vue
<template>
<ux-text color="#2979ff" size="20" text="自定义颜色与字号"></ux-text>
</template>粗体 bold / 块状 block
bold 加粗,block 让文本独占一行。
查看源码
vue
<template>
<ux-text bold text="这是一段加粗文本"></ux-text>
<ux-text block text="这是一段块状文本,独占一行展示"></ux-text>
</template>行数省略 lines
lines 限制显示行数,超出部分以省略号显示(示例为 1 行 / 2 行)。
查看源码
vue
<template>
<ux-text lines="1" text="这是一段超长的文本内容,用于演示单行省略的效果,超出部分将以省略号显示"></ux-text>
<ux-text lines="2" text="这是一段超长的文本内容,用于演示两行省略的效果,超出两行的部分将以省略号显示"></ux-text>
</template>价格模式 mode="price"
mode="price" 对数字做价格格式化(千分位、保留两位小数)。
查看源码
vue
<template>
<ux-text mode="price" text="12345.678" type="error"></ux-text>
</template>手机号模式 mode="phone"
第一行 format="encrypt" 星号加密,第二行 call 点击直接拨打
查看源码
vue
<template>
<ux-text mode="phone" format="encrypt" text="13812345678"></ux-text>
<ux-text mode="phone" call text="13812345678"></ux-text>
</template>姓名加密
mode="name" 配合 format="encrypt" 对姓名做星号加密。
查看源码
vue
<template>
<ux-text mode="name" format="encrypt" text="张三丰"></ux-text>
<ux-text mode="name" format="encrypt" text="欧阳娜娜"></ux-text>
</template>日期格式化 mode="date"
mode="date" 格式化时间戳,format 自定义格式(不传时默认 yyyy-mm-dd)。
查看源码
vue
<template>
<ux-text mode="date" :text="dateValue"></ux-text>
<ux-text mode="date" format="yyyy-mm-dd hh:MM:ss" :text="dateValue"></ux-text>
</template>
<script setup lang="uts">
// 时间戳示例(2026-08-05 12:00:00)
const dateValue: number = 1783245600000;
</script>超链接模式 mode="link"
mode="link" 渲染为可点击链接,href 指定跳转地址。
查看源码
vue
<template>
<ux-text mode="link" href="https://uviewpro.cn" text="uView Pro 官网"></ux-text>
</template>图标 prefix-icon / suffix-icon
prefix-icon / suffix-icon 在文本前后添加图标。
查看源码
vue
<template>
<ux-text prefix-icon="home" text="前置图标文本"></ux-text>
<ux-text suffix-icon="arrow-right" text="后置图标文本"></ux-text>
</template>文字装饰 decoration
underline 下划线 / line-through 删除线
查看源码
vue
<template>
<ux-text decoration="underline" color="#2979ff" text="这是一段带下划线的文本"></ux-text>
<ux-text decoration="line-through" color="#fa3534" text="这是一段带删除线的文本"></ux-text>
</template>外边距与行高 margin / line-height
margin 与 line-height 均支持数字,自动加单位
查看源码
vue
<template>
<ux-text :margin="20" :line-height="30" text="这是一段设置外边距与行高的文本"></ux-text>
<ux-text :margin="20" :line-height="40" color="#2979ff" text="这是一段设置外边距与行高的文本"></ux-text>
</template>对齐与换行 align / word-wrap
block + align=center 水平居中,word-wrap=break-word 长文本断行
查看源码
vue
<template>
<ux-text block align="center" word-wrap="break-word" text="这是一段很长的文本,用于演示 word-wrap=break-word 在超出容器宽度时的断行效果。"></ux-text>
</template>样式对象 icon-style / text-style
iconStyle 控制图标字号,textStyle 控制文字样式
查看源码
vue
<template>
<ux-text
prefix-icon="home"
:icon-style="iconCustomStyle"
:text-style="textCustomStyle"
text="自定义图标与文字样式"
></ux-text>
</template>
<script setup lang="uts">
const iconCustomStyle: UTSJSONObject = { 'font-size': '16px' };
const textCustomStyle: UTSJSONObject = { 'font-style': 'italic' };
</script>开放能力 open-type(仅微信小程序)
open-type 文本在微信小程序下渲染为原生 button,可触发开放能力回调
查看源码
vue
<!-- #ifdef MP-WEIXIN -->
<template>
<view class="open-type-list">
<ux-text
open-type="getUserInfo"
lang="zh_CN"
margin="0 0 16px"
text="获取用户信息"
@getuserinfo="onGetUserInfo"
></ux-text>
<ux-text
open-type="contact"
session-from="ux-text-demo"
send-message-title="text-demo"
margin="0 0 16px"
text="联系客服"
@contact="onContact"
></ux-text>
<ux-text
open-type="getPhoneNumber"
text="获取手机号"
@getphonenumber="onGetPhoneNumber"
></ux-text>
</view>
<demo-result v-if="openTypeResult !== ''" :text="'回调结果: ' + openTypeResult" />
</template>
<style lang="scss">
.open-type-list {
display: flex;
flex-direction: column;
width: 100%;
}
</style>
<script setup lang="uts">
const openTypeResult = ref<string>('');
function updateOpenTypeResult(event: UniEvent) {
const detail = (event as unknown as UTSJSONObject)['detail'];
openTypeResult.value = detail == null ? '{}' : JSON.stringify(detail);
}
function onGetUserInfo(event: UniEvent) {
updateOpenTypeResult(event);
console.log('ux-text getUserInfo', (event as unknown as UTSJSONObject)['detail']);
}
function onContact(event: UniEvent) {
updateOpenTypeResult(event);
console.log('ux-text contact', (event as unknown as UTSJSONObject)['detail']);
}
function onGetPhoneNumber(event: UniEvent) {
updateOpenTypeResult(event);
console.log('ux-text getPhoneNumber', (event as unknown as UTSJSONObject)['detail']);
}
</script>
<!-- #endif -->API
Props
| 属性 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
customStyle | 自定义根节点样式 | string | UTSJSONObject | - | - |
customClass | 自定义根节点样式类 | string | '' | - |
type | 主题颜色 | string | '' | - |
show | 是否显示文本 | boolean | true | - |
text | 显示的值 | string | number | '' | - |
prefixIcon | 前置图标 | string | '' | - |
suffixIcon | 后置图标 | string | '' | - |
mode | 文本处理模式 | string | 'text' | - |
href | 链接地址 | string | '' | - |
format | 格式化规则 | string | '' | - |
call | 是否拨打电话 | boolean | false | - |
openType | 小程序打开方式 | string | '' | - |
lang | 返回用户信息的语言,zh_CN / zh_TW / en,只微信小程序有效 | string | '' | - |
sessionFrom | 会话来源,open-type=contact 时有效,只微信小程序有效 | string | '' | - |
sendMessageTitle | 会话内消息卡片标题,open-type=contact 时有效 | string | '' | - |
sendMessagePath | 会话内消息卡片跳转路径,open-type=contact 时有效 | string | '' | - |
sendMessageImg | 会话内消息卡片图片,open-type=contact 时有效 | string | '' | - |
showMessageCard | 是否显示会话内消息卡片,open-type=contact 时有效 | boolean | false | - |
appParameter | 打开 APP 时向 APP 传递的参数,open-type=launchApp 时有效 | string | '' | - |
formType | 用于 form 组件,点击分别触发 form 的 submit/reset 事件 | string | '' | - |
bold | 是否粗体 | boolean | false | - |
block | 是否块状 | boolean | false | - |
lines | 显示行数,超出省略号 | string | number | '' | - |
color | 文本颜色 | string | '' | - |
size | 字体大小(默认值与 const/let 段 DEFAULT_SIZE 保持一致) | string | number | 14 | - |
iconStyle | 图标样式 | UTSJSONObject | string | - | - |
textStyle | 文字样式 | UTSJSONObject | string | - | - |
decoration | 文字装饰 | string | 'none' | - |
margin | 外边距 | UTSJSONObject | string | number | 0 | - |
lineHeight | 行高 | string | number | '' | - |
align | 对齐方式 | string | 'left' | - |
wordWrap | 换行方式 | string | 'normal' | - |
Events
| 事件名 | 说明 | 回调参数 | 版本 |
|---|---|---|---|
click | 点击触发事件 | event: UniEvent | - |
getuserinfo | 获取用户信息回调,open-type=getUserInfo 时有效 | event: UniEvent | - |
contact | 客服消息回调,open-type=contact 时有效 | event: UniEvent | - |
getphonenumber | 获取手机号回调,open-type=getPhoneNumber 时有效 | event: UniEvent | - |
error | 开放能力发生错误时回调 | event: UniEvent | - |
launchapp | 打开 APP 成功回调,open-type=launchApp 时有效 | event: UniEvent | - |
opensetting | 打开授权设置页并关闭后回调,open-type=openSetting 时有效 | event: UniEvent | - |
Slots
| 插槽名 | 说明 | 版本 |
|---|---|---|
default | 自定义文本内容(默认显示 text;price / link / open-type 模式下同样替换默认文本) | - |
注意事项
mode与format配合:phone+format="encrypt"输出138****5678星号加密;name+format="encrypt"姓名加密;date不传format时默认yyyy-mm-dd。mode="phone"时只有call=true才会在点击时调起uni.makePhoneCall,否则仅派发click。decoration在 App 端仅支持underline/line-through(内部走text-decoration-line),其他值无效;H5 / 微信小程序用text-decoration。- 多行省略用
lines传行数,组件会自动追加块状与省略样式;App 走原生max-lines省略,H5 / 微信小程序走-webkit-line-clamp。 open-type仅微信小程序生效,此时文本整体渲染为原生button;App / H5 忽略该属性,仍按普通文本渲染。type非空时忽略color(type优先);size走三级优先链:props > 全局 config(componentProps.ux-text.size)> 内建默认 14。price模式仅对纯数字(含小数)做千分位格式化,非数字原样返回。