home_guangbo.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { _decorator, CacheMode, Color, Component, Label, Node, 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.lab_content.getComponent(Label).getComponent(Label).string = ''
  17. this.unscheduleAllCallbacks()
  18. return
  19. }
  20. this.cur_index = 0
  21. this.getContentText()
  22. }
  23. private getContentText() {
  24. if(this.cur_index>=this.data_list.length) {
  25. this.cur_index = 0
  26. }
  27. let cur_content = this.data_list[this.cur_index].title
  28. let lab_com = this.lab_content.getComponent(Label)
  29. lab_com.getComponent(Label).string = cur_content
  30. let cur_content_width = this.computeTextWidth(cur_content,lab_com.fontSize,lab_com.lineHeight)
  31. // console.log('cur_content_width=',cur_content_width)
  32. let cur_content_height = this.lab_content.getComponent(UITransform).contentSize.height
  33. this.lab_content.getComponent(UITransform).setContentSize(new Size(cur_content_width,cur_content_height))
  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 runContent(content_width:number) {
  38. this.unscheduleAllCallbacks()
  39. let run_pos_x = this.lab_content.getPosition().x
  40. this.schedule(()=>{
  41. run_pos_x -= this.speed
  42. // console.log('cur_lab_content_postion_x=',cur_lab_content_postion_x)
  43. this.lab_content.setPosition(run_pos_x,this.lab_content.getPosition().y,0)
  44. if(run_pos_x<=-(content_width+this.screen_width/2)) {
  45. run_pos_x = this.screen_width/2+this.left_padding
  46. this.unscheduleAllCallbacks()
  47. this.cur_index++
  48. this.getContentText()
  49. }
  50. },0.08)
  51. }
  52. /**
  53. * @param text 要显示文本内容
  54. * @param designSize label的设计宽高
  55. * @param fontSize 字体大小
  56. * @param lineHeight 行高
  57. */
  58. private computeTextWidth(text: string, fontSize: number, lineHeight: number):number {
  59. let node = new Node();
  60. var newlabel :Label = node.addComponent(Label);
  61. newlabel.fontSize = fontSize;
  62. newlabel.lineHeight = lineHeight;
  63. newlabel.string = text;
  64. newlabel.color = new Color(0, 0, 0, 0);
  65. //计算宽度
  66. newlabel.overflow = Label.Overflow.NONE; //设置不换行和不等比压缩,文字原来有多长就多长
  67. newlabel.cacheMode = CacheMode.CHAR; //这里要设一下CacheMode.CHAR, 否则Label最大长度只有2048,不能再大了.
  68. newlabel.updateRenderData(true);
  69. let textWidth = newlabel.getComponent(UITransform).contentSize.width;
  70. node.destroy();
  71. return textWidth;
  72. }
  73. }