简体中文
font-size 属性用于设置字体大小,更改字体大小还会更新字体大小相关的 <length> 单位,例如 line-height 属性的 em 单位值。
| Web | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|
| 4.0 | 3.9 | 4.11 | 4.61 | 5.0 |
font-size: <absolute-size> | <relative-size> | <length-percentage>;
| 名称 | 兼容性 | 描述 |
|---|---|---|
| large | 基于用户默认字体大小(medium)的绝对大小关键字。 | |
| larger | 相对大小关键字。字体大小将相对于父元素的字体大小变大或变小,大致按照上面用于区分绝对大小关键字的比率。 | |
| medium | 基于用户默认字体大小(medium)的绝对大小关键字。 | |
| small | 基于用户默认字体大小(medium)的绝对大小关键字。 | |
| smaller | 相对大小关键字。字体大小将相对于父元素的字体大小变大或变小,大致按照上面用于区分绝对大小关键字的比率。 | |
| x-large | 基于用户默认字体大小(medium)的绝对大小关键字。 | |
| x-small | 基于用户默认字体大小(medium)的绝对大小关键字。 | |
| xx-large | 基于用户默认字体大小(medium)的绝对大小关键字。 | |
| xx-small | 基于用户默认字体大小(medium)的绝对大小关键字。 | |
| xxx-large | 基于用户默认字体大小(medium)的绝对大小关键字。 | |
| math | 使用特殊的数学缩放规则来确定 font-size 属性的计算值。 |
| 平台 | 默认值 |
|---|---|
| uvue | 16px |
注意:W3C 默认值为:medium
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
示例
<template>
<!-- #ifdef APP -->
<scroll-view style="flex: 1">
<!-- #endif -->
<view style="flex-grow: 1;">
<text class="uni-tips">说明:左边是正常版本,右边是拍平版本</text>
<view class="demo-box">
<view class="common">
<text ref="text" :style="{'font-size': fontSize}">font-size: {{fontSize}}</text>
<text style="font-size: 30px;">font-size: 30px</text>
</view>
<view class="common">
<text ref="text" :style="{'font-size': fontSize}" flatten>font-size: {{fontSize}}</text>
<text style="font-size: 30px;" flatten>font-size: 30px</text>
</view>
</view>
<view class="uni-common-mt">
<text class="uni-title-text">setProperty 设置与 getPropertyValue 获取 font-size 测试</text>
</view>
<view class="common-box">
<!-- 普通版本 -->
<view class="uni-common-mt">
<text class="uni-title-text">font-size</text>
<text class="uni-info">设置值: {{fontSizeProp}}</text>
<text class="uni-info">获取值: {{fontSizeActual}}</text>
<view class="test-box">
<text ref="textRef" :style="{ fontSize: fontSizeProp }">当前 font-size: {{fontSizeProp}}</text>
</view>
</view>
<!-- 拍平版本 -->
<view class="uni-common-mt">
<text class="uni-title-text">测试拍平</text>
<text class="uni-info">设置值: {{fontSizeProp}}</text>
<text class="uni-info">获取值: {{fontSizeActualFlat}}</text>
<view class="test-box">
<text ref="textRefFlat" :style="{ fontSize: fontSizeProp }" flatten>当前 font-size: {{fontSizeProp}}</text>
</view>
</view>
</view>
<view class="uni-common-mt uni-common-mb">
<text class="uni-tips">第一个枚举值,'' (空字符串) - 空值情况</text>
<enum-data :items="fontSizeEnum" title="font-size 枚举值" @change="radioChangeFontSize" :compact="true"></enum-data>
<input-data :defaultValue="fontSizeProp" title="font-size 自定义值" type="text" @confirm="inputChangeFontSize"></input-data>
</view>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script setup lang="uts">
import { ItemType } from '@/components/enum-data/enum-data-types'
const fontSize = ref('15px')
// 自动化测试
const setFontSize = () => {
fontSize.value = '30px'
}
defineExpose({
setFontSize
})
const fontSizeEnum: ItemType[] = [
{ value: 0, name: '' },
{ value: 1, name: '0' },
{ value: 2, name: '0px' },
{ value: 3, name: '10px' },
{ value: 4, name: '20px' },
{ value: 5, name: '0rpx' },
{ value: 6, name: '20rpx' },
]
const fontSizeProp = ref('15px')
const fontSizeActual = ref('')
const fontSizeActualFlat = ref('')
const textRef = ref(null as UniTextElement | null)
const textRefFlat = ref(null as UniTextElement | null)
const getPropertyValues = () => {
fontSizeActual.value = textRef.value?.style.getPropertyValue('font-size') ?? ''
fontSizeActualFlat.value = textRefFlat.value?.style.getPropertyValue('font-size') ?? ''
}
const changeFontSize = (value: string) => {
fontSizeProp.value = value
textRef.value?.style.setProperty('font-size', value)
textRefFlat.value?.style.setProperty('font-size', value)
// 使用 nextTick 确保样式已应用后再获取值
nextTick(() => {
getPropertyValues()
})
}
const radioChangeFontSize = (index: number) => {
const selectedItem = fontSizeEnum.find((item): boolean => item.value === index)
if (selectedItem != null) {
changeFontSize(selectedItem.name)
}
}
const inputChangeFontSize = (value: string) => {
changeFontSize(value)
}
onReady(() => {
getPropertyValues()
})
</script>
<style>
.common{
background-color: gray;
justify-content: center;
height: 100px;
flex:1;
}
.demo-box {
flex-direction: row;
margin-top: 10px;
justify-content: space-around;
}
.common-box{
flex-direction: row;
justify-content: space-around;
}
.test-box {
width: 180px;
height: 80px;
background-color: gray;
justify-content: center;
align-items: center;
}
</style>
App平台仅支持以像素值(px)和相对像素值(rpx)设置字体大小,默认值为16px。
如果仅开发App,那么属性值可以不设置单位,不设置单位时当做 px 处理。但无法兼容web和小程序。
不支持百分比的单位、不支持基于用户默认字体大小的绝对大小关键字,如small、medium、large等、不支持em、rem、ex等单位。
虽然支持但不推荐使用rpx。
正常情况下,普通字体不需要、也不应该设置font-size。使用默认的16px即可。
更并不需要显式的设置font-size:16px,这种多余的代码浪费性能。
需要变大或变小的字体,基于16px的默认值,适当增加或缩小字号即可。
如果在font-size中使用rpx,类似于web开发中给字体大小设百分比,没有意义。rpx在font-size中使用有如下问题:
详见rpx适用范围
属性值必须设置单位,无单位时当做非法值处理。非法值会回退为默认值,即16px。