Skip to content

Commit ae06cb1

Browse files
committed
fix: multiple fixes
1 parent c246901 commit ae06cb1

File tree

3 files changed

+62
-53
lines changed

3 files changed

+62
-53
lines changed

src/label-common.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Label as HtmlViewDefinition, TextShadow } from './label';
1+
import { Label as LabelViewDefinition, TextShadow } from './label';
22
import { booleanConverter, Color, CSSType, dip } from 'tns-core-modules/ui/core/view';
3-
import { Label as TNLabel } from 'tns-core-modules/ui/label/label';
3+
import { Label as TNLabel } from 'tns-core-modules/ui/label';
44
import { Style } from 'tns-core-modules/ui/styling/style';
55
import { CssProperty, InheritedCssProperty, makeParser, makeValidator, Property } from 'tns-core-modules/ui/core/properties';
66
import { isIOS } from 'tns-core-modules/platform';
@@ -9,7 +9,7 @@ import { layout } from 'tns-core-modules/utils/utils';
99
export const cssProperty = (target: Object, key: string | symbol) => {
1010
// property getter
1111
const getter = function() {
12-
return this.style.key;
12+
return this.style[key];
1313
};
1414

1515
// property setter
@@ -26,7 +26,7 @@ export const cssProperty = (target: Object, key: string | symbol) => {
2626
};
2727

2828
@CSSType('HTMLLabel')
29-
export class LabelBase extends TNLabel implements HtmlViewDefinition {
29+
export class LabelBase extends TNLabel implements LabelViewDefinition {
3030
@cssProperty maxLines: string | number;
3131
@cssProperty autoFontSize: boolean;
3232
@cssProperty verticalTextAlignment: VerticalTextAlignment;
@@ -79,9 +79,9 @@ export const textShadowProperty = new CssProperty<Style, string | TextShadow>({
7979
});
8080
textShadowProperty.register(Style);
8181

82-
export type VerticalTextAlignment = 'initial' | 'top' | 'middle' | 'bottom';
82+
export type VerticalTextAlignment = 'initial' | 'top' | 'middle' | 'bottom' | 'center';
8383

84-
const textAlignmentConverter = makeParser<VerticalTextAlignment>(makeValidator<VerticalTextAlignment>('initial', 'top', 'middle', 'bottom'));
84+
const textAlignmentConverter = makeParser<VerticalTextAlignment>(makeValidator<VerticalTextAlignment>('initial', 'top', 'middle', 'bottom', 'center'));
8585
export const verticalTextAlignmentProperty = new InheritedCssProperty<Style, VerticalTextAlignment>({
8686
name: 'verticalTextAlignment',
8787
cssName: 'vertical-text-align',

src/label.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ export declare class Label extends TNLabel {
1515
/**
1616
* Gets the native [android widget](http://developer.android.com/reference/android/widget/TextView.html) that represents the user interface for this component. Valid only when running on Android OS.
1717
*/
18-
android: any /* android.widget.TextView */;
18+
android?: any /* android.widget.TextView */;
1919

2020
/**
2121
* Gets the native [UITextView](https://developer.apple.com/documentation/uikit/uitextview) that represents the user interface for this component. Valid only when running on iOS.
2222
*/
23-
ios: any /* UITextView */;
23+
ios?: any /* UITextView */;
2424

2525
/**
2626
* Gets or sets html string for the HtmlView.
@@ -29,7 +29,7 @@ export declare class Label extends TNLabel {
2929

3030
verticalTextAlignment: VerticalTextAlignment;
3131
}
32-
export type VerticalTextAlignment = 'initial' | 'top' | 'middle' | 'bottom';
32+
export type VerticalTextAlignment = 'initial' | 'top' | 'middle' | 'bottom' | 'center';
3333

3434
export declare const htmlProperty: Property<Label, string>;
3535
export declare const verticalTextAlignmentProperty: Property<Label, VerticalTextAlignment>;

src/label.ios.ts

+53-44
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
WhiteSpace,
1212
whiteSpaceProperty
1313
} from 'tns-core-modules/ui/text-base/text-base';
14-
import { TextShadow } from './label';
14+
import { TextShadow, verticalTextAlignmentProperty, VerticalTextAlignment } from './label';
1515
import { isString } from 'tns-core-modules/utils/types';
1616

1717
export * from './label-common';
@@ -79,48 +79,7 @@ class ObserverClass extends NSObject {
7979
if (path === 'contentSize') {
8080
const owner = this._owner && this._owner.get();
8181
if (owner) {
82-
const inset = owner.nativeViewProtected.textContainerInset;
83-
const top = layout.toDeviceIndependentPixels(owner.effectivePaddingTop + owner.effectiveBorderTopWidth);
84-
85-
switch (owner.verticalTextAlignment) {
86-
case 'initial': // not supported
87-
case 'top':
88-
owner.nativeViewProtected.textContainerInset = {
89-
top,
90-
left: inset.left,
91-
bottom: inset.bottom,
92-
right: inset.right
93-
};
94-
break;
95-
96-
case 'middle': {
97-
const height = tv.sizeThatFits(CGSizeMake(tv.bounds.size.width, 10000)).height;
98-
let topCorrect = (tv.bounds.size.height - height * tv.zoomScale) / 2.0;
99-
topCorrect = topCorrect < 0.0 ? 0.0 : topCorrect;
100-
// tv.contentOffset = CGPointMake(0, -topCorrect);
101-
owner.nativeViewProtected.textContainerInset = {
102-
top: top + topCorrect,
103-
left: inset.left,
104-
bottom: inset.bottom,
105-
right: inset.right
106-
};
107-
break;
108-
}
109-
110-
case 'bottom': {
111-
const height = tv.sizeThatFits(CGSizeMake(tv.bounds.size.width, 10000)).height;
112-
let bottomCorrect = tv.bounds.size.height - height * tv.zoomScale;
113-
bottomCorrect = bottomCorrect < 0.0 ? 0.0 : bottomCorrect;
114-
// tv.contentOffset = CGPointMake(0, -bottomCorrect);
115-
owner.nativeViewProtected.textContainerInset = {
116-
top: top + bottomCorrect,
117-
left: inset.left,
118-
bottom: inset.bottom,
119-
right: inset.right
120-
};
121-
break;
122-
}
123-
}
82+
owner.updateVerticalAlignment();
12483
}
12584
}
12685
}
@@ -161,7 +120,8 @@ export class Label extends LabelBase {
161120

162121
public initNativeView() {
163122
super.initNativeView();
164-
this._observer = ObserverClass.alloc();
123+
console.log('initNativeView');
124+
this._observer = ObserverClass.alloc().init();
165125
this._observer['_owner'] = new WeakRef(this);
166126
this.nativeViewProtected.addObserverForKeyPathOptionsContext(this._observer, 'contentSize', NSKeyValueObservingOptions.New, null);
167127
this.nativeViewProtected.attributedText = this.htmlText;
@@ -180,6 +140,52 @@ export class Label extends LabelBase {
180140
}
181141
}
182142

143+
updateVerticalAlignment() {
144+
const tv = this.nativeTextViewProtected;
145+
const inset = this.nativeViewProtected.textContainerInset;
146+
const top = layout.toDeviceIndependentPixels(this.effectivePaddingTop + this.effectiveBorderTopWidth);
147+
switch (this.verticalTextAlignment) {
148+
case 'initial': // not supported
149+
case 'top':
150+
this.nativeViewProtected.textContainerInset = {
151+
top,
152+
left: inset.left,
153+
bottom: inset.bottom,
154+
right: inset.right
155+
};
156+
break;
157+
158+
case 'middle':
159+
case 'center': {
160+
const height = tv.sizeThatFits(CGSizeMake(tv.bounds.size.width, 10000)).height;
161+
let topCorrect = (tv.bounds.size.height - height * tv.zoomScale) / 2.0;
162+
topCorrect = topCorrect < 0.0 ? 0.0 : topCorrect;
163+
// tv.contentOffset = CGPointMake(0, -topCorrect);
164+
this.nativeViewProtected.textContainerInset = {
165+
top: top + topCorrect,
166+
left: inset.left,
167+
bottom: inset.bottom,
168+
right: inset.right
169+
};
170+
break;
171+
}
172+
173+
case 'bottom': {
174+
const height = tv.sizeThatFits(CGSizeMake(tv.bounds.size.width, 10000)).height;
175+
let bottomCorrect = tv.bounds.size.height - height * tv.zoomScale;
176+
bottomCorrect = bottomCorrect < 0.0 ? 0.0 : bottomCorrect;
177+
// tv.contentOffset = CGPointMake(0, -bottomCorrect);
178+
this.nativeViewProtected.textContainerInset = {
179+
top: top + bottomCorrect,
180+
left: inset.left,
181+
bottom: inset.bottom,
182+
right: inset.right
183+
};
184+
break;
185+
}
186+
}
187+
}
188+
183189
get ios(): UITextView {
184190
return this.nativeViewProtected;
185191
}
@@ -556,4 +562,7 @@ export class Label extends LabelBase {
556562
// this.nativeViewProtected.textContainer.maximumNumberOfLines = value as number;
557563
// }
558564
// }
565+
[verticalTextAlignmentProperty.setNative](value: VerticalTextAlignment) {
566+
this.updateVerticalAlignment();
567+
}
559568
}

0 commit comments

Comments
 (0)