show indexOfObject in UILabel - uilabel

I am trying to show the number of objects in an NSArray in a UILabel as part of a UITabelViewCell:
UILabel *numberLabel = (UILabel *)[cell viewWithTag:103];
numberLabel.numberOfLines = [self.inputValues indexOfObject:inputValue];
NSLog(#"rownumber is: %d", numberLabel.numberOfLines);
The NSLoggives me the right number, but the label shows just the default title value. What am I missing here? No errors are thrown.

You need to set the label's text property. Example:
NSUInteger index = [self.inputValues indexOfObject:inputValue];
numberLabel.text = [NSString stringWithFormat:#"Row %lu", (unsigned long)index];
However, it seems likely that you could just use the row from the cell's index path, like this:
numberLabel.text = [NSString stringWithFormat:#"Row %ld", (long)indexPath.row];

I'm pretty sure that instead you want:
UILabel *numberLabel = (UILabel *)[cell viewWithTag:103];
numberLabel.text = [NSString stringWithFormat:#"%d", [self.inputValues indexOfObject:inputValue]];
NSLog(#"rownumber is: %d", numberLabel.numberOfLines);

Related

cocoa-touch How can I find selected text in UITextview to text among a NSString

How can I find selected text in UITextview among a NSString
//Declare
NSString* myText = #"This is the first row and This is the second row";
myUITextview.text = myText;
// we have the same results in here "This is the" and "row"
//Then check it when text in UITextview is selected
- (void)textViewDidChangeSelection:(UITextView *)textView
{
NSString *selectedText = [textView.text substringWithRange:[textView selectedRange]];
//selectedText "This is" from the second not from the first
}
is there anyway to recognise that? compare? check range or something?
In order to add text before and after the selected text in UITextView, you should do like this:
NSString *fullText = textView.text;
NSRange selectedRange = [textView selectedRange];
NSString *selectedText = [fullText substringWithRange:selectedRange];
NSString *addBeforeSel = #"abc";
NSString *addAfterSel = #"def";
NSString *replace = [NSString stringWithFormat:#"%#%#%#", addBeforeSel, selectedText, addAfterSel];
NSString *result = [fullText stringByReplacingCharactersInRange:selectedRange withString:replace];

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

Setting the text of a UILabel to an int?

The question is pretty much self explanatory. I need to set the text property of a UILabel instance to an int. How do I go about doing this? Sorry if it's a nooby question.
Thanks!
Assuming you have:
UILabel *myLabel; //instance of your label
int myInt; //the integer you need to set as text into UILabel
you can do this, and it's pretty simple:
[myLabel setText:[NSString stringWithFormat:#"%d", myInt]];
or:
myLabel.text = [NSString stringWithFormat:#"%d", myInt];
NSNumber *number = [NSNumber numberWithInt:yourInt];
[yourLabel setText:[number stringValue]];
label.text = [NSString stringWithFormat:#"%i",intNumber];
Try this:
[label setText:[NSString stringWithFormat:#"%d", intValue]];

Dynamically Create Object Within Loop - 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.

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