垂直对齐UILabel中的文本

我是iPhone新手,

我如何垂直对齐UILabel中的文本?

这是我的代码片段,

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

显示的文本格式为123456但我想要文本应垂直显示,

6
5
4
3
2
1

任何帮助将appriciated。


无法将UILabel的文本垂直对齐。 但是,您可以使用NSString sizeWithFont:方法动态更改标签的高度,并根据需要设置它的x和y。

作为替代方案,您可以使用UITextField 。 它支持contentVerticalAlignment peoperty,因为它是UIControl的子类。 您必须将其userInteractionEnabled设置为NO以防止用户在其上键入文本。

编辑1

那么,而不是一个UILabel,使一个UITableView像: -

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;

编辑2找到了使用UILabels的方法! :) 看一下这个....

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

如果它只是一个单行的文本,就像在你的例子中一样,你可以使用各种解决方法。 我个人会创建一个UILabel子类如下,

@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

你现在只需要更换

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

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

获得垂直文本。


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

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/28291.html

上一篇: Vertically align text in a UILabel

下一篇: Autolayout: Add constraint to superview and not Top Layout Guide?