垂直对齐UILabel中的文本

时间:2012-10-20 07:27:21

标签: iphone ios ipad 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

任何帮助都会得到满足。

5 个答案:

答案 0 :(得分:6)

无法垂直对齐UILabel中的文字。但是,您可以使用sizeWithFont:的{​​{1}}方法动态更改标签的高度,并根据需要设置其x和y。

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

编辑1

而不是UILabel,创建一个像UITableView: -

NO

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

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;

答案 1 :(得分:4)

如果它只是您示例中的单行文本,则可以使用各种变通方法。我个人会创建一个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:@"%c\n", [text characterAtIndex:i]];
    }

    super.text = newString;
}

@end

您现在只需要替换

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

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

获取垂直文字。

答案 2 :(得分:3)

以下代码运行良好

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

答案 3 :(得分: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];

答案 4 :(得分:1)

您不能进行垂直对齐,而是使用其他方式显示它

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];
相关问题