home_guangbo.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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>' // 加粗(<b><\b>)CHAR模式,无效
  30. // console.log('cur_content=',cur_content)
  31. this.setLabContent(cur_content)
  32. let cur_content_width = this.lab_content.getComponent(UITransform).contentSize.width
  33. // console.log('cur_content_width=',cur_content_width)
  34. this.lab_content.setPosition(this.screen_width/2+this.left_padding,this.lab_content.getPosition().y,0)
  35. this.runContent(cur_content_width)
  36. }
  37. private getContentLabelText() {
  38. if(this.cur_index>=this.data_list.length) {
  39. this.cur_index = 0
  40. }
  41. let cur_content = this.data_list[this.cur_index].title
  42. this.setLabContent(cur_content)
  43. let lab_component = this.lab_content.getComponent(Label)
  44. let cur_content_width = this.computeLabelTextWidth(cur_content,lab_component.fontSize,lab_component.lineHeight)
  45. // console.log('cur_content_width=',cur_content_width)
  46. let cur_content_height = this.lab_content.getComponent(UITransform).contentSize.height
  47. this.lab_content.getComponent(UITransform).setContentSize(new Size(cur_content_width,cur_content_height))
  48. this.lab_content.setPosition(this.screen_width/2+this.left_padding,this.lab_content.getPosition().y,0)
  49. this.runContent(cur_content_width)
  50. }
  51. private runContent(content_width:number) {
  52. this.unscheduleAllCallbacks()
  53. let run_pos_x = this.lab_content.getPosition().x
  54. this.schedule(()=>{
  55. run_pos_x -= this.speed
  56. // console.log('cur_lab_content_postion_x=',cur_lab_content_postion_x)
  57. this.lab_content.setPosition(run_pos_x,this.lab_content.getPosition().y,0)
  58. if(run_pos_x<=-(content_width+this.screen_width/2)) {
  59. run_pos_x = this.screen_width/2+this.left_padding
  60. this.unscheduleAllCallbacks()
  61. this.cur_index++
  62. // this.getContentLabelText()
  63. this.getContentRichText()
  64. }
  65. },0.08)
  66. }
  67. private setLabContent(text:string) {
  68. // this.lab_content.getComponent(Label).string = text
  69. this.lab_content.getComponent(RichText).string = text
  70. }
  71. /**
  72. * @param text 要显示文本内容
  73. * @param designSize label的设计宽高
  74. * @param fontSize 字体大小
  75. * @param lineHeight 行高
  76. */
  77. private computeLabelTextWidth(text: string, fontSize: number, lineHeight: number):number {
  78. // 计算Label宽度
  79. let node = new Node();
  80. var newlabel :Label = node.addComponent(Label);
  81. newlabel.fontSize = fontSize;
  82. newlabel.lineHeight = lineHeight;
  83. newlabel.string = text;
  84. newlabel.color = new Color(0, 0, 0, 0);
  85. //计算宽度
  86. newlabel.overflow = Label.Overflow.NONE; //设置不换行和不等比压缩,文字原来有多长就多长
  87. newlabel.cacheMode = CacheMode.CHAR; //这里要设一下CacheMode.CHAR, 否则Label最大长度只有2048,不能再大了.
  88. newlabel.updateRenderData(true);
  89. let textWidth = newlabel.getComponent(UITransform).contentSize.width;
  90. node.destroy();
  91. return textWidth;
  92. }
  93. }