egret图文混排

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
class TextImage extends eui.Group {
       public templet: egret.TextField
       constructor(protected _TextImageData: Array<any> = []) {
           super();
           this.init()
       }
       set TextImageData(val: Array<any>) { this._TextImageData = val; this.init(); }
       get TextImageData() { return this._TextImageData }
       _size = 36
       /**设置文字字体大小 */
       set size(val: number) {
           this._size = val
       }
       get size(): number {
           return this._size
       }
       private _width: number;
       /**设置宽度 */
       set width(val) { this._width = val; this.invalidate(); }
       get width() { return this._width };
       private _verticalAlign = egret.VerticalAlign.MIDDLE;
       /**设置vercatical */
       set verticalAlign(val) { this._verticalAlign = val; this.invalidate(); }
       get verticalAlign() { return this._verticalAlign };
       _maxWidth: number
       /**设置组件的最大宽度 */
       set maxWidth(val: number) {
           this._maxWidth = val
           if (this.templet.width > val) {
               this.width = val
               this.invalidate()
           }
       }
       get maxWidth(): number {
           return this._maxWidth
       }
       private _lineSpacing = 0
       /**设置组件的行间距 */
       set lineSpacing(val: number) {
           if (this._lineSpacing === val) return
           this._lineSpacing = val
           this.invalidate()
       }
       get lineSpacing(): number {
           return this._lineSpacing
       }
       /**立即计算布局 */
       validateNow() {
           this.init()
           this.invalidate_Flag = false
       }
       /**组件的每行高度 */
       lineHeightArray: Array<number> = []
       components: any[] = []
       protected init() {
           this.removeChildren()
           if (this._TextImageData.length == 0) {
               this.width = this.height = 0
               return
           }
           // 保存组件,清除事件绑定
           this.components = []
           this._TextImageData.forEach(component => {
               if (typeof component == 'object') {
                   this.components.push(component)
                   if (!component['watch']) {
                       egret.is(component, 'eui.Image') && component.once(egret.Event.COMPLETE, this.invalidate, this)
                       eui.Watcher.watch(component, ['width'], this.invalidate, this)
                       eui.Watcher.watch(component, ['height'], this.invalidate, this)
                       component['watch'] = true
                   }
               }
           })
           let textFlow = this._TextImageData.map(text =>
               typeof text === "string" || egret.is(text, 'eui.Label')
                   ? { text: egret.is(text, 'eui.Label') ? text.text : text }
                   : { text: '樂', style: { size: text.width } }
           )
           let TextField: egret.TextField = this.templet = new egret.TextField();
           TextField.size = this.size;
           TextField.lineSpacing = 0;
           TextField.width = this.width
           TextField.textFlow = textFlow
           TextField.height = TextField.height
           this.height = 0
           this.lineHeightArray = []
           let componentTestIndex = 0 //测量高度的组件序号
           let componentIndex = 0 //布局的组件序号
           TextField['linesArr'].forEach((item,lines) => {
               console.log('item=>', item)
               //获取最大高度
               let maxheight = this.size
               //组件序号
               item.elements.forEach((element) => {
                   if (element.text == '樂') {
                       let c = this.components[componentTestIndex++]
                       maxheight = Math.max(maxheight, c.height)
                   }
               })
               maxheight = this._lineSpacing + maxheight
               this.lineHeightArray.push(maxheight)
               //开始布局
               let x = 0
               item.elements.forEach(element => {
                   let component
                   if (element.text !== '樂') {
                       component = new egret.TextField()
                       component.x = x
                       component.size = this.size
                       component.text = element.text
                       component.width = element.width
                       component.textColor = this._textColor
                       component.height = maxheight
                       component.y = this.height
                       component.verticalAlign = this._verticalAlign;
                   } else {
                       component = this.components[componentIndex++]
                       component.x = x
                       component.y = this.height + (maxheight - component.height) / 2
                   }
                   x += component.width
                   this.addChild(component)
               })
               this.height += maxheight
           })
           this._width = this.templet.width
       }
       _textColor: number = 0x000000
       set textColor(val) {
           this._textColor = val
           this.invalidate()
       }
       get textColor() {
           return this._textColor
       }
       /**失效验证 */
       private invalidate_Flag: boolean = false
       private invalidate(e?: egret.Event): void {
           if (this.invalidate_Flag) {
               return
           }
           this.invalidate_Flag = true
           this.once(egret.Event.ENTER_FRAME, () => {
               this.init()
               this.invalidate_Flag = false
           }, this);
       }
   }

  

posted @   游戏鼻祖  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2017-10-23 unity 同一标签里内容拥有不同颜色
点击右上角即可分享
微信分享提示