You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Vertical Text Alignment for UILabel?

For UILabel on the iPhone, there is a property for horizontal text alignment which is textAlignment. But when I have a label that can hold 2 lines (numberOfLines = 2), when the text in the label is only one line, that line centers itself in the entire frame (that can hold two lines), when there are two lines, they both fit fine.

What I'd like to happen is if there is one line in a label that holds two, for the line NOT to be in the middle of the frame but to be pushed to the top of the frame as if there were a second line. In other languages (HTML that I can think of offhand) there is such thing as a verticalTextAlignment with options like top, center, bottom.

So, what am I missing here to make just one line of a UILabel to go to the top of its frame and not center itself in the middle of the frame?

Thanks for any direction.

MacBook Pro, Mac OS X (10.4.10)

Posted on Oct 20, 2008 12:55 PM

Reply
32 replies

Dec 10, 2009 3:12 AM in response to HarrisIphone

How can i use the class?
Sample please

Into the .m
#import "VerticallyAlignedLabel.h"

//Name of my uilabel is currField
CGRect newFrame = currField.frame;
VerticallyAlignedLabel *verticalUILabel = [[VerticallyAlignedLabel alloc] init];
[verticalUILabel drawTextInRect:newFrame];
[verticalUILabel setVerticalAlignment:VerticalAlignmentTop];

// This don't work
Please help me.

Dec 24, 2009 5:58 AM in response to DonnieM51

Thank you ivan and brucepdx for this classes! It do a great job for me 🙂

For paolo.andrea just use this classe as a UILabel Classe. Something like that (not tested):
VerticallyAlignedLabel *verticalUILabel = [VerticallyAlignedLabel alloc] init;
verticalUILabel.text = @"my text";
[self.view addSubview: verticalUILabel];
[verticalUILabel release];

Dec 24, 2009 4:06 PM in response to DonnieM51

Hi,

how do I get the horizontal alignment to work for this?

Label2 *myLabel = [[Label2 alloc] initWithFrame:CGRectMake (30,100,200,50);
myLabel.textAlignment=UITextAlignmentRight;

the set textAligment doesn't work, how do I make it use the super?

if I use

UILabel *testLabel=[[UILabel alloc]initWithFrame:CGRectMake(30,100,200,50)];
testLabel.textAlignment=UITextAlignmentRight;

it works fine.

Thanks for all your food,

-

Apr 3, 2010 4:18 AM in response to Jonathan Milenko

SypherF430, BTW you don't need to create Label's subclass like "Label2", the Obj-C's category is the feature just for similar tasks. It's allowed to write directly in your class implementation file:

@interface UILabel (PrivateMethods)
-(void) alignTextTop;
-(void) alignTextBottom;
@end

...

@implementation UILabel (PrivateMethods)

-(void) alignTextTop
{
CGSize textSize = [self.text sizeWithFont:self.font constrainedToSize:self.bounds.size];

CGRect newRect = self.frame;
newRect.size.height = textSize.height;
self.frame = newRect;
}

-(void) alignTextBottom
{
CGSize textSize = [self.text sizeWithFont:self.font constrainedToSize:self.bounds.size];

CGRect newRect = self.frame;
newRect.origin.y += (newRect.size.height - textSize.height);
newRect.size.height = textSize.height;
self.frame = newRect;
}

@end

Jun 17, 2011 3:57 AM in response to Clive Paterson

Hi Clive, did you able to use this class successfully? I am trying to use it but it doesn't make any difference. I tried following two way but no difference. The text still appears horizontally!


1.

VerticallyAlignedLabel *verticalUILabel = [[VerticallyAlignedLabelalloc] initWithFrame:CGRectMake(0, 370, 50, 100)];

verticalUILabel.text = @"10:15";

[self.view addSubview: verticalUILabel];

[verticalUILabel release];


2. Created label in .H file


IBOutletVerticallyAlignedLabel *myLable; => Changed custom class for this label to VerticallyAlignedLabel


On ViedDidLoad => myLable.text = @"10:15"


Still horizontally 😟


Am I missing something? FYI, I want my text appear in iPhone's left bottom corner like the image attached.


User uploaded file

Vertical Text Alignment for UILabel?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.