Skip to content

Commit 44be75a

Browse files
author
Hristo Hristov
authored
Move UILableImpl as TNSLabel in widgets. (NativeScript#2298)
Refactor Label class to the new use TNSLabel class. Added definitions.
1 parent 9d3df48 commit 44be75a

File tree

2 files changed

+74
-53
lines changed

2 files changed

+74
-53
lines changed

tns-core-modules/org.nativescript.widgets.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,9 @@
177177
}
178178
}
179179
}
180+
}
181+
182+
declare class TNSLabel extends UILabel {
183+
borderThickness: UIEdgeInsets;
184+
padding: UIEdgeInsets;
180185
}

tns-core-modules/ui/label/label.ios.ts

Lines changed: 69 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import * as enums from "ui/enums";
33
import * as utils from "utils/utils";
44
import * as backgroundModule from "ui/styling/background";
5-
import view = require("ui/core/view");
5+
import {View} from "ui/core/view";
66
import style = require("ui/styling/style");
77

88
global.moduleMerge(common, exports);
@@ -14,49 +14,6 @@ function ensureBackground() {
1414
}
1515
}
1616

17-
class UILabelImpl extends UILabel {
18-
19-
private _owner: WeakRef<Label>;
20-
21-
public static initWithOwner(owner: WeakRef<Label>): UILabelImpl {
22-
let labelImpl = <UILabelImpl>UILabelImpl.new();
23-
labelImpl._owner = owner;
24-
return labelImpl;
25-
}
26-
27-
public textRectForBoundsLimitedToNumberOfLines(bounds: CGRect, numberOfLines: number): CGRect {
28-
let rect = super.textRectForBoundsLimitedToNumberOfLines(bounds, numberOfLines);
29-
let owner = this._owner.get();
30-
if (owner) {
31-
let size = rect.size;
32-
rect = CGRectMake(
33-
- (owner.borderWidth + owner.style.paddingLeft),
34-
- (owner.borderWidth + owner.style.paddingTop),
35-
size.width + (owner.borderWidth + owner.style.paddingLeft + owner.style.paddingRight + owner.borderWidth),
36-
size.height + (owner.borderWidth + owner.style.paddingTop + owner.style.paddingBottom + owner.borderWidth)
37-
);
38-
}
39-
40-
return rect;
41-
}
42-
43-
public drawTextInRect(rect: CGRect): void {
44-
let owner = this._owner.get();
45-
let textRect: CGRect;
46-
let size = rect.size;
47-
if (owner) {
48-
textRect = CGRectMake((owner.borderWidth + owner.style.paddingLeft), (owner.borderWidth + owner.style.paddingTop),
49-
size.width - (owner.borderWidth + owner.style.paddingLeft + owner.style.paddingRight + owner.borderWidth),
50-
size.height - (owner.borderWidth + owner.style.paddingTop + owner.style.paddingBottom + owner.borderWidth));
51-
}
52-
else {
53-
textRect = CGRectMake(0, 0, size.width, size.height);
54-
}
55-
56-
super.drawTextInRect(textRect);
57-
}
58-
}
59-
6017
enum FixedSize {
6118
NONE = 0,
6219
WIDTH = 1,
@@ -71,7 +28,7 @@ export class Label extends common.Label {
7128
constructor() {
7229
super();
7330

74-
this._ios = UILabelImpl.initWithOwner(new WeakRef(this));
31+
this._ios = TNSLabel.new();
7532
this._ios.userInteractionEnabled = true;
7633
}
7734

@@ -89,7 +46,7 @@ export class Label extends common.Label {
8946
this.style._updateTextDecoration();
9047
this.style._updateTextTransform();
9148
}
92-
49+
9350
_requestLayoutOnTextChanged(): void {
9451
if (this._fixedSize === FixedSize.BOTH) {
9552
return;
@@ -119,7 +76,7 @@ export class Label extends common.Label {
11976
}
12077

12178
this._fixedSize = (widthMode === utils.layout.EXACTLY ? FixedSize.WIDTH : FixedSize.NONE)
122-
| (heightMode === utils.layout.EXACTLY ? FixedSize.HEIGHT : FixedSize.NONE);
79+
| (heightMode === utils.layout.EXACTLY ? FixedSize.HEIGHT : FixedSize.NONE);
12380

12481
var nativeSize = nativeView.sizeThatFits(CGSizeMake(width, height));
12582
var labelWidth = nativeSize.width;
@@ -131,17 +88,19 @@ export class Label extends common.Label {
13188
var measureWidth = Math.max(labelWidth, this.minWidth);
13289
var measureHeight = Math.max(nativeSize.height, this.minHeight);
13390

134-
var widthAndState = view.View.resolveSizeAndState(measureWidth, width, widthMode, 0);
135-
var heightAndState = view.View.resolveSizeAndState(measureHeight, height, heightMode, 0);
91+
var widthAndState = View.resolveSizeAndState(measureWidth, width, widthMode, 0);
92+
var heightAndState = View.resolveSizeAndState(measureHeight, height, heightMode, 0);
13693

13794
this.setMeasuredDimension(widthAndState, heightAndState);
13895
}
13996
}
14097
}
14198

99+
let zeroInsets = UIEdgeInsetsZero;
100+
142101
export class LabelStyler implements style.Styler {
143102
//Background methods
144-
private static setBackgroundInternalProperty(view: view.View, newValue: any) {
103+
private static setBackgroundInternalProperty(view: View, newValue: any) {
145104
var uiLabel: UILabel = <UILabel>view._nativeView;
146105
if (uiLabel && uiLabel.layer) {
147106
var flipImage = true;
@@ -152,7 +111,7 @@ export class LabelStyler implements style.Styler {
152111
}
153112
}
154113

155-
private static resetBackgroundInternalProperty(view: view.View, nativeValue: any) {
114+
private static resetBackgroundInternalProperty(view: View, nativeValue: any) {
156115
var uiLabel: UILabel = <UILabel>view._nativeView;
157116
if (uiLabel && uiLabel.layer) {
158117
var uiColor = <UIColor>nativeValue;
@@ -161,7 +120,7 @@ export class LabelStyler implements style.Styler {
161120
}
162121
}
163122

164-
private static getNativeBackgroundInternalValue(view: view.View): any {
123+
private static getNativeBackgroundInternalValue(view: View): any {
165124
var uiLabel: UILabel = <UILabel>view._nativeView;
166125
if (uiLabel && uiLabel.layer && uiLabel.layer.backgroundColor) {
167126
return UIColor.colorWithCGColor(uiLabel.layer.backgroundColor);
@@ -170,12 +129,69 @@ export class LabelStyler implements style.Styler {
170129
return undefined;
171130
}
172131

132+
private static setBorderWidthProperty(view: View, newValue: number) {
133+
LabelStyler.setNativeBorderWidth(view, newValue);
134+
}
135+
136+
private static resetBorderWidthProperty(view: View, nativeValue: number) {
137+
LabelStyler.setNativeBorderWidth(view, nativeValue);
138+
}
139+
140+
private static setNativeBorderWidth(view: View, newValue: number) {
141+
let nativeView = <UIView>view._nativeView;
142+
if (nativeView instanceof UIView) {
143+
nativeView.layer.borderWidth = newValue;
144+
}
145+
if (nativeView instanceof TNSLabel) {
146+
nativeView.borderThickness = { top: newValue, left: newValue, bottom: newValue, right: newValue };
147+
}
148+
}
149+
150+
private static getBorderWidthProperty(view: View): number {
151+
let nativeView = <UIView>view._nativeView;
152+
if (nativeView instanceof UIView) {
153+
return nativeView.layer.borderWidth;
154+
}
155+
return 0;
156+
}
157+
158+
private static setPaddingProperty(view: View, newValue: UIEdgeInsets) {
159+
LabelStyler.setNativePadding(view, newValue);
160+
}
161+
162+
private static resetPaddingProperty(view: View, nativeValue: UIEdgeInsets) {
163+
LabelStyler.setNativePadding(view, nativeValue);
164+
}
165+
166+
private static setNativePadding(view: View, padding: UIEdgeInsets) {
167+
let nativeView = <UIView>view._nativeView;
168+
if (nativeView instanceof TNSLabel) {
169+
nativeView.padding = { top: padding.top, left: padding.left, bottom: padding.bottom, right: padding.right };
170+
}
171+
}
172+
173+
private static getPaddingProperty(view: View): UIEdgeInsets {
174+
let nativeView = <UIView>view._nativeView;
175+
if (nativeView instanceof TNSLabel) {
176+
return nativeView.padding;
177+
}
178+
return zeroInsets;
179+
}
180+
173181
public static registerHandlers() {
174182
style.registerHandler(style.backgroundInternalProperty, new style.StylePropertyChangedHandler(
175183
LabelStyler.setBackgroundInternalProperty,
176184
LabelStyler.resetBackgroundInternalProperty,
177185
LabelStyler.getNativeBackgroundInternalValue), "Label");
186+
style.registerHandler(style.borderWidthProperty, new style.StylePropertyChangedHandler(
187+
LabelStyler.setBorderWidthProperty,
188+
LabelStyler.resetBorderWidthProperty,
189+
LabelStyler.getBorderWidthProperty), "Label");
190+
style.registerHandler(style.nativePaddingsProperty, new style.StylePropertyChangedHandler(
191+
LabelStyler.setPaddingProperty,
192+
LabelStyler.resetPaddingProperty,
193+
LabelStyler.getPaddingProperty), "Label");
178194
}
179195
}
180196

181-
LabelStyler.registerHandlers();
197+
LabelStyler.registerHandlers();

0 commit comments

Comments
 (0)