Skip to content

Commit

Permalink
[TIMOB-2574] Implement vertical align for IOS labels
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalduggal committed Jul 10, 2012
1 parent 2f2b552 commit 00e3d13
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
4 changes: 2 additions & 2 deletions iphone/Classes/TiUILabel.h
Expand Up @@ -15,8 +15,8 @@
BOOL requiresLayout;
CGRect padding;
BOOL repad;
CGRect initialLabelFrame;
UIControlContentVerticalAlignment verticalAlign;
CGRect initialLabelFrame;
}

@property(nonatomic,getter=isHighlighted) BOOL highlighted; // default is NO
Expand Down
34 changes: 33 additions & 1 deletion iphone/Classes/TiUILabel.m
Expand Up @@ -72,7 +72,31 @@ -(CGFloat)contentHeightForWidth:(CGFloat)width

-(void)padLabel
{
[label setFrame:initialLabelFrame];
CGSize actualLabelSize = [self sizeForFont:initialLabelFrame.size.width];
CGFloat originX = (initialLabelFrame.size.width - actualLabelSize.width)/2.0;
if (originX < 0) {
originX = 0;
}
CGRect labelRect = CGRectMake(originX, 0, actualLabelSize.width, actualLabelSize.height);
switch (verticalAlign) {
case UIControlContentVerticalAlignmentBottom:
labelRect.origin.y = initialLabelFrame.size.height - actualLabelSize.height;
break;
case UIControlContentVerticalAlignmentCenter:
labelRect.origin.y = (initialLabelFrame.size.height - actualLabelSize.height)/2;
if (labelRect.origin.y < 0) {
labelRect.size.height = (initialLabelFrame.size.height - labelRect.origin.y);
}
break;
default:
if (initialLabelFrame.size.height < actualLabelSize.height) {
labelRect.size.height = initialLabelFrame.size.height;
}
break;
}

[label setFrame:CGRectIntegral(labelRect)];

if (repad &&
backgroundView != nil &&
!CGRectIsEmpty(initialLabelFrame))
Expand Down Expand Up @@ -112,6 +136,7 @@ -(UILabel*)label
label.backgroundColor = [UIColor clearColor];
label.numberOfLines = 0;
[self addSubview:label];
self.clipsToBounds = YES;
}
return label;
}
Expand Down Expand Up @@ -140,6 +165,13 @@ -(BOOL)isHighlighted

#pragma mark Public APIs

-(void)setVerticalAlign_:(id)value
{
verticalAlign = [TiUtils intValue:value def:1];
if (label != nil) {
[self padLabel];
}
}
-(void)setText_:(id)text
{
[[self label] setText:[TiUtils stringValue:text]];
Expand Down
6 changes: 6 additions & 0 deletions iphone/Classes/TiUILabelProxy.m
Expand Up @@ -14,6 +14,12 @@ @implementation TiUILabelProxy

USE_VIEW_FOR_CONTENT_WIDTH

-(void)_initWithProperties:(NSDictionary *)properties
{
[self initializeProperty:@"verticalAlign" defaultValue:NUMINT(1)];
[super _initWithProperties:properties];
}

-(CGFloat)contentHeightForWidth:(CGFloat)suggestedWidth
{
NSString *value = [TiUtils stringValue:[self valueForKey:@"text"]];
Expand Down

0 comments on commit 00e3d13

Please sign in to comment.