home_guangbo.ts 3.1 KB

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