Skip to content

Instantly share code, notes, and snippets.

@tzmartin
Created September 3, 2013 17:49
Show Gist options
  • Save tzmartin/6427217 to your computer and use it in GitHub Desktop.
Save tzmartin/6427217 to your computer and use it in GitHub Desktop.
Native dynamic height calculation for UIViews in Titanium Mobile SDK. Useful for situations where getSize(), getRect() or postLayout event is not sufficient. Typically needed when view dimensions are set to Ti.UI.FILL or Ti.UI.SIZE. Add this method to TiUiLabelProxy.m, TiUITableViewProxy.m Reference: http://developer.appcelerator.com/question/14…
-(id)suggestHeight:(id)arg
{
CGFloat height = 0;
// resolve arg
if (arg != nil && [arg isKindOfClass:[NSArray class]])
{
NSArray *args = (NSArray *)arg;
if (args.count > 0)
arg = [args objectAtIndex:0];
else
arg = nil;
}
// if no arg then use defined width for the label
if (arg == nil)
arg = [self valueForKey:@"width"];
// if arg set then compute the width
if (arg != nil)
{
TiDimension d = [TiUtils dimensionValue:arg];
CGFloat width = 0;
if (d.type == TiDimensionTypeDip)
width = d.value;
else if (d.type == TiDimensionTypePercent && self.parent != nil)
width = [self.parent.size.width floatValue] * d.value;
if (width>0)
height = [self contentHeightForWidth:width];
}
return [NSNumber numberWithFloat:height];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment