2012-10-20 81 views
5

我是iPhone新手,垂直對齊UILabel中的文本

如何垂直對齊UILabel中的文本?

這裏是我的代碼片段,顯示

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

文本是在123456格式,但我希望文本應顯示垂直狀,

6 
5 
4 
3 
2 
1 

任何幫助將appriciated。

+5

這不稱爲對齊,這被稱爲書寫方向。對齊是完全不同的。僅供參考我不認爲有實現你想要的方法。也許使用CoreText和CoreGraphics,你可以像這樣畫一個CFStringRef,但不能使用UILabel。 – 2012-10-20 07:29:11

+0

嗨,使用我的答案這是工作好了。請使用參考 – 2012-10-20 07:57:40

回答

6

它無法對齊文本中UILabel垂直。但是,您可以使用sizeWithFont:方法NSString動態更改標籤的高度,只需根據需要設置它的x和y即可。

作爲替代方案,您可以使用UITextField。它支持contentVerticalAlignment屬性,因爲它是UIControl的子類。您必須將其userInteractionEnabled設置爲NO以防止用戶在其上鍵入文本。

EDIT 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]; 
+0

http://stackoverflow.com/questions/11332782/how-to-vertically-align-a-uilabel-used-as-a-leftview-in-a-uitextfield-with-the-t .. ..也檢查以下鏈接.... – IronManGill

+0

配合,我試過但沒有成功'textfield = [[UITextField alloc] init]; UILabel * startsWith = [[UILabel alloc] init]; startingWith.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

+0

你是否只想使用UILabel?因爲我可以給你一個更好的建議.... – IronManGill

1

你不能做垂直allignment而是用另一種方式來顯示它

UILabel *lbl=[[UILabel alloc]init]; 
lbl.frame=CGRectMake(10,10,100,400); 
lbl.backgroundColor=[UIColor clearColor]; 
lbl.font=[UIFont systemFontOfSize:13]; 
[email protected]"1\n2\n3\n4\n5\n6"; 
[scrollVw addSubview:lbl]; 
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]; 
[email protected]"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]; 
    [email protected]"123456"; 
    lbl.numberOfLines = 10; 
    [self.view addSubview:lbl]; 
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]; 

獲得垂直文本。

+0

+1好答案! – Hemang