css属性 | 说明 |
|---|---|
overflow | 规定了内容超出元素框时发生的事情。
|
text-overflow | 规定当文本溢出包含元素时发生的事情。
|
检查对应Text组件的textOverflow和maxLines参数设置。当Text中的文本内容过多,overflow设置为TextOverflow.None、TextOverflow.Clip或TextOverflow.Ellipsis,且maxLines行数不为1时,文本则会进行换行展示。
Row() {
Text('AAAAAAAAAAAAAAAAAAAAA' +
'BBBBBBBBBBBBBBBBBBBBB' +
'CCCCCCCCCCCCCCCCCCCCC' +
'DDDDDDDDDDDDDDDDDDDDD'
)
.maxLines(2)
}.width('100%') 根据具体场景决定文本是否超出隐藏以及是否限制文本在一行内显示或者调整布局。
@Entry
@Component
struct ArkUIOverflow {
build() {
Column({ space: 8 }) {
Row() {
Text('AAAAAAAAAAAAAAAAAAAAA' +
'BBBBBBBBBBBBBBBBBBBBB' +
'CCCCCCCCCCCCCCCCCCCCC' +
'DDDDDDDDDDDDDDDDDDDDD'
)
.textOverflow({ overflow: TextOverflow.Ellipsis })
.maxLines(1)
}
.width('100%')
.padding(16)
.backgroundColor('#f1f3f5')
}
.padding({
left: 20,
right: 20
})
.height('100%')
.width('100%')
}
} 效果如下:

修改H5页面文本的设置,限制块内显示行数为1,规定当文本溢出时显示省略符号来代表被修剪的文本,参考代码如下:
src/main/resources/rawfile/CloseDialog.html文件:
<!doctype html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>OverflowTest</title>
</head>
<body>
<div class="container">
<div id="mytext">
testtesttesttesttesttesttesttesttesttesttesttesttesttest
</div>
</div>
</body>
</html>
<style>
html, body {
width: 100vw;
height: 100vh;
padding: 0;
margin: 0;
}
.container {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
}
#mytext {
/* 超出限定的宽度就隐藏*/
overflow:hidden;
/* 规定当文本溢出时显示省略符号来代表被修剪的文本*/
text-overflow: ellipsis;
width: 200px;
height: 20px;
line-height: 20px;
padding: 16px;
background-color: #f6f7f8;
}
</style> src/main/ets/pages/WebOverflow.ets文件:
import { webview } from '@kit.ArkWeb';
@Entry
@Component
struct WebOverflow {
webController = new webview.WebviewController();
build() {
Column() {
Web({
src: $rawfile('OverflowTest.html'),
controller: this.webController
})
.javaScriptAccess(true)
.geolocationAccess(false)
.fileAccess(false)
.domStorageAccess(true)
}
.height('100%')
.width('100%')
}
} 效果如下:
