chenjun

视频详情页中标题已经4行的情况下展现全文并可点击展现

@@ -15,6 +15,7 @@ export struct PlayerTitleView { @@ -15,6 +15,7 @@ export struct PlayerTitleView {
15 @State titleHeight: number = 0 15 @State titleHeight: number = 0
16 @State rmhPlatform: number = 0 // 1是人民号 16 @State rmhPlatform: number = 0 // 1是人民号
17 @State isOverLines: boolean = false 17 @State isOverLines: boolean = false
  18 + @State isTitleOverLines: boolean = false
18 @State summary: string = '' 19 @State summary: string = ''
19 20
20 @State private titleLines: number = 0 21 @State private titleLines: number = 0
@@ -92,6 +93,41 @@ export struct PlayerTitleView { @@ -92,6 +93,41 @@ export struct PlayerTitleView {
92 console.log(TAG, 'clipStr:', clipStr) 93 console.log(TAG, 'clipStr:', clipStr)
93 return clipStr 94 return clipStr
94 } 95 }
  96 + /**
  97 + * 截断文本
  98 + * @param {string} str 要截断的文本 '啊啊啊啊啊'
  99 + * @param {number} fontSize 字体大小(px)
  100 + * @param {number} maxLines 最大行数 3
  101 + * @param {number} textWidth 文本宽度(px) vp 需要转换vp2px()
  102 + * @returns {string} clipStr 截断后的文本 '啊啊'
  103 + */
  104 + clipTitleText(str: string, fontSize: number, maxLines: number, textWidth: number): string {
  105 + let strArr: string[] = str.split("")
  106 + let truncateContent: string = '啊啊啊啊啊啊' // ...比正常文字宽度更小,这里使用啊啊啊(任意三个文字)代替计算
  107 + let measureTruncateWidth: number = measure.measureText({
  108 + textContent: truncateContent,
  109 + fontSize: fontSize,
  110 + fontWeight: 600,
  111 + lineHeight: 20,
  112 + wordBreak:WordBreak.BREAK_ALL
  113 + })
  114 + let clipStr: string = ''
  115 + for (let i = 0; i < strArr.length; i++) {
  116 + if (measure.measureText({
  117 + textContent: clipStr,
  118 + fontSize: fontSize,
  119 + fontWeight: 600,
  120 + lineHeight: 20,
  121 + wordBreak:WordBreak.BREAK_ALL
  122 + }) >= textWidth * maxLines - measureTruncateWidth) {
  123 + this.isTitleOverLines = true
  124 + break;
  125 + }
  126 + clipStr += strArr[i]
  127 + }
  128 + console.log(TAG, 'clipTitleText clipStr:', clipStr)
  129 + return clipStr
  130 + }
95 131
96 aboutToAppear(): void { 132 aboutToAppear(): void {
97 this.rmhPlatform = this.contentDetailData?.rmhPlatform || 0 133 this.rmhPlatform = this.contentDetailData?.rmhPlatform || 0
@@ -136,19 +172,20 @@ export struct PlayerTitleView { @@ -136,19 +172,20 @@ export struct PlayerTitleView {
136 172
137 } 173 }
138 174
139 - Text(this.getTitle())  
140 - .fontColor(Color.White)  
141 - .fontSize(15)  
142 - .maxLines(4)  
143 - .lineHeight(20)  
144 - .fontWeight(600)  
145 - .fontFamily('PingFang SC-Regular')  
146 - .textOverflow({ overflow: TextOverflow.Ellipsis })  
147 - .margin({ bottom: 2 }) //8  
148 - .onAreaChange((oldArea: Area, newArea: Area) => {  
149 - this.titleLines = Math.ceil((newArea.height as number) / 20) // 20是行高  
150 - this.updateSummaryLines()  
151 - }) 175 + // Text(this.getTitle())
  176 + // .fontColor(Color.White)
  177 + // .fontSize(15)
  178 + // .maxLines(4)
  179 + // .lineHeight(20)
  180 + // .fontWeight(600)
  181 + // .fontFamily('PingFang SC-Regular')
  182 + // .textOverflow({ overflow: TextOverflow.Ellipsis })
  183 + // .margin({ bottom: 2 }) //8
  184 + // .onAreaChange((oldArea: Area, newArea: Area) => {
  185 + // this.titleLines = Math.ceil((newArea.height as number) / 20) // 20是行高
  186 + // this.updateSummaryLines()
  187 + // })
  188 + this.titleBuilder()
152 /** 189 /**
153 * 标题大于三行或存在简介显示查看详情按钮 190 * 标题大于三行或存在简介显示查看详情按钮
154 */ 191 */
@@ -232,4 +269,51 @@ export struct PlayerTitleView { @@ -232,4 +269,51 @@ export struct PlayerTitleView {
232 .alignItems(HorizontalAlign.Start) 269 .alignItems(HorizontalAlign.Start)
233 .visibility(this.isOpenDetail || this.isDragging ? Visibility.None : Visibility.Visible) 270 .visibility(this.isOpenDetail || this.isDragging ? Visibility.None : Visibility.Visible)
234 } 271 }
  272 +
  273 + @Builder
  274 + titleBuilder() {
  275 + Text() {
  276 + Span(this.clipTitleText(this.getTitle(), 14, 4, this.windowWidth - 230 - vp2px(50)))
  277 + .fontSize(15)
  278 + .fontColor(Color.White)
  279 + .lineHeight(20)
  280 + .fontWeight(600)
  281 + .fontFamily('PingFang SC-Regular')
  282 + if (this.isTitleOverLines) {
  283 + Span('... 全文')
  284 + .fontColor('#888888')
  285 + .fontWeight(600)
  286 + .fontFamily('PingFang SC-Regular')
  287 + .fontSize(12)
  288 + .onClick(() => {
  289 + this.isOpenDetail = true
  290 + this.dialogController?.open()
  291 + })
  292 + ImageSpan($r('app.media.comment_unfold_svg'))
  293 + .width(14)
  294 + .height(14)
  295 + .objectFit(ImageFit.Fill)
  296 + .verticalAlign(ImageSpanAlignment.BOTTOM)
  297 + .margin({bottom:1})
  298 + // .padding({
  299 + // bottom: 4
  300 + // })
  301 + .onClick(() => {
  302 + this.isOpenDetail = true
  303 + this.dialogController?.open()
  304 + })
  305 +
  306 + }
  307 + }
  308 + .onAreaChange((oldArea: Area, newArea: Area) => {
  309 + this.titleLines = Math.ceil((newArea.height as number) / 20) // 20是行高
  310 + this.updateSummaryLines()
  311 + })
  312 + .padding({
  313 + left: 0,//6
  314 + right: 6,
  315 + top: 0,//4
  316 + bottom: 4
  317 + })
  318 + }
235 } 319 }