Skip to content

Commit

Permalink
Fix for #18
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Nov 23, 2016
1 parent d3165a3 commit 58243f6
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 117 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class SomeComponent {
## Css Styling

- **color** - set the text label color
- **font-size** - checkbox is sized to text from here
- **font-size** - checkbox is sized to text from here : default 15
- **border-width** - set the line width of the checkbox element: iOS only

## Demo Setup
Expand Down
21 changes: 12 additions & 9 deletions checkbox.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ export class CheckBox extends View implements CheckBoxInterface {
this._setValue(CheckBox.textProperty, value);
}

get fillColor() : string {
get fillColor(): string {
return this._fillColor;
}

set fillColor(color: string) {
this._fillColor = color;

if(this._android && device.sdkVersion >= "21")
if (this._android && device.sdkVersion >= "21")
this._android.setButtonTintList(android.content.res.ColorStateList.valueOf(new Color(this._fillColor).android));
}

//There is no difference between tint and fill on the android widget
get tintColor() : string {
get tintColor(): string {
return this.fillColor;
}

Expand All @@ -81,10 +81,13 @@ export class CheckBox extends View implements CheckBoxInterface {
if (this.text) {
this._android.setText(this.text);
}
if (!this.style.fontSize) {
this.style.fontSize = 15;
}



if(this._android){
if(this.fillColor && device.sdkVersion >= "21"){
if (this._android) {
if (this.fillColor && device.sdkVersion >= "21") {
//Set bound colors
this._android.setButtonTintList(android.content.res.ColorStateList.valueOf(new Color(this._fillColor).android));
}
Expand Down Expand Up @@ -141,14 +144,14 @@ function onTextPropertyChanged(data: PropertyChangeData) {


export class CheckBoxStyler implements style.Styler {
private static setColorLabelProperty(view: any, newValue: any) {
private static setColorLabelProperty(view: any, newValue: any) {
var cb = <android.widget.CheckBox>view._nativeView;
if (cb) {
if (cb) {
(<any>cb).setTextColor(new Color(newValue).android);
}
}

// font
// font
private static setFontInternalProperty(view: any, newValue: any, nativeValue?: any) {
var tv = <android.widget.CheckBox>view._nativeView;
var fontValue = <Font>newValue;
Expand Down
121 changes: 62 additions & 59 deletions checkbox.ios.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
/// <reference path="BEMCheckBox.d.ts" /> Needed for autocompletion and compilation.

import {CheckBoxInterface} from "./";
import { CheckBoxInterface } from "./";
import { View } from "ui/core/view";
import {ContentView} from "ui/content-view";
import { ContentView } from "ui/content-view";
import { Property, PropertyChangeData } from "ui/core/dependency-observable";
import { PropertyMetadata } from "ui/core/proxy";
import {Color} from "color";
import {Label} from "ui/label";
import {Button} from "ui/button";
import {StackLayout} from "ui/layouts/stack-layout";
import style = require("ui/styling/style");
import { Color } from "color";
import { Label } from "ui/label";
import { Button } from "ui/button";
import { StackLayout } from "ui/layouts/stack-layout";


declare var CGRectMake: any, CGPointMake: any;

export class CheckBox extends Button implements CheckBoxInterface {
public static checkedProperty = new Property("checked", "CheckBox", new PropertyMetadata(false));

private _iosCheckbox: BEMCheckBox;
private _delegate: BEMCheckBoxDelegateImpl;
private _checked: boolean;
private _fillColor: string = "#0075ff";
private _tintColor: string = "#0075ff";
private _lineWidth: number = 1;
Expand All @@ -41,26 +38,26 @@ export class CheckBox extends Button implements CheckBoxInterface {
this._tintColor = "#0075ff";
this._onAnimationType = 2;
this._offAnimationType = 2;

this._iosCheckbox = <BEMCheckBox>BEMCheckBox.alloc().initWithFrame(CGRectMake(0, 0, 21, 21));
this._delegate = BEMCheckBoxDelegateImpl.initWithOwner(new WeakRef(this));
}
}

get checked(): boolean {
return this._getValue(CheckBox.checkedProperty);
return this._getValue(CheckBox.checkedProperty);
}

set checked(value: boolean) {
this._setValue(CheckBox.checkedProperty, value);
}

set fillColor(color: string) {
this._iosCheckbox.onFillColor = new Color(color).ios;
this._fillColor = color;
this._iosCheckbox.onFillColor = new Color(color).ios;
this._fillColor = color;
}

set tintColor(color: string) {
this._tintColor = color;
this._iosCheckbox.onTintColor = new Color(color).ios;
this._tintColor = color;
this._iosCheckbox.onTintColor = new Color(color).ios;
}

/* NATIVE PROPERTIES */
Expand Down Expand Up @@ -136,19 +133,25 @@ export class CheckBox extends Button implements CheckBoxInterface {
public reload(value: boolean) {
this._iosCheckbox.reload();
}

/* END NATIVE PROPERTIES */


public onLoaded() {
super.onLoaded();

var fontSize = this.style.fontSize;
var fontSize;

if (!this.style.fontSize) {
fontSize = 15;
} else {
fontSize = this.style.fontSize;
}
this._iosCheckbox.delegate = this._delegate;
// //Positioning
this._iosCheckbox.frame = CGRectMake(0, 0, fontSize, fontSize);
this._iosCheckbox.center = CGPointMake(this._iosCheckbox.center.x, (fontSize / 2) + 3);

//Positioning
this._iosCheckbox.frame = CGRectMake(0,0,fontSize,fontSize);
this._iosCheckbox.center = CGPointMake( this._iosCheckbox.center.x, (fontSize / 2) + 3);

this.style.paddingLeft = fontSize + (fontSize > 20 ? 10 : 5);
this.style.textAlignment = "left";

Expand All @@ -158,11 +161,11 @@ export class CheckBox extends Button implements CheckBoxInterface {


//Allow label click to change the textbox
this.addEventListener("tap", function(args){
var checkbox = <CheckBox>args.object;
checkbox.checked = !checkbox.checked;
});
this.addEventListener("tap", function (args) {
var checkbox = <CheckBox>args.object;
checkbox.checked = !checkbox.checked;
});

if (typeof this._lineWidth !== 'undefined') {
this.lineWidth = this._lineWidth;
}
Expand Down Expand Up @@ -196,12 +199,12 @@ export class CheckBox extends Button implements CheckBoxInterface {
}

public onUnloaded() {
this._iosCheckbox.delegate = null;
super.onUnloaded();
}
this._iosCheckbox.delegate = null;
super.onUnloaded();
}


public toggle(){
public toggle() {
this.checked = !this.checked;
}

Expand All @@ -223,15 +226,15 @@ export class CheckBox extends Button implements CheckBoxInterface {
}

public _onCheckedPropertyChanged(data: PropertyChangeData) {
if(this._iosCheckbox){
this._iosCheckbox.setOnAnimated(data.newValue, true);
}
if (this._iosCheckbox) {
this._iosCheckbox.setOnAnimated(data.newValue, true);
}
}
}

function onCheckedPropertyChanged(data: PropertyChangeData) {
var checkbox = <CheckBox>data.object;
checkbox._onCheckedPropertyChanged(data);
var checkbox = <CheckBox>data.object;
checkbox._onCheckedPropertyChanged(data);
}


Expand All @@ -240,33 +243,33 @@ function onCheckedPropertyChanged(data: PropertyChangeData) {


class BEMCheckBoxDelegateImpl extends NSObject implements BEMCheckBoxDelegate {
public static ObjCProtocols = [BEMCheckBoxDelegate];
/*public static ObjCExposedMethods = {
"didTapCheckBox": { returns: interop.types.void, params: [NSObject] }
};*/
public static ObjCProtocols = [BEMCheckBoxDelegate];
/*public static ObjCExposedMethods = {
"didTapCheckBox": { returns: interop.types.void, params: [NSObject] }
};*/

private _owner: WeakRef<CheckBox>;
private _owner: WeakRef<CheckBox>;

public static initWithOwner(owner: WeakRef<CheckBox>): BEMCheckBoxDelegateImpl {
let delegate = <BEMCheckBoxDelegateImpl>BEMCheckBoxDelegateImpl.new();
delegate._owner = owner;
return delegate;
}
public static initWithOwner(owner: WeakRef<CheckBox>): BEMCheckBoxDelegateImpl {
let delegate = <BEMCheckBoxDelegateImpl>BEMCheckBoxDelegateImpl.new();
delegate._owner = owner;
return delegate;
}

public animationDidStopForCheckBox(checkBox: BEMCheckBox): void {
//TODO: Maybe trigger event later?
}
public animationDidStopForCheckBox(checkBox: BEMCheckBox): void {
//TODO: Maybe trigger event later?
}

public didTapCheckBox(checkBox: BEMCheckBox): void {
let owner = this._owner.get();
if (owner) {
var eventData = {
eventName: "tap",
object: owner
};

owner.notify(eventData);
owner._onPropertyChangedFromNative(CheckBox.checkedProperty, checkBox.on);
}
public didTapCheckBox(checkBox: BEMCheckBox): void {
let owner = this._owner.get();
if (owner) {
var eventData = {
eventName: "tap",
object: owner
};

owner.notify(eventData);
owner._onPropertyChangedFromNative(CheckBox.checkedProperty, checkBox.on);
}
}
}
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export declare class CheckBox {
}

export interface CheckBoxInterface {
text: string;
text?: string;
checked: boolean;
fillColor: string;
tintColor: string;
Expand Down

This file was deleted.

0 comments on commit 58243f6

Please sign in to comment.