2010-10-29 17 views
0

J'essaie de comprendre comment prendre les caractères individuels dans un objet NSString et créer des UILabels à partir d'eux, avec le jeu de caractères UILabel au caractère individuel.Comment puis-je convertir les caractères d'un objet NSString en objets UILabel?

Je suis nouveau à Cocoa, mais jusqu'à présent je cette ...

NSString *myString = @"This is a string object"; 

for(int i = 0; i < [myString length]; i++) 
{ 
    //Store the character 
    UniChar chr = [myString characterAtIndex:i]; 

    //Stuck here, I need to convert character back to an NSString object so I can... 

    //Create the UILabel 
    UILabel *lbl = [[UILabel alloc] initWithFrame....]; 
    [lbl setText:strCharacter]; 

    //Add the label to the view 
    [[self view] addSubView:lbl]; 
} 

A part où je suis coincé, mon approche se sent déjà très hackish, mais je suis un noob et encore apprentissage. Toute suggestion sur la façon d'aborder cela serait très utile.

Merci beaucoup pour votre aide!

+1

Vous fuit les étiquettes –

+1

Par curiosité, pourquoi voudriez-vous faire cela? Vous pouvez potentiellement créer beaucoup de UILabels, ce qui n'est pas très efficace. – Rits

+0

c'est juste un moyen rapide de tester des fonctionnalités de jeu pour un jeu sur lequel je travaille. c'est temporaire. – BeachRunnerFred

Répondre

4

Vous voulez utiliser -substringWithRange: avec une chaîne de la longueur 1.

NSString *myString = @"This is a string object"; 

NSView *const parentView = [self superview]; 
const NSUInteger len = [myString length]; 
for (NSRange r = NSMakeRange(0, 1); r.location < len; r.location += 1) { 
    NSString *charString = [myString substringWithRange:r]; 

    /* Create a UILabel. */ 
    UILabel *label = [[UILabel alloc] initWithFrame....]; 
    [lbl setText:charString]; 

    /* Transfer ownership to |parentView|. */ 
    [parentView addSubView:label]; 
    [label release]; 
} 
+0

wow, cela semble beaucoup moins hacky. Je vous remercie! – BeachRunnerFred