@@ -2,8 +2,9 @@ import { htmlProperty, LabelBase, lineBreakProperty, maxLinesProperty, textShado
2
2
import { layout } from 'tns-core-modules/utils/utils' ;
3
3
import { fontInternalProperty , Length , paddingBottomProperty , paddingLeftProperty , paddingRightProperty , paddingTopProperty , View } from 'tns-core-modules/ui/page/page' ;
4
4
import { Font } from 'tns-core-modules/ui/styling/font' ;
5
- import { WhiteSpace , whiteSpaceProperty } from 'tns-core-modules/ui/text-base/text-base' ;
5
+ import { TextTransform , WhiteSpace , whiteSpaceProperty } from 'tns-core-modules/ui/text-base/text-base' ;
6
6
import { TextShadow } from './label' ;
7
+ import { isString } from 'tns-core-modules/utils/types' ;
7
8
8
9
export * from './label-common' ;
9
10
enum FixedSize {
@@ -19,6 +20,26 @@ declare module 'tns-core-modules/ui/text-base/text-base' {
19
20
}
20
21
}
21
22
23
+ function NSStringFromNSAttributedString ( source : NSAttributedString | string ) : NSString {
24
+ return NSString . stringWithString ( ( source instanceof NSAttributedString && source . string ) || ( source as string ) ) ;
25
+ }
26
+ export function getTransformedText ( text : string , textTransform : TextTransform ) : string {
27
+ if ( ! text || ! isString ( text ) ) {
28
+ return '' ;
29
+ }
30
+
31
+ switch ( textTransform ) {
32
+ case 'uppercase' :
33
+ return NSStringFromNSAttributedString ( text ) . uppercaseString ;
34
+ case 'lowercase' :
35
+ return NSStringFromNSAttributedString ( text ) . lowercaseString ;
36
+ case 'capitalize' :
37
+ return NSStringFromNSAttributedString ( text ) . capitalizedString ;
38
+ default :
39
+ return text ;
40
+ }
41
+ }
42
+
22
43
function lineBreakToLineBreakMode ( value : string ) {
23
44
switch ( value ) {
24
45
case 'end' :
@@ -111,7 +132,6 @@ export class Label extends LabelBase {
111
132
}
112
133
113
134
public createNativeView ( ) {
114
- console . log ( 'createNativeView' , this ) ;
115
135
const view = UITextView . new ( ) ;
116
136
if ( ! view . font ) {
117
137
view . font = UIFont . systemFontOfSize ( 12 ) ;
@@ -136,7 +156,6 @@ export class Label extends LabelBase {
136
156
this . _observer = ObserverClass . alloc ( ) ;
137
157
this . _observer [ '_owner' ] = new WeakRef ( this ) ;
138
158
this . nativeViewProtected . addObserverForKeyPathOptionsContext ( this . _observer , 'contentSize' , NSKeyValueObservingOptions . New , null ) ;
139
- console . log ( 'initNativeView' , this ) ;
140
159
this . nativeViewProtected . attributedText = this . htmlText ;
141
160
// this.htmlText = null;
142
161
// this.needsHTMLUpdate = false;
@@ -186,37 +205,34 @@ export class Label extends LabelBase {
186
205
}
187
206
188
207
const isTextView = this . nativeTextViewProtected instanceof UITextView ;
189
- console . log ( 'lineHeight' , style . lineHeight , style . whiteSpace ) ;
190
- if ( style . lineHeight || style . whiteSpace || style [ 'lineBreak' ] ) {
208
+ if ( style . lineHeight || style . whiteSpace === 'nowrap' || ( style [ 'lineBreak' ] && style [ 'lineBreak' ] !== 'none' ) ) {
191
209
const paragraphStyle = NSMutableParagraphStyle . alloc ( ) . init ( ) ;
192
210
paragraphStyle . minimumLineHeight = style . lineHeight ;
193
211
// make sure a possible previously set text alignment setting is not lost when line height is specified
194
212
paragraphStyle . alignment = ( this . nativeTextViewProtected as UITextField | UITextView | UILabel ) . textAlignment ;
195
213
196
214
// make sure a possible previously set line break mode is not lost when line height is specified
197
215
198
- console . log ( 'lineBreakMode' , this . nativeTextViewProtected . textContainer . lineBreakMode ) ;
199
216
if ( style [ 'lineBreak' ] ) {
200
217
paragraphStyle . lineBreakMode = lineBreakToLineBreakMode ( style [ 'lineBreak' ] ) ;
201
218
} else if ( style . whiteSpace ) {
202
219
paragraphStyle . lineBreakMode = whiteSpaceToLineBreakMode ( style . whiteSpace ) ;
203
220
}
204
221
dict . set ( NSParagraphStyleAttributeName , paragraphStyle ) ;
205
- } else if ( isTextView ) {
222
+ } else if ( isTextView && this . style . textAlignment !== 'initial' ) {
206
223
const paragraphStyle = NSMutableParagraphStyle . alloc ( ) . init ( ) ;
207
224
paragraphStyle . alignment = this . nativeTextViewProtected . textAlignment ;
208
225
dict . set ( NSParagraphStyleAttributeName , paragraphStyle ) ;
209
226
}
210
227
211
- if ( style . color && ( dict . size > 0 || isTextView ) ) {
228
+ if ( style . color && dict . size > 0 ) {
212
229
dict . set ( NSForegroundColorAttributeName , style . color . ios ) ;
213
230
}
214
231
215
232
const text = this . text ;
216
- const string = text === undefined || text === null ? '' : text . toString ( ) ;
217
- const source = string ;
218
- console . log ( 'setTextDecorationAndTransform' , dict . size , isTextView ) ;
219
- if ( dict . size > 0 || isTextView ) {
233
+ const str = text === undefined || text === null ? '' : text . toString ( ) ;
234
+ const source = getTransformedText ( str , this . textTransform ) ;
235
+ if ( dict . size > 0 ) {
220
236
if ( isTextView ) {
221
237
// UITextView's font seems to change inside.
222
238
dict . set ( NSFontAttributeName , this . nativeTextViewProtected . font ) ;
@@ -303,11 +319,9 @@ export class Label extends LabelBase {
303
319
fontSize = this . style . fontInternal . fontSize ;
304
320
}
305
321
}
306
- // console.log('span', fontFamily, fontSize);
307
322
308
323
htmlString = `<span style="font-family: ${ fontFamily } ; font-size:${ fontSize } ;">${ htmlString } </span>` ;
309
324
const nsString = NSString . stringWithString ( htmlString ) ;
310
- // console.log('updateHTMLString1', htmlString);
311
325
const nsData = nsString . dataUsingEncoding ( NSUTF8StringEncoding ) ;
312
326
const options = {
313
327
[ DTDefaultTextAlignment ] : kCTLeftTextAlignment ,
@@ -333,11 +347,8 @@ export class Label extends LabelBase {
333
347
}
334
348
}
335
349
) ;
336
- // console.log('updateHTMLString', this, this.html);
337
350
// const nsString = NSString.stringWithString(htmlString);
338
- // // console.log('updateHTMLString1');
339
351
// const nsData = nsString.dataUsingEncoding(NSUnicodeStringEncoding);
340
- // // console.log('creating NSAttributedString', htmlString, nsData.length, new Error().stack);
341
352
// this.htmlText = NSAttributedString.alloc().initWithDataOptionsDocumentAttributesError(
342
353
// nsData,
343
354
// <any>{
@@ -346,7 +357,6 @@ export class Label extends LabelBase {
346
357
// },
347
358
// null
348
359
// );
349
- // console.log('updateHTMLString', 'done');
350
360
351
361
// this.needsHTMLUpdate = false;
352
362
this . _requestLayoutOnTextChanged ( ) ;
@@ -365,7 +375,6 @@ export class Label extends LabelBase {
365
375
}
366
376
[ htmlProperty . setNative ] ( value : string ) {
367
377
// this.htmlText = value;
368
- // console.log('htmlProperty', this, value !== this.html, !! this.htmlText);
369
378
// if (this.needsHTMLUpdate || !this.style.fontInternal) {
370
379
// this.needsHTMLUpdate = true;
371
380
if ( ! this . style . fontInternal || ! this . applyingNativeSetters ) {
@@ -378,7 +387,6 @@ export class Label extends LabelBase {
378
387
return nativeView . font ;
379
388
}
380
389
[ fontInternalProperty . setNative ] ( value : Font | UIFont ) {
381
- // console.log('fontInternalProperty', this, !!this.html, new Error().stack);
382
390
super [ fontInternalProperty . setNative ] ( value ) ;
383
391
// this.needsHTMLUpdate = true;
384
392
// font setter always called after html
@@ -466,7 +474,6 @@ export class Label extends LabelBase {
466
474
467
475
[ lineBreakProperty . setNative ] ( value : string ) {
468
476
const nativeView = this . nativeTextViewProtected ;
469
- console . log ( 'lineBreakProperty' , value ) ;
470
477
nativeView . textContainer . lineBreakMode = lineBreakToLineBreakMode ( value ) ;
471
478
}
472
479
[ textShadowProperty . setNative ] ( value : TextShadow ) {
@@ -479,7 +486,6 @@ export class Label extends LabelBase {
479
486
}
480
487
[ whiteSpaceProperty . setNative ] ( value : WhiteSpace ) {
481
488
const nativeView = this . nativeTextViewProtected ;
482
- console . log ( 'whiteSpaceProperty' , value ) ;
483
489
484
490
nativeView . textContainer . lineBreakMode = whiteSpaceToLineBreakMode ( value ) ;
485
491
}
0 commit comments