Skip to content

Instantly share code, notes, and snippets.

@thecfguy
Forked from anonymous/index.js
Created March 21, 2016 11:46
Show Gist options
  • Save thecfguy/89c666f0ba5b0eb48f33 to your computer and use it in GitHub Desktop.
Save thecfguy/89c666f0ba5b0eb48f33 to your computer and use it in GitHub Desktop.
Arrange labels for all kind of density
function dpToPixel(dp) {
return ( parseInt(dp) * (Titanium.Platform.displayCaps.dpi / 160));
}
// convert pixel to dp.
function pixelToDp(px,nounit) {
return ( parseInt(px) / (Titanium.Platform.displayCaps.dpi / 160)) + (nounit?0:'dp');
}
var reArrangeLable = function(){
var screenwidth = pixelToDp($.vw.toImage().width,true);
var screenheight = pixelToDp($.vw.toImage().height,true);
Ti.API.info([screenwidth,screenheight]);
$.lefttop.top = 0;
$.lefttop.left = 0;
$.righttop.top = 0;
$.righttop.left = (screenwidth -$.righttop.width - 20) + 'dp';
$.center.top = (screenheight/2 - $.center.height/2) + 'dp';
$.center.left = pixelToDp(Ti.Platform.displayCaps.platformWidth/2,true)-($.center.width/2) + 'dp';
$.leftbottom.top = screenheight - $.leftbottom.height;
$.leftbottom.left = 0;
$.rightbottom.top = screenheight - $.rightbottom.height;
$.rightbottom.left = (screenwidth -$.rightbottom.width) + 'dp';
};
Ti.Gesture.addEventListener('orientationchange',function(e) {
setTimeout(reArrangeLable,1000);
});
$.index.open();
setTimeout(reArrangeLable,1000);
<Alloy>
<Window class="container">
<View id="vw">
<Label id="lefttop" onClick="doClick" backgroundColor="#12ef43">lefttop</Label>
<Label id="righttop" onClick="doClick" backgroundColor="#ff1243">righttop</Label>
<Label id="leftbottom" onClick="doClick" backgroundColor="#12ef43">leftbottom</Label>
<Label id="rightbottom" onClick="doClick" backgroundColor="#ff1243">rightbottom</Label>
<Label id="center" onClick="doClick" backgroundColor="#ff1243">center</Label>
</View>
</Window>
</Alloy>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment