Vertically align text in a UILabel

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.


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];

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:@"%cn", [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.


嗨下面的代码是很好的工作

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];
链接地址: http://www.djcxy.com/p/28292.html

上一篇: 使用自动收缩在UILabel中垂直居中

下一篇: 垂直对齐UILabel中的文本