2012-10-20 19 views
5

Sono nuovo per iPhone,Allinea verticalmente il testo in una UILabel

Come allineare verticalmente il mio testo in UILabel?

Ecco il mio frammento di codice,

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

testo visualizzato è nel formato di 123456 ma voglio testo dovrebbe essere visualizzare verticalmente come,

6 
5 
4 
3 
2 
1 

Qualsiasi aiuto sarà appriciated.

+5

Questo non è chiamato allineamento, questo è chiamato direzione di scrittura. L'allineamento è qualcosa di completamente diverso. FYI Non penso che ci sia un metodo per ottenere ciò che vuoi. Magari usando CoreText e CoreGraphics, puoi disegnare un CFStringRef come questo, ma non usare UILabel. –

+0

Ciao, usa la mia risposta Funziona bene. Si prega di utilizzare tour Riferimento –

risposta

6

La sua impossibilità di allineare il testo in UILabel verticale. Tuttavia, puoi modificare in modo dinamico l'altezza dell'etichetta utilizzando il metodo sizeWithFont: di NSString e impostare semplicemente le xe y che desideri.

In alternativa è possibile utilizzare UITextField. Supporta la proprietà contentVerticalAlignment in quanto è una sottoclasse di UIControl. Devi impostare userInteractionEnabled su NO per impedire all'utente di digitare del testo su di esso.

EDIT 1

Bene invece di un UILabel, Creare un UITableView come: -

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 trovato il metodo che utilizza UILabels troppo !! :) 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]; 
+0

http://stackoverflow.com/questions/11332782/how-to-vertically-align-a-uilabel-used-as-a-leftview-in-a-suitextfield-with-the-t .. .. Controlla anche il seguente link .... – IronManGill

+0

Mate, ho provato questo, ma non ha avuto successo '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

+0

Vuoi usare solo un UILabel ?? Coz posso darti un suggerimento migliore .... – IronManGill

1

non puoi fare allineamento verticale, ma usare un altro modo per visualizzarlo

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

Hi il seguente codice è un lavoro ben

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

Se solo un unico testo allineato come nel tuo esempio è possibile utilizzare diverse soluzioni alternative. Personalmente mi creare una sottoclasse UILabel come segue,

@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 

Ora basta sostituire

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

con

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

per ottenere il testo verticale.

+0

+1 Buona risposta! – Hemang

Problemi correlati