Dynamically Create Object Within Loop - Objective C - objective-c

I'm looking for a way to dynamically create NSString objects in objective C based on how many of them I need (between 1 and 5). I then want to use those strings as names of objects which also are dynamically created;
Pseudo Code:
for (i=1, i <= number_of_characters, i++)
{
NSMutableString* theString = [NSMutableString character];
[theString appendString:[NSString stringWithFormat:#"%i ",i]];
UILabel *theString;
[theString release];
}
and I am hoping to get several UILabel objects named:
character1
character2
character3
and so on...
Thanks!

You can create UILabel objects on the fly, but you can't create variables at runtime. If you want to set the text of the label to theString, that's no problem:
NSMutableArray *labels = [NSMutableArray array];
for (i=1, i <= number_of_characters, i++)
{
NSMutableString* theString = [NSString stringWithFormat:#"%i ",i];
UILabel *label = [[UILabel alloc] initWithFrame:someCGRect];
label.text = theString;
[labels addObject:label];
[theString release];
}
Now you've got an array full of labels, each of which has a number as its text. The labels haven't been added to any view yet, so you'll want to take care of that.

Related

Attributed text disappears in UITextView- Objective C

I have an array which has index 0 already filled, and this array stores NSStrings. I also have a "parallel" array which has index 0 already filled, and it stores NSNumbers.
In implementation:
#implementation ViewController
NSMutableArray *intonationStrings;
NSMutableArray *intonationSizes;
In viewDidLoad():
- (void)viewDidLoad {
[super viewDidLoad];
intonationStrings = [[NSMutableArray alloc] initWithObjects:#"placeholderxyz", nil];
NSNumber *placeholderXYZ = [NSNumber numberWithInt:0];
intonationStrings = [[NSMutableArray alloc] initWithObjects:placeholderXYZ, nil];
_resizeLyrics.minimumValue = 9;
_resizeLyrics.maximumValue = 40;
There is a slider with a minimum value of 9 and maximum of 40 which represents font size. There is a text field, and it's current text is used to create an NSRange within a text view to begin attributing the text. This action takes place inside of a button action:
-(IBAction)applyResizeAct:(id)sender{
NSString *lyricString = _lyricPad.text;
NSString *searchString = _searchLyrics.text;
NSMutableAttributedString *lyricStringMut = [[NSMutableAttributedString alloc] initWithString: lyricString];
float sliderValue = _resizeLyrics.value;
[intonationStrings addObject:searchString];
[intonationSizes addObject:[NSNumber numberWithInt:sliderValue]];
for (int i=1; i<[intonationStrings count]; i++){
NSRange searchStringRange = [lyricString rangeOfString:intonationStrings[i]];
NSInteger searchStringNewSize = [intonationSizes[i] integerValue];
int searchStringNewSizeInteger = (int) searchStringNewSize;
[lyricStringMut addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Menlo" size:searchStringNewSizeInteger]
range:searchStringRange];
}
self.lyricPad.attributedText = lyricStringMut;
}
The issue is I can't seem to figure out why typing "Pasta" in the text field, setting the slider to about 25 and tapping the button simply makes the word "Pasta" in the text view holding "Pasta is good." just disappear. My attribute should be applied: Font type Menlo at size 25 for "Pasta" and other sizes applied to strings in the parallel mutable array. Anything helps. Thanks in advance!

How to view the entire content of an array in a label (xcode 4.1)

i'm programming in Obj-c with xcode4.1, i have an array with numbers in it, and i want to visualize all of them in a label...can anyone help me around this please?
thanks!
this is the code:
combinedString=[[NSMutableArray alloc] init];
NSString *finalStringLabel=#"";
for (i=0; i<=textLength; i++) {
//character coding
char myChar = [myString characterAtIndex:i];
NSString *myCharS=[NSString stringWithFormat:#"%c", myChar];
int asciiCode=[myCharS characterAtIndex:0];
NSString *asciiS=[NSString stringWithFormat:#"%i", asciiCode];
[combinedString addObject:asciiS];
}
finalStringLabel=[NSString stringWithFormat:#"", [combinedString componentsJoinedByString:#"."]];
myLabel.text=finalStringLabel;
[combinedString release];
}
You can use this
NSArray *yourArray;
NSString *createdString = [yourArray componentsJoinedByString:#" "];
myLabel.text = createdString;
As your array is combinedString,
combinedString=[[NSMutableArray alloc] init];
looks like you are providing values after this line or this is not a property (this is a local as you are releasing it later), and your code in not complete.
Anyways,
You don't need to create an empty string and then assign new object to it, need to do as :
myLabel.text=[combinedString componentsJoinedByString:#"."];
[combinedString release];
}

getting text from uilabel in objective-c

I have a project where I am trying to grab text from a uilabel that has been populated from a web service. I can grab and manipulate the text just fine but I need to send certain characters from the string to another web service call. I can not figure out how to grab the first, second and last characters in my string. I am both new to Objective-C as well as programming so any help would be much appreciated.
You can do it like this:
UILabel * l = [[UILabel alloc] init];
l.text = #"abcdef"; //set text to uilabel
[self.view addSubview:l];
NSString * text = l.text; //get text from uilabel
unichar first = [text characterAtIndex:0]; //get first char
unichar second = [text characterAtIndex:1];
unichar last = [text characterAtIndex:text.length -1];
If you need results as strings you can use:
NSString * firstAsString = [text substringWithRange:NSMakeRange(0, 1)]; //first character as string
or you can convert the unichar to string like this:
NSString * x = [NSString stringWithFormat:#"%C", last];
Per this it should be fairly easy:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UILabel_Class/Reference/UILabel.html
textLabel.text = #"Foo";
Where textLabel is instance of UILabel

NSMutableDictionary containing UIImageViews...Add mutable copy to an NSMutableArray? Alternative?

I'm trying to build words in arrays by their key value in a dictionary. However, it won't work the way I'd like it to, because I can't "copy" a UIImageView.
It would be PERFECT, if when adding the letter from the dictionary to an array, it gives me a copy instead of the real object. I don't want to make multiple images of the same letter and add it to the dictionary, because then I could no longer call it by key "s" or "a", and I need more than one array to use the same letters at at time.
What can I do?
//How I create the letters
char s = 's';
NSString *key = [NSString stringWithFormat:#"%c", s];
alphabetS = [[UIImageView alloc] init]];
[alphabetS setImage:[UIImage imageNamed:#"s.png"]];
[allTilesDictionary setObject:alphabetS forKey:key];
[alphabetS release];
//How I use the imageviews from the dictionary
NSMutableArray *wordOne = [[NSMutableArray alloc] initWithObjects:[allTilesDictionary objectForKey:#"s"],[allTilesDictionary objectForKey:#"h"],[allTilesDictionary objectForKey:#"o"],[allTilesDictionary objectForKey:#"p"], nil];
EDIT: My solution. It works perfectly.
for (UIImageView *letters in wordOne)
{
newLetter = [[UIImageView alloc] init];
newLetter.image = letters.image;
newLetter.userInteractionEnabled = YES;
//I can now either lay them out wherever I want on the view, or add them to a new array.
}
Your original code, slightly modified:
//How I create the letters
char s = 's';
NSString *key = [NSString stringWithFormat:#"%c", s];
[allTilesDictionary setObject:[UIImage imageNamed:#"s.png"] forKey:key];
//How I use the imageviews from the dictionary
NSMutableArray *wordOne = [[NSMutableArray alloc] initWithObjects:[allTilesDictionary objectForKey:#"s"],[allTilesDictionary objectForKey:#"h"],[allTilesDictionary objectForKey:#"o"],[allTilesDictionary objectForKey:#"p"], nil];
Your use of that code, slightly modified:
for (UIImage *letters in wordOne)
{
newLetter = [[UIImageView alloc] init];
newLetter.image = letters;
newLetter.userInteractionEnabled = YES;
//I can now either lay them out wherever I want on the view, or add them to a new array.
}
No unnecessary UIImageViews created.
for (UIImageView *letters in wordOne)
{
newLetter = [[UIImageView alloc] init];
newLetter.image = letters.image;
newLetter.userInteractionEnabled = YES;
//I can now either lay them out wherever I want on the view, or add them to a new array.
}

How can I convert the characters in an NSString object to UILabel objects?

I'm trying to figure out how to take the individual characters in an NSString object and create UILabels from them, with the UILabel text set to the individual character.
I'm new to Cocoa, but so far I have this...
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];
}
Aside from where I'm stuck, my approach already feels very hackish, but I'm a noob and still learning. Any suggestions for how to approach this would be very helpful.
Thanks so much for all your help!
You want to use -substringWithRange: with a substring of length 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];
}