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
Related
Im trying to fetch a random Emoji character from an array. how can i convert these values into unicode format without XCode having a fit? :
label.text = [NSString stringWithFormat:#"\U%#",[emojis randomObject]];
I get : \U used with no following hex digits every time.
can you not construct a unicode format like that?
Here was my solution:
NSString *code = emojArray[i];
NSScanner *hexScan = [NSScanner scannerWithString:code];
unsigned int hexNum;
[hexScan scanHexInt:&hexNum];
UTF32Char inputChar = hexNum;
NSString *res = [[NSString alloc] initWithBytes:&inputChar length:4 encoding:NSUTF32LittleEndianStringEncoding];
res, when displayed in a UILabel is now an emoji.
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];
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.
Hi i am reading a book by aaron hillegass on cococa programming and I doing one of the mini tasks he asks us to do.
the task is to create an application that has one window open and has 1 input text field, a button and a label.
when a user inputs some text and presses the button, the label displays the text and the length of the text inputted.
Here is what I have got so far
//retrieve text from textfield
NSString *string = [textFieldInput stringValue];
//retrieve length of text and store in NSInteger called length
NSInteger length = [string length];
//store length in string format
NSString *string_length = [NSString stringWithFormat:#"%d", length];
//join strings
NSString *full_string = [string stringByAppendingString:(#"has ",string_length,#" characters")];
//set label text
[textField setStringValue:full_string];
however the actual string is shown and the characters string is shown, just not the string_length. any suggestions and am i going about this in the right way? Thanks.
NSString *fullString = [string stringByAppendingFormat:#"has %# characters", string_length];
//retrieve text from textfield
NSString *string = [textFieldInput stringValue];
NSString *fullString = [string stringByAppendingFormat:#" has %d characters", [string length]];
//set label text
[textField setStringValue:fullString];
Your usage of stringByAppendingString: is wrong.
If I understand you correctly, you are trying to pass a list of strings that should be appended to string but that method only takes a single string argument.
You can try the following:
NSString* fullString = [string stringByAppendingString:[NSString stringWithFormat:#"%# %# %#", #"has ", string_length, #" characters"]];
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];
}