xugenyuan

ref |> 临时解决评论内容带折行且超过3行时 无法展开收起问题

Signed-off-by: xugenyuan <xugenyuan@wondertek.com.cn>
@@ -6,8 +6,10 @@ import display from '@ohos.display'; @@ -6,8 +6,10 @@ import display from '@ohos.display';
6 const collapseString = '...展开全文' 6 const collapseString = '...展开全文'
7 const uncollapseString = '...收起' 7 const uncollapseString = '...收起'
8 8
  9 +const TestLongText = "超过三行超\n过三行超过\n三行超超过三行\n超过三行超过三行超过三\n行超过"
  10 +
9 @Component 11 @Component
10 -@Preview 12 +// @Preview
11 export struct CommentText { 13 export struct CommentText {
12 // 长文本 14 // 长文本
13 @State longMessage: string = '' 15 @State longMessage: string = ''
@@ -26,6 +28,7 @@ export struct CommentText { @@ -26,6 +28,7 @@ export struct CommentText {
26 @State expandedStates: boolean = false; 28 @State expandedStates: boolean = false;
27 @State fontSize: number = 18; 29 @State fontSize: number = 18;
28 @State fontWeight: FontWeight = FontWeight.Regular 30 @State fontWeight: FontWeight = FontWeight.Regular
  31 + @State lineHeight: number = 24
29 fontColor: ResourceColor = $r('app.color.color_222222') 32 fontColor: ResourceColor = $r('app.color.color_222222')
30 // 测量文本宽度(单位px) 33 // 测量文本宽度(单位px)
31 @State textWidth: number = 0; 34 @State textWidth: number = 0;
@@ -37,6 +40,8 @@ export struct CommentText { @@ -37,6 +40,8 @@ export struct CommentText {
37 promise: Promise<Array<display.Display>> = display.getAllDisplays() 40 promise: Promise<Array<display.Display>> = display.getAllDisplays()
38 41
39 aboutToAppear() { 42 aboutToAppear() {
  43 + // this.longMessage = TestLongText
  44 +
40 console.log(`文本宽度为:${this.textWidth}`) 45 console.log(`文本宽度为:${this.textWidth}`)
41 let padding = vp2px(5 + this.marginWidth) 46 let padding = vp2px(5 + this.marginWidth)
42 47
@@ -58,6 +63,17 @@ export struct CommentText { @@ -58,6 +63,17 @@ export struct CommentText {
58 this.isExpanded = (this.screenWidth - padding) * this.lines <= this.textWidth 63 this.isExpanded = (this.screenWidth - padding) * this.lines <= this.textWidth
59 // this.expandedStates = this.isExpanded; 64 // this.expandedStates = this.isExpanded;
60 65
  66 + let lines = this.getTextLineNum(
  67 + this.longMessage,
  68 + (this.screenWidth - padding),
  69 + this.lineHeight,
  70 + this.fontSize,
  71 + this.fontWeight
  72 + )
  73 + if (lines > 3) {
  74 + this.isExpanded = true
  75 + }
  76 +
61 //需要展开的话计算3行需要显示的文字 77 //需要展开的话计算3行需要显示的文字
62 if (this.isExpanded) { 78 if (this.isExpanded) {
63 79
@@ -76,13 +92,25 @@ export struct CommentText { @@ -76,13 +92,25 @@ export struct CommentText {
76 wordBreak:WordBreak.BREAK_ALL 92 wordBreak:WordBreak.BREAK_ALL
77 }) 93 })
78 94
  95 + let size = this.topMeasureText(
  96 + string,
  97 + (this.screenWidth - padding),
  98 + this.lineHeight,
  99 + this.fontSize,
  100 + this.fontWeight,
  101 + WordBreak.BREAK_ALL)
  102 + let height: number = Number(size.height)
  103 + let lines = Math.ceil(px2vp(height) / this.lineHeight)
  104 +
79 //计算有误差20 105 //计算有误差20
80 106
81 - if (thisTextWidth >= maxLineTextWidth) { 107 + if (thisTextWidth >= maxLineTextWidth || lines > 3) {
  108 + // if (this.maxLineMesssage.endsWith('\n')) {
  109 + // this.maxLineMesssage = this.maxLineMesssage.slice(0, this.maxLineMesssage.length - 1)
  110 + // }
82 break 111 break
83 } 112 }
84 this.maxLineMesssage = element; 113 this.maxLineMesssage = element;
85 -  
86 } 114 }
87 } 115 }
88 116
@@ -152,6 +180,24 @@ export struct CommentText { @@ -152,6 +180,24 @@ export struct CommentText {
152 180
153 // .height('100%') 181 // .height('100%')
154 } 182 }
  183 +
  184 + // 获取文本几行
  185 + private getTextLineNum(text: string, constraintWidth: number, lineHeight: number, fontSize: number | string | Resource, fontWeight: FontWeight) {
  186 + let size = this.topMeasureText(text, constraintWidth, lineHeight, fontSize, fontWeight)
  187 + let height: number = Number(size.height)
  188 + return Math.ceil(px2vp(height) / lineHeight)
  189 + }
  190 +
  191 + private topMeasureText(text: string, constraintWidth: number, lineHeight: number, fontSize: number | string | Resource, fontWeight: FontWeight, wordBreak?: WordBreak) {
  192 + return measure.measureTextSize({
  193 + textContent: text,
  194 + fontSize: fontSize,
  195 + lineHeight: lineHeight,
  196 + constraintWidth: constraintWidth,
  197 + fontWeight: fontWeight,
  198 + wordBreak: wordBreak,
  199 + })
  200 + }
155 } 201 }
156 202
157 203
@@ -161,7 +207,11 @@ export struct CommentText { @@ -161,7 +207,11 @@ export struct CommentText {
161 struct Index { 207 struct Index {
162 build() { 208 build() {
163 Column() { 209 Column() {
164 - CommentText() 210 + CommentText({longMessage: TestLongText,
  211 + maxline: 3,
  212 + fontSize: 14,
  213 + fontWeight: FontWeight.Regular,
  214 + marginWidth: (95 + 16)})
165 } 215 }
166 } 216 }
167 } 217 }