home_guangbo.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { _decorator, CacheMode, Color, Component, Label, Node, RichText, Size, Tween, tween, UITransform, Vec3, view } from 'cc';
  2. import { guangboData } from '../../data';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('home_guangbo')
  5. export class home_guangbo extends Component {
  6. @property(Node) lab_content:Node = null
  7. private cur_index = 0
  8. private data_list:guangboData[] = []
  9. private screen_width:number = 1080
  10. private speed:number = 5
  11. private left_padding:number = 30
  12. init(data_list:guangboData[]) {
  13. this.data_list = data_list
  14. // console.log(this.data_list)
  15. if(this.data_list.length==0) {
  16. this.setLabContent('')
  17. this.unscheduleAllCallbacks()
  18. return
  19. }
  20. this.cur_index = 0
  21. // this.getContentLabelText()
  22. this.getContentRichText()
  23. }
  24. private getContentRichText() {
  25. if(this.cur_index>=this.data_list.length) {
  26. this.cur_index = 0
  27. }
  28. let cur_content = this.data_list[this.cur_index].title
  29. cur_content = '<b>' + cur_content + '</b>' // 加粗
  30. this.setLabContent(cur_content)
  31. let cur_content_width = this.lab_content.getComponent(UITransform).contentSize.width
  32. // console.log('cur_content_width=',cur_content_width)
  33. this.lab_content.setPosition(this.screen_width/2+this.left_padding,this.lab_content.getPosition().y,0)
  34. this.runContent(cur_content_width)
  35. }
  36. private getContentLabelText() {
  37. if(this.cur_index>=this.data_list.length) {
  38. this.cur_index = 0
  39. }
  40. let cur_content = this.data_list[this.cur_index].title
  41. this.setLabContent(cur_content)
  42. let lab_component = this.lab_content.getComponent(Label)
  43. let cur_content_width = this.computeLabelTextWidth(cur_content,lab_component.fontSize,lab_component.lineHeight)
  44. // console.log('cur_content_width=',cur_content_width)
  45. let cur_content_height = this.lab_content.getComponent(UITransform).contentSize.height
  46. this.lab_content.getComponent(UITransform).setContentSize(new Size(cur_content_width,cur_content_height))
  47. this.lab_content.setPosition(this.screen_width/2+this.left_padding,this.lab_content.getPosition().y,0)
  48. this.runContent(cur_content_width)
  49. }
  50. private runContent(content_width:number) {
  51. this.unscheduleAllCallbacks()
  52. let run_pos_x = this.lab_content.getPosition().x
  53. this.schedule(()=>{
  54. run_pos_x -= this.speed
  55. // console.log('cur_lab_content_postion_x=',cur_lab_content_postion_x)
  56. this.lab_content.setPosition(run_pos_x,this.lab_content.getPosition().y,0)
  57. if(run_pos_x<=-(content_width+this.screen_width/2)) {
  58. run_pos_x = this.screen_width/2+this.left_padding
  59. this.unscheduleAllCallbacks()
  60. this.cur_index++
  61. // this.getContentLabelText()
  62. this.getContentRichText()
  63. }
  64. },0.08)
  65. }
  66. private setLabContent(text:string) {
  67. // this.lab_content.getComponent(Label).string = text
  68. this.lab_content.getComponent(RichText).string = text
  69. }
  70. /**
  71. * @param text 要显示文本内容
  72. * @param designSize label的设计宽高
  73. * @param fontSize 字体大小
  74. * @param lineHeight 行高
  75. */
  76. private computeLabelTextWidth(text: string, fontSize: number, lineHeight: number):number {
  77. // 计算Label宽度
  78. let node = new Node();
  79. var newlabel :Label = node.addComponent(Label);
  80. newlabel.fontSize = fontSize;
  81. newlabel.lineHeight = lineHeight;
  82. newlabel.string = text;
  83. newlabel.color = new Color(0, 0, 0, 0);
  84. //计算宽度
  85. newlabel.overflow = Label.Overflow.NONE; //设置不换行和不等比压缩,文字原来有多长就多长
  86. newlabel.cacheMode = CacheMode.CHAR; //这里要设一下CacheMode.CHAR, 否则Label最大长度只有2048,不能再大了.
  87. newlabel.updateRenderData(true);
  88. let textWidth = newlabel.getComponent(UITransform).contentSize.width;
  89. node.destroy();
  90. return textWidth;
  91. }
  92. }