Skip to content

Commit 19c638a

Browse files
OMFG, fontsize, feature parity, border-width, updated demo, blah blah
1 parent 1bcf9c6 commit 19c638a

File tree

10 files changed

+271
-67
lines changed

10 files changed

+271
-67
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ A NativeScript plugin for the native checkbox widget.
99
#### Platform controls used:
1010
Android |
1111
---------- |
12-
[Android CheckBox](https://developer.android.com/reference/android/widget/CheckBox.html) |
12+
[Android CheckBox](https://developer.android.com/reference/android/widget/CheckBox.html)
1313
iOS |
1414
---------- |
1515
[BEMCheckBox](http://cocoapods.org/pods/BEMCheckBox) |
@@ -35,7 +35,7 @@ From your command prompt/terminal go to your app's root folder and execute:
3535
xmlns:CheckBox="nativescript-checkbox" loaded="pageLoaded">
3636
<ActionBar title="Native Checkbox" />
3737
<StackLayout>
38-
<CheckBox:CheckBox checked="{{ checkProp }}" text="{{ myCheckText }}" color="{{ myCheckColor }}" id="myCheckbox" />
38+
<CheckBox:CheckBox checked="{{ checkProp }}" text="{{ myCheckText }}" fillColor="{{ myCheckColor }}" id="myCheckbox" />
3939
<CheckBox:CheckBox text="CheckBox Label" checked="false" />
4040
</StackLayout>
4141
</Page>
@@ -62,14 +62,19 @@ public getCheckProp() {
6262

6363
- **checked** - boolean
6464
- **text** - text to use with the checkbox
65+
- **fillColor** - Color of the checkbox element
66+
67+
## Events
68+
- **checkedChanged** - When the state of the checkbox changes
6569

6670
## API
6771

6872
- **toggle()** - Change the checked state of the view to the inverse of its current state.
6973

7074
## Styling
7175

72-
- **color** - set the checkbox color tint - Android 21+ only.
76+
- **color** - set the text label color
77+
- **border-width** - set the line width of the checkbox element: iOS only
7378

7479
## Demo Setup
7580
* npm install tns-platform-declarations

checkbox.android.ts

Lines changed: 98 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
import {CheckBoxInterface} from "./checkbox";
12
import { View } from "ui/core/view";
23
import { Color } from "color";
34
import { isAndroid, device } from "platform";
45
import { Property, PropertyChangeData } from "ui/core/dependency-observable";
56
import { PropertyMetadata } from "ui/core/proxy";
7+
import {Font} from "ui/styling/font";
8+
import enums = require("ui/enums");
69
import style = require("ui/styling/style");
710

811
declare var android: any;
912

10-
export class CheckBox extends View {
13+
export class CheckBox extends View implements CheckBoxInterface {
1114
private _android: any; /// android.widget.CheckBox
15+
private _fillColor: string;
16+
private _checkBoxSize: number;
1217

1318
public static checkedProperty = new Property(
1419
"checked",
@@ -22,6 +27,11 @@ export class CheckBox extends View {
2227
new PropertyMetadata(false)
2328
);
2429

30+
constructor() {
31+
super();
32+
33+
this._checkBoxSize = 21;
34+
}
2535

2636
get android() {
2737
return this._android;
@@ -34,17 +44,46 @@ export class CheckBox extends View {
3444
get checked(): boolean {
3545
return this._getValue(CheckBox.checkedProperty);
3646
}
47+
3748
set checked(value: boolean) {
3849
this._setValue(CheckBox.checkedProperty, value);
3950
}
4051

41-
get text(): boolean {
52+
get text(): string {
4253
return this._getValue(CheckBox.textProperty);
4354
}
44-
set text(value: boolean) {
55+
set text(value: string) {
4556
this._setValue(CheckBox.textProperty, value);
4657
}
4758

59+
get fillColor() : string {
60+
return this._fillColor;
61+
}
62+
63+
set fillColor(color: string) {
64+
this._fillColor = color;
65+
66+
if(this._android)
67+
this._android.setButtonTintList(android.content.res.ColorStateList.valueOf(new Color(this._fillColor).android));
68+
}
69+
70+
get checkboxSize(){
71+
return this._checkBoxSize;
72+
}
73+
74+
set checkBoxSize(size: number) {
75+
this._checkBoxSize = size;
76+
}
77+
78+
//There is no difference between tint and fill on the android widget
79+
get tintColor() : string {
80+
return this.fillColor;
81+
}
82+
83+
set tintColor(color: string) {
84+
this.fillColor = color;
85+
}
86+
4887

4988
public _createUI() {
5089

@@ -54,6 +93,12 @@ export class CheckBox extends View {
5493
this._android.setText(this.text);
5594
}
5695

96+
//Set bound colors
97+
if(this._android){
98+
if(this.fillColor)
99+
this._android.setButtonTintList(android.content.res.ColorStateList.valueOf(new Color(this._fillColor).android));
100+
}
101+
57102
var that = new WeakRef(this);
58103

59104
this._android.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener({
@@ -73,9 +118,6 @@ export class CheckBox extends View {
73118
public toggle(): void {
74119
this._android.toggle();
75120
}
76-
77-
78-
79121
}
80122

81123

@@ -107,21 +149,68 @@ function onTextPropertyChanged(data: PropertyChangeData) {
107149

108150

109151
export class CheckBoxStyler implements style.Styler {
110-
private static setColorProperty(view: any, newValue: any) {
152+
private static setColorLabelProperty(view: any, newValue: any) {
111153
var cb = <android.widget.CheckBox>view._nativeView;
112154
if (cb) {
113-
(<any>cb).setButtonTintList(android.content.res.ColorStateList.valueOf(new Color(newValue).android));
155+
(<any>cb).setTextColor(new Color(newValue).android);
156+
}
157+
}
158+
159+
// font
160+
private static setFontInternalProperty(view: any, newValue: any, nativeValue?: any) {
161+
var tv = <android.widget.CheckBox>view._nativeView;
162+
var fontValue = <Font>newValue;
163+
164+
var typeface = fontValue.getAndroidTypeface();
165+
if (typeface) {
166+
tv.setTypeface(typeface);
167+
}
168+
else {
169+
tv.setTypeface(nativeValue.typeface);
170+
}
171+
172+
if (fontValue.fontSize) {
173+
tv.setTextSize(fontValue.fontSize);
174+
}
175+
else {
176+
tv.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, nativeValue.size);
177+
}
178+
}
179+
180+
private static resetFontInternalProperty(view: any, nativeValue: any) {
181+
var tv: android.widget.CheckBox = <android.widget.CheckBox>view._nativeView;
182+
if (tv && nativeValue) {
183+
tv.setTypeface(nativeValue.typeface);
184+
tv.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, nativeValue.size);
114185
}
115186
}
116187

188+
private static getNativeFontInternalValue(view: any): any {
189+
var tv: android.widget.TextView = <android.widget.CheckBox>view._nativeView;
190+
return {
191+
typeface: tv.getTypeface(),
192+
size: tv.getTextSize()
193+
};
194+
}
117195
private static resetColorProperty(view: View, nativeValue: number) {
118196
// Do nothing.
119197
}
120198

199+
121200
public static registerHandlers() {
122201
style.registerHandler(style.colorProperty, new style.StylePropertyChangedHandler(
123-
CheckBoxStyler.setColorProperty,
202+
CheckBoxStyler.setColorLabelProperty,
203+
CheckBoxStyler.resetColorProperty), "CheckBox");
204+
205+
style.registerHandler(style.fontInternalProperty, new style.StylePropertyChangedHandler(
206+
CheckBoxStyler.setFontInternalProperty,
207+
CheckBoxStyler.resetFontInternalProperty,
208+
CheckBoxStyler.getNativeFontInternalValue), "CheckBox");
209+
/*
210+
style.registerHandler(style.backgroundColorProperty, new style.StylePropertyChangedHandler(
211+
CheckBoxStyler.setColorLabelProperty,
124212
CheckBoxStyler.resetColorProperty), "CheckBox");
213+
*/
125214

126215
style.registerHandler(style.borderWidthProperty, style.ignorePropertyHandler, "CheckBox");
127216
style.registerHandler(style.borderColorProperty, style.ignorePropertyHandler, "CheckBox");

checkbox.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ declare module "nativescript-checkbox" {
3535
*/
3636
toggle(): void;
3737

38+
3839
}
40+
}
41+
42+
export interface CheckBoxInterface{
43+
text: string;
44+
checked: boolean;
45+
fillColor: string;
46+
tintColor: string;
47+
checkBoxSize: number;
3948

49+
toggle(): void;
4050
}

0 commit comments

Comments
 (0)