Getting system date, time in Corona SDK

For getting system date, time in Corona SDK, use the following code,

local date = os.date( "*t" )    -- returns table of date & time values
print( date.year, date.month )  -- print year and month
print( date.hour, date.min )    -- print hour and minutes
 
print( os.date( "%c" ) )        -- print out time/date string: e.g., "Thu Nov 18 14:55:02 2011"

Audio file handling in sencha

	
var mpanel = new Ext.Panel({
fullscreen: true,
items: [
{
xtype: 'audio',
url  : "my-music.mp3"
}
]
});

Params & functions:

url: Location of the audio to play.

preload: Will begin preloading the media immediately.

pause: Pauses media playback.

play: Starts or resumes media playback.

remove: Removes a component from this container.

removeAll: Removes all components from this container.

nextSibling: Returns the next sibling of this Component.

insert: Inserts a Component into this Container at a specified index.

hide: Hide the component.

isDisabled: Method to determine whether this Component is currently disabled.

 

PHP func_get_arg() example of by ref and by val arguments

<?php
function byVal($arg) {
    echo 'As passed     : ', var_export(func_get_arg(0)), PHP_EOL;
    $arg = 'baz';
    echo 'After change  : ', var_export(func_get_arg(0)), PHP_EOL;
}

function byRef(&$arg) {
    echo 'As passed     : ', var_export(func_get_arg(0)), PHP_EOL;
    $arg = 'baz';
    echo 'After change  : ', var_export(func_get_arg(0)), PHP_EOL;
}

$arg = 'bar';
byVal($arg);
byRef($arg);
?>

How to search nsmutablearray

You can do “searching” of arrays using predicates, like so:

NSMutableArray* names = [NSMutableArray arrayWithObjects:@"Andy", @"Bart", @"Bob", nil]; 
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] 'b'"];
NSArray* namesStartingWithB = [names filteredArrayUsingPredicate: predicate];

// namesStartingWithB now contains @”Bart” & @”Bob”

Adding a Market Link in Android App

We know about the Linnkify class in android. Have you ever wondered that we can use this same class to add a market link?
Here is a sample code for the same.


TextView txtView = (TextView) findViewById(R.id.linkme);

Pattern WordMatcher = Pattern.compile("\\b[A-Z]+[a-z0-9]+[A-Z][A-Za-z0-9]+\\b");
String MarketURL = "market://details?id=com.schogini.geeta.pack&hl=pt_PT";
Linkify.addLinks(txtView, WordMatcher, MarketURL);

Sencha Touch Custom Picker Form Field

Currently Sencha Touch has only a date picker for forms. But, I wanted a custom picker (like in XCode) where I can define the columns and values. So this is my quick fix solution. I have used the datePicker form field code as a reference. Please feel free to correct or enhance. It would be great and helpful if, you could post back your changes.

/**
 * @class Ext.form.SchPicker
 * @extends Ext.form.Field
 * 
 
Specialized field which has a button which when pressed, shows a {@link Ext.Picker}
with columns that you have defined.

 * @xtype schpickerfield
 * @author Gayatri S Ajith 
 */
Ext.form.SchPicker = Ext.extend(Ext.form.Field, {
    ui: 'select',
    picker: null,
    destroyPickerOnHide: false,
    initComponent: function() {
        this.addEvents(
            'change'
        );

        this.tabIndex = -1;
        this.useMask = true;
        Ext.form.Text.superclass.initComponent.apply(this, arguments);
    },
    getSchPicker: function() {
        if (!this.schPicker) {
            if (this.picker instanceof Ext.Picker) {
                this.schPicker = this.picker;
            } else {
                this.schPicker = new Ext.Picker(Ext.apply(this.picker || {}));
            }

            this.schPicker.setValue(this.value || null);
            this.schPicker.on({
                scope : this,
                change: this.onPickerChange,
                hide  : this.onPickerHide
            });
        }

        return this.schPicker;
    },
    onMaskTap: function() {
        if (Ext.form.SchPicker.superclass.onMaskTap.apply(this, arguments) !== true) {
            return false;
        }
        
        this.getSchPicker().show();
    },
    
    /**
     * Called when the picker changes its value
     * @param {Ext.SchPicker} picker The date picker
     * @param {Object} value The new value from the date picker
     * @private
     */
    onPickerChange : function(picker, value) {
        this.setValue(value);
        this.fireEvent('change', this, this.getValue());
    },
    
    /**
     * Destroys the picker when it is hidden, if
     * {@link Ext.form.SchPicker#destroyPickerOnHide destroyPickerOnHide} is set to true
     * @private
     */
    onPickerHide: function() {
        if (this.destroyPickerOnHide && this.schPicker) {
            this.schPicker.destroy();
        }
    },

    // inherit docs
	/**
 	 * this will set the value of the picker and will also store the value
	 * in the format specified. If format is not specified then, it will 
	 * just concat the columns separated with a pipe(|)
	 */
    setValue: function(value, animated) {
		this.value = value;
        if (this.schPicker) {
            this.schPicker.setValue(value, animated);
            this.value = (value != null) ? this.schPicker.getValue() : null;
        }
		if (Ext.isObject(this.value)) {
			if (!this.valueFormat) {
				var str = '';
				for (var k in this.value) {
					str += this.value[k] + '|';
				}
				this.value = str.substr(0, str.length-1);
			} else {
				var i = 0;
				var str = this.valueFormat;
				for (var k in this.value) {
					str = str.replace(k, this.value[k])
				}
				this.value = str;
			}
		}
        if (this.rendered) {
            this.fieldEl.dom.value = this.getValue(true);
        }
		
        return this;
    },
    
    
	/**
     * Returns the value of the field, which will be a {@link Date} unless the format parameter is true.
     * @param {Boolean} format True to format the value with Ext.util.Format.defaultDateFormat
     */
    getValue: function(format) {
        var value = this.value || null;
        return value;
    },
    
    // @private
    onDestroy: function() {
        if (this.schPicker) {
            this.schPicker.destroy();
        }
        
        Ext.form.SchPicker.superclass.onDestroy.call(this);
    }
});

Ext.reg('schpickerfield', Ext.form.SchPicker);

How to use it:

{
	xtype: 'form',
	items: [{
		xtype: 'fieldset',
		defaults: {
			labelWidth: '30%'
		},
		title: 'Event Form',
		items: [{
			xtype: 'textfield',
			name: 'event',
			label: 'Event Name',
		}, {
			xtype: 'schpickerfield',
			name: 'time',
			label: 'Event Time',
			value: '',
			valueFormat: 'hour:mins ampm', // note that we are using the column names in the format. 
										   // if the could names do not match then, it will not be replaced.
			picker: {
				useTitles: true,
				slots: [
					{
						name : 'hour',
						title: 'Hour',
						data : [
							{text: '1', value: '1'},
							{text: '2', value: '2'},
							{text: '3', value: '3'},
							{text: '4', value: '4'},
							{text: '5', value: '5'},
							{text: '6', value: '6'},
							{text: '7', value: '7'},
							{text: '8', value: '8'},
							{text: '9', value: '9'},
							{text: '10', value: '10'},
							{text: '11', value: '11'},
							{text: '12', value: '12'},
						]
					}, {
						name : 'mins',
						title: 'Min',
						data : [
							{text: '00', value: '00'},
							{text: '15', value: '15'},
							{text: '30', value: '20'},
							{text: '45', value: '45'},
						]
					}, {
						name : 'ampm',
						title: 'AM/PM',
						data : [
							{text: 'am', value: 'am'},
							{text: 'pm', value: 'pm'}
						]
					}
				]
			}
		}, {
			xtype: 'selectfield',
			name: 'frequency',
			label: 'Repeat',
			options: [
				{text: 'Every day', value: 'every day'},
				{text: 'Every week', value: 'every week'},
				{text: 'Every month', value: 'every month'},
				{text: 'Every 6 months', value: 'every 6 months'},
				{text: 'Every year', value: 'every year'},
			]
		}]
	}]
}

Converting HEX Color code to UIColor in Xcode

Include the following in the project where you need to use this.

#define UIColorFromHEXValue(hexValue) [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 green:((float)((hexValue & 0xFF00) >> 8))/255.0 blue:((float)(hexValue & 0xFF))/255.0 alpha:1.0]

Adding this will make the method UIColorFromHEXValue available throughout the class.

Now you can call this method when you need to convert hexadecimal colorcode to UIColor. If you want to convert hex value FFFFFF to UIColor, then then use it like this.

UIColorFromRGB(0xFFFFFF)  - This will return UIColor corresponding to hex colorcode FFFFFF.

 

 

Create table with css without using table, tr, td tags

You can create tables with css without using <table>, <tr>, <td> tags:

<html><title>CSS Table</title>
<style type="text/css">
.main{
float:left;
height:540px;
width: 800px;
}
.form_style{
clear:both;
}
.cell{
height:60px;
width:60px;
color:#dddddd;
border: 1px solid #999999;
text-align:center;
float:left;
}
.cell-last{
height:60px;
width:60px;
color:#dddddd;
border: 1px solid #999999;
text-align:center;
float:left;
clear:left;
}
</style>
</script>
</head>
<body>
<div>
<?php for ($i=0; $i<8; $i++) {
for( $j=0; $j<10; $j++ ){
if( $i > 0 && $j == 0 ){
?>
<div id="cell_<?php echo $i.'_'.$j; ?>"></div>

<?php }else{ ?>

<div id="cell_<?php echo $i.'_'.$j;?>"></div>
<?php         }
}
}
?>
</div>
</body>
</html>

Adding b2Body of Box2d or Other Pointers as Objects to NSMutableArray

Consider a NSMutableArray object named mutableFixtureArray.

NSValue *bodyVal = [NSValue valueWithPointer:ballFixture]; //Converting the fixture of the body we need to add to the array to NSValue.

[mutableFixtureArray addObject:bodyVal]; //Adding the NSValue corresponding to the fixture to the mutableArray.

 

Getting the body back from the NSMutableArray

NSValue *val = [mutableFixtureArray objectAtIndex:0]; //Copying the value in the array at a particular index(here zero) to a NSValue object.

b2Fixture *currentFixture;

[val getValue:&currentFixture];//Assigning the fixture value from the array to the b2Fixture object. Now the currentFixture contains the fixture stored into the array.

b2Body *theBody;

theBody = currentFixture->GetBody(); //Getting the body from the fixture.