5

I am new to iPhone,

How do I vertically align my text in the UILabel?

Here is my Code snippet,

    UILabel *lbl=[[UILabel alloc]init];
    lbl.frame=CGRectMake(10,10,100,100);
    lbl.backgroundColor=[UIColor clearColor];
    lbl.font=[UIFont systemFontOfSize:13];
    lbl.text=@"123456";
    [scrollVw addSubview:lbl];

text displayed is in the format of 123456 but i want text should be display vertically like,

6
5
4
3
2
1

Any help will be appriciated.

Krunal
  • 6,440
  • 21
  • 91
  • 155
  • 5
    This is not called alignment, this is called writing direction. Alignment is something completely different. FYI I don't think there's a method for achieving what you want. Maybe using CoreText and CoreGraphics, you can draw a CFStringRef like this, but not using UILabel. –  Oct 20 '12 at 07:29
  • Hi, Use My Answer It's work Well. Please use tour Reference –  Oct 20 '12 at 07:57

5 Answers5

6

Its impossible to align the text in UILabel vertically. But, you can dynamically change the height of the label using sizeWithFont: method of NSString, and just set its x and y as you want.

As an alternative you can use UITextField. It supports the contentVerticalAlignment peoperty as it is a subclass of UIControl. You have to set its userInteractionEnabled to NO to prevent user from typing text on it.

EDIT 1

Well instead of a UILabel , Make a UITableView like :-

TableActivityLevel=[[UITableView alloc] initWithFrame:CGRectMake(224, 203, 27, 0) style:UITableViewStylePlain];
TableActivityLevel.delegate=self;
TableActivityLevel.dataSource=self;
TableActivityLevel.autoresizingMask = UIViewAutoresizingFlexibleHeight; 
TableActivityLevel.rowHeight = 17;
TableActivityLevel.backgroundColor=[UIColor clearColor];    
TableActivityLevel.layer.borderColor = [[UIColor clearColor]CGColor];
TableActivityLevel.separatorStyle = UITableViewCellSeparatorStyleNone;

EDIT 2 Found the method using UILabels too !! :) Check this out....

UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 10.0, 100.0)];
[label1 setBackgroundColor:[UIColor clearColor]];
[label1 setNumberOfLines:0];
[label1 setCenter:self.view.center];
[label1 setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];
[label1 setTextColor:[UIColor whiteColor]];
[label1 setTextAlignment:UITextAlignmentCenter];
[label1 setText:@"1 2 3 4 5 6"];
[self.view addSubview:label1];
IronManGill
  • 7,222
  • 2
  • 31
  • 52
  • http://stackoverflow.com/questions/11332782/how-to-vertically-align-a-uilabel-used-as-a-leftview-in-a-uitextfield-with-the-t .... Check the following link too .... – IronManGill Oct 20 '12 at 07:39
  • Mate,i tried this but didn't success `textfield=[[UITextField alloc]init]; UILabel*startsWith =[[UILabel alloc] init]; startsWith.frame=CGRectMake(20, 20, 20, 20); startsWith.font= self.textfield.font; startsWith.textAlignment= self.textfield.textAlignment; startsWith.textColor = [UIColor blackColor]; startsWith.backgroundColor = [UIColor clearColor]; startsWith.text = @"123456"; [startsWith sizeToFit]; startsWith.frame = CGRectOffset(startsWith.frame, 0, -1); [scrollVw addSubview:startsWith]; self.textfield.leftViewMode = UITextFieldViewModeAlways;` – Krunal Oct 20 '12 at 07:50
  • Obviously you need to add something like [self.View addSubView: TableActivityLevel]; ... Have you done that ? – IronManGill Oct 20 '12 at 08:16
  • yes i added.. and also removed this line TableActivityLevel.hidden = YES; – Krunal Oct 20 '12 at 08:17
4

If it just a single lined text as in your example you can use various workarounds. I personally would create a UILabel subclass as follows,

@implementation VerticalLabel

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.numberOfLines = 0;
    }
    return self;
}

- (void)setText:(NSString *)text {
    NSMutableString *newString = [NSMutableString string];
    for (int i = text.length-1; i >= 0; i--) {
        [newString appendFormat:@"%c\n", [text characterAtIndex:i]];
    }

    super.text = newString;
}

@end

You now just have to replace

UILabel *lbl = [[UILabel alloc] init];

with

UILabel *lbl = [[VerticalLabel alloc] init];

to get vertical text.

Basit Ali
  • 619
  • 8
  • 10
3

Hi the following code is work well

UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,10,10,300);
lbl.backgroundColor=[UIColor clearColor];
lbl.numberOfLines=6; // Use one to 6 numbers
lbl.font=[UIFont systemFontOfSize:13];
lbl.text=@"123456";

NSString *reverse=lbl.text;
NSMutableString *reversedString = [NSMutableString string];
NSInteger charIndex = [reverse length];
while (charIndex > 0) {
    charIndex--;
    NSRange subStrRange = NSMakeRange(charIndex, 1);
    [reversedString appendString:[reverse substringWithRange:subStrRange]];
}
NSLog(@"%@", reversedString);


CGSize labelSize = [lbl.text sizeWithFont:lbl.font
                          constrainedToSize:lbl.frame.size
                              lineBreakMode:lbl.lineBreakMode];
lbl.frame = CGRectMake(
                         lbl.frame.origin.x, lbl.frame.origin.y,
                         lbl.frame.size.width, labelSize.height);


lbl.text=reversedString;

 [scrollview addSubview:lbl];
2
UILabel *lbl=[[UILabel alloc]init];
    lbl.frame=CGRectMake(10,10,10,100);
    lbl.backgroundColor=[UIColor clearColor];
    lbl.font=[UIFont systemFontOfSize:13];
    lbl.text=@"123456";
    lbl.numberOfLines = 10;
    [self.view addSubview:lbl];
Prasad G
  • 6,702
  • 7
  • 42
  • 65
1

You cant do vertical allignment but use another way to display it

UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,10,100,400);
lbl.backgroundColor=[UIColor clearColor];
lbl.font=[UIFont systemFontOfSize:13];
lbl.text=@"1\n2\n3\n4\n5\n6";
[scrollVw addSubview:lbl];
Rahul Patel
  • 5,858
  • 6
  • 46
  • 72