12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import { _decorator, CacheMode, Color, Component, Label, Node, Size, Tween, tween, UITransform, Vec3, view } from 'cc';
- import { guangboData } from '../../data';
- const { ccclass, property } = _decorator;
- @ccclass('home_guangbo')
- export class home_guangbo extends Component {
- @property(Node) lab_content:Node = null
- private cur_index = 0
- private data_list:guangboData[] = []
- private screen_width:number = 1080
- private speed:number = 5
- private left_padding:number = 30
- init(data_list:guangboData[]) {
- this.data_list = data_list
- // console.log(this.data_list)
- if(this.data_list.length==0) {
- this.lab_content.getComponent(Label).getComponent(Label).string = ''
- this.unscheduleAllCallbacks()
- return
- }
- this.cur_index = 0
- this.getContentText()
- }
- private getContentText() {
- if(this.cur_index>=this.data_list.length) {
- this.cur_index = 0
- }
- let cur_content = this.data_list[this.cur_index].title
- let lab_com = this.lab_content.getComponent(Label)
- lab_com.getComponent(Label).string = cur_content
- let cur_content_width = this.computeTextWidth(cur_content,lab_com.fontSize,lab_com.lineHeight)
- // console.log('cur_content_width=',cur_content_width)
- let cur_content_height = this.lab_content.getComponent(UITransform).contentSize.height
- this.lab_content.getComponent(UITransform).setContentSize(new Size(cur_content_width,cur_content_height))
- this.lab_content.setPosition(this.screen_width/2+this.left_padding,this.lab_content.getPosition().y,0)
- this.runContent(cur_content_width)
- }
- private runContent(content_width:number) {
- this.unscheduleAllCallbacks()
- let run_pos_x = this.lab_content.getPosition().x
- this.schedule(()=>{
- run_pos_x -= this.speed
- // console.log('cur_lab_content_postion_x=',cur_lab_content_postion_x)
- this.lab_content.setPosition(run_pos_x,this.lab_content.getPosition().y,0)
- if(run_pos_x<=-(content_width+this.screen_width/2)) {
- run_pos_x = this.screen_width/2+this.left_padding
- this.unscheduleAllCallbacks()
- this.cur_index++
- this.getContentText()
- }
- },0.08)
- }
- /**
- * @param text 要显示文本内容
- * @param designSize label的设计宽高
- * @param fontSize 字体大小
- * @param lineHeight 行高
- */
- private computeTextWidth(text: string, fontSize: number, lineHeight: number):number {
- let node = new Node();
- var newlabel :Label = node.addComponent(Label);
- newlabel.fontSize = fontSize;
- newlabel.lineHeight = lineHeight;
- newlabel.string = text;
- newlabel.color = new Color(0, 0, 0, 0);
- //计算宽度
- newlabel.overflow = Label.Overflow.NONE; //设置不换行和不等比压缩,文字原来有多长就多长
- newlabel.cacheMode = CacheMode.CHAR; //这里要设一下CacheMode.CHAR, 否则Label最大长度只有2048,不能再大了.
-
- newlabel.updateRenderData(true);
- let textWidth = newlabel.getComponent(UITransform).contentSize.width;
- node.destroy();
-
- return textWidth;
- }
- }
|