Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 195caa1

Browse files
committedJul 29, 2019
fix: some ios fixes and optimisations
1 parent 205972c commit 195caa1

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed
 

‎src/label.ios.ts

+28-22
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { htmlProperty, LabelBase, lineBreakProperty, maxLinesProperty, textShado
22
import { layout } from 'tns-core-modules/utils/utils';
33
import { fontInternalProperty, Length, paddingBottomProperty, paddingLeftProperty, paddingRightProperty, paddingTopProperty, View } from 'tns-core-modules/ui/page/page';
44
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';
66
import { TextShadow } from './label';
7+
import { isString } from 'tns-core-modules/utils/types';
78

89
export * from './label-common';
910
enum FixedSize {
@@ -19,6 +20,26 @@ declare module 'tns-core-modules/ui/text-base/text-base' {
1920
}
2021
}
2122

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+
2243
function lineBreakToLineBreakMode(value: string) {
2344
switch (value) {
2445
case 'end':
@@ -111,7 +132,6 @@ export class Label extends LabelBase {
111132
}
112133

113134
public createNativeView() {
114-
console.log('createNativeView', this);
115135
const view = UITextView.new();
116136
if (!view.font) {
117137
view.font = UIFont.systemFontOfSize(12);
@@ -136,7 +156,6 @@ export class Label extends LabelBase {
136156
this._observer = ObserverClass.alloc();
137157
this._observer['_owner'] = new WeakRef(this);
138158
this.nativeViewProtected.addObserverForKeyPathOptionsContext(this._observer, 'contentSize', NSKeyValueObservingOptions.New, null);
139-
console.log('initNativeView', this);
140159
this.nativeViewProtected.attributedText = this.htmlText;
141160
// this.htmlText = null;
142161
// this.needsHTMLUpdate = false;
@@ -186,37 +205,34 @@ export class Label extends LabelBase {
186205
}
187206

188207
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')) {
191209
const paragraphStyle = NSMutableParagraphStyle.alloc().init();
192210
paragraphStyle.minimumLineHeight = style.lineHeight;
193211
// make sure a possible previously set text alignment setting is not lost when line height is specified
194212
paragraphStyle.alignment = (this.nativeTextViewProtected as UITextField | UITextView | UILabel).textAlignment;
195213

196214
// make sure a possible previously set line break mode is not lost when line height is specified
197215

198-
console.log('lineBreakMode', this.nativeTextViewProtected.textContainer.lineBreakMode);
199216
if (style['lineBreak']) {
200217
paragraphStyle.lineBreakMode = lineBreakToLineBreakMode(style['lineBreak']);
201218
} else if (style.whiteSpace) {
202219
paragraphStyle.lineBreakMode = whiteSpaceToLineBreakMode(style.whiteSpace);
203220
}
204221
dict.set(NSParagraphStyleAttributeName, paragraphStyle);
205-
} else if (isTextView) {
222+
} else if (isTextView && this.style.textAlignment !== 'initial') {
206223
const paragraphStyle = NSMutableParagraphStyle.alloc().init();
207224
paragraphStyle.alignment = this.nativeTextViewProtected.textAlignment;
208225
dict.set(NSParagraphStyleAttributeName, paragraphStyle);
209226
}
210227

211-
if (style.color && (dict.size > 0 || isTextView)) {
228+
if (style.color && dict.size > 0) {
212229
dict.set(NSForegroundColorAttributeName, style.color.ios);
213230
}
214231

215232
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) {
220236
if (isTextView) {
221237
// UITextView's font seems to change inside.
222238
dict.set(NSFontAttributeName, this.nativeTextViewProtected.font);
@@ -303,11 +319,9 @@ export class Label extends LabelBase {
303319
fontSize = this.style.fontInternal.fontSize;
304320
}
305321
}
306-
// console.log('span', fontFamily, fontSize);
307322

308323
htmlString = `<span style="font-family: ${fontFamily}; font-size:${fontSize};">${htmlString}</span>`;
309324
const nsString = NSString.stringWithString(htmlString);
310-
// console.log('updateHTMLString1', htmlString);
311325
const nsData = nsString.dataUsingEncoding(NSUTF8StringEncoding);
312326
const options = {
313327
[DTDefaultTextAlignment]: kCTLeftTextAlignment,
@@ -333,11 +347,8 @@ export class Label extends LabelBase {
333347
}
334348
}
335349
);
336-
// console.log('updateHTMLString', this, this.html);
337350
// const nsString = NSString.stringWithString(htmlString);
338-
// // console.log('updateHTMLString1');
339351
// const nsData = nsString.dataUsingEncoding(NSUnicodeStringEncoding);
340-
// // console.log('creating NSAttributedString', htmlString, nsData.length, new Error().stack);
341352
// this.htmlText = NSAttributedString.alloc().initWithDataOptionsDocumentAttributesError(
342353
// nsData,
343354
// <any>{
@@ -346,7 +357,6 @@ export class Label extends LabelBase {
346357
// },
347358
// null
348359
// );
349-
// console.log('updateHTMLString', 'done');
350360

351361
// this.needsHTMLUpdate = false;
352362
this._requestLayoutOnTextChanged();
@@ -365,7 +375,6 @@ export class Label extends LabelBase {
365375
}
366376
[htmlProperty.setNative](value: string) {
367377
// this.htmlText = value;
368-
// console.log('htmlProperty', this, value !== this.html, !! this.htmlText);
369378
// if (this.needsHTMLUpdate || !this.style.fontInternal) {
370379
// this.needsHTMLUpdate = true;
371380
if (!this.style.fontInternal || !this.applyingNativeSetters) {
@@ -378,7 +387,6 @@ export class Label extends LabelBase {
378387
return nativeView.font;
379388
}
380389
[fontInternalProperty.setNative](value: Font | UIFont) {
381-
// console.log('fontInternalProperty', this, !!this.html, new Error().stack);
382390
super[fontInternalProperty.setNative](value);
383391
// this.needsHTMLUpdate = true;
384392
// font setter always called after html
@@ -466,7 +474,6 @@ export class Label extends LabelBase {
466474

467475
[lineBreakProperty.setNative](value: string) {
468476
const nativeView = this.nativeTextViewProtected;
469-
console.log('lineBreakProperty', value);
470477
nativeView.textContainer.lineBreakMode = lineBreakToLineBreakMode(value);
471478
}
472479
[textShadowProperty.setNative](value: TextShadow) {
@@ -479,7 +486,6 @@ export class Label extends LabelBase {
479486
}
480487
[whiteSpaceProperty.setNative](value: WhiteSpace) {
481488
const nativeView = this.nativeTextViewProtected;
482-
console.log('whiteSpaceProperty', value);
483489

484490
nativeView.textContainer.lineBreakMode = whiteSpaceToLineBreakMode(value);
485491
}

0 commit comments

Comments
 (0)
Please sign in to comment.