iOS - Displaying Two Words At A Time From String Array - ios

I'm doing an RSVP reading project app where it blinks words on the screen. You can set the word chunk size (how many words you want displayed at a time) to either 1, 2, or 3. I got it working for 1 word by having my paragraph in a string and doing:
[self.textInput componentsSeparatedByString:#" ";
This makes me an array of words that I can use to blink one word at a time. How would I be able to do this with displaying 2 words at a time? Is there a way I can use this function again to do it differently, or should I iterate over this word array and make a new one with 2 word strings?
Any help or advice would be greatly appreciated as to what the best practice would to get this done. Thanks.

just like keith said create an array
NSArray *allwordsArray = [self.textInput componentsSeparatedByString:#" "];
Now you got all the info you need. Meaning you got the array with every word in it. Now its just a matter of putting it together. (I haven't tested this code)
NSMutableArray *twoWordArray = [[NSMutableArray alloc] init];
int counter=0;
for (int i=0; i<[allwordsArray count]; i++)
{
if (counter >= [allwordsArray count]) break;
NSString *str1 = [NSString stringwithformat#"%#", [allwordsArray objectAtIndex:counter]];
counter++;
if (counter >= [allwordsArray count]) break;
NSString *str2 = [NSString stringwithformat#"%#", [allwordsArray objectAtIndex:counter]];
NSString *combinedStr = [NSString stringwithformat#"%# %#", str1,str2];
[twoWordArray addObject: combinedStr];
counter++;
}

You have broken the string into components, which is on the right track. You could then make a smaller array that only includes components until you reach the chunk size. The final step would be to rejoin the string.
NSArray *components = [self.textInput componentsSeparatedByString:#" "];
NSRange chunkRange = NSMakeRange(0, chunkSize);
NSArray *lessComponents = [components subarrayWithRange:chunkRange];
NSString *newString = [lessComponents componentsJoinedByString:#" "];

Related

Listing floats from array with format

I have an NSMutableArray that contains floats. I can display one float in a specific format or display all of them without formatting but I cannot figure out how to do both at once. I first wrap the float in an NSNumber so that it can be added to the array:
NSMutableArray *array = [[NSMutableArray alloc] init];
NSNumber *num = [NSNumber numberWithFloat:5.10f];
[array addObject:num];
NSNumberFormatter works for formatting the numbers to be displayed in a textView. Here is the code I used to do that:
for (NSNumber *aNumber in array) {
self.textView.text = [NSString stringWithFormat:#"%#"
[NSNumberFormatter localizedStringFromNumber:aNumber
numberStyle:NSNumberFormatterCurrencyStyle]];
}
The obvious problem here is the text is replaced with the number in the array each time the loop runs rather than listing all of them.
So, I did more research and found componentsJoinedByString:
self.textView.text = [NSString stringWithFormat:#"%#", [array componentsJoinedByString:#", "]];
This worked well to list them but lacked the formatting style: It displays, for example, 5.10 as 5.1 which I don't want. I tried to combine them somehow but it didn't work, and I also tried stringByAppendingString in the loop using the formatter but that was a mess that did not work at all.
You are replacing the text in the textView instead of appending..
for (NSNumber *aNumber in array) {
NSString *result = [NSNumberFormatter localizedStringFromNumber:aNumber
numberStyle:NSNumberFormatterCurrencyStyle];
if (aNumber == array.firstObject && !self.textView.text.length) {
self.textView.text = result;
}
else {
self.textView.text = [NSString stringWithFormat:#"%#, %#", self.textView.text, result];
}
}

How to show new line with numbers format from server response of String in iOS

I am getting server response as "We've organizations in following something locations. 1. First test 2. Second test 3. Third test 4. Fourth test. Choose one for more information";
I am working on Objective-C and trying to showing in UILabel, but issue is I want to show as Paragraph. Like splitting the string by following.
We've organizations in following something locations.
First test
Second test
Third test
Fourth test
Choose one for more information.
The above one is example, but the data is completely dynamic and no idea about how many points would be there in response string.
Anyone have idea about this.
Try this :
NSString *str = #"We've organizations in following something locations. 1. First test 2. Second test 3. Third test 4. Fourth test.";
str = [str stringByReplacingOccurrencesOfString:#"1." withString:#"\n 1."];
str = [str stringByReplacingOccurrencesOfString:#"2" withString:#"\n 2"];
str = [str stringByReplacingOccurrencesOfString:#"3" withString:#"\n 3"];
str = [str stringByReplacingOccurrencesOfString:#"4" withString:#"\n 4"];
yourlbl.text = str;
Dynamic way
NSString *str = #"We've organizations in following something locations. 1. First test 2. Second test 3. Third test 4. Fourth test.";
NSCharacterSet *numberCharset = [NSCharacterSet characterSetWithCharactersInString:#"0123456789"];
NSScanner *theScanner = [NSScanner scannerWithString:str];
while (![theScanner isAtEnd]) {
// Eat non-digits and negative sign
[theScanner scanUpToCharactersFromSet:numberCharset
intoString:NULL];
int aInt;
if ([theScanner scanInt:&aInt]) {
str = [str stringByReplacingOccurrencesOfString:[NSString stringWithFormat:#"%d",aInt] withString:[NSString stringWithFormat:#"\n %d",aInt]];
}
}
You can use like this
NSArray *array = [NSArray arrayWithObjects:#"First test",#"Second test",#"Third test" , #"Fourth test Choose one for more information.", nil];
__block NSString *strConcate = [NSString new];
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
strConcate = [strConcate stringByAppendingString:[NSString stringWithFormat:#"%lu. %#\n",(unsigned long)(idx + 1),obj]];
}];
self.labelString.text = strConcate;
Finally, I found the solution, here it is. This may helps someone in future.
NSArray *pointsArray = [trimmedString componentsSeparatedByString:#"."];
NSString *strOccurence, *strReplacing;
for (int i=1; i<pointsArray.count; i++){
strOccurence = [NSString stringWithFormat:#"%d.",i];
strReplacing = [NSString stringWithFormat:#"\n%d.",i];
trimmedString = [trimmedString stringByReplacingOccurrencesOfString:strOccurence withString:strReplacing];
}
cell.textLabel.text = trimmedString;

how to get single characters from string in ios for Gujrati language(other language)

I am trying to get single characters from NSString, like "ઐતિહાસિક","પ્રકાશન","ક્રોધ". I want output like 1)ઐ,તિ,હા,સિ,ક 2) પ્ર,કા,શ,ન 3) ક્રો,ધ, but output is coming like this 1)ઐ , ત , િ , હ , િ , ક 2) પ , ્ , ર , ક , ા , શ , ન 3)ક , ્ , ર , ો , ધ
I have used code like below:
NSMutableArray *array = [[NSMutableArray alloc]init];
for (int i=0; i<strElement.length; i++)
{
NSString *str = [strElement substringWithRange:NSMakeRange(i, 1)];
[array addObject:str];
}
NSLog(#"%#",array);
Let's take strElement as "ક્રોધ" then I got output like this ક , ્ , ર , ો , ધ
But I need output like this ક્રો,ધ
Is there any way that I can get the desired output? Any method available directly in iOS or need to create it by my self then any way or idea how to create it?
Any help is appreciated
Your code is assuming that each character in the string is a single unichar value. But it is not. Some of the Unicode characters are composed of multiple unichar values.
The solution is to use rangeOfComposedCharacterSequenceAtIndex: instead of substringWithRange: with a fixed range length of 1.
NSString *strElement = #"ઐતિહાસિક પ્રકાશન ક્રોધ";
NSMutableArray *array = [[NSMutableArray alloc]init];
NSInteger i = 0;
while (i < strElement.length) {
NSRange range = [strElement rangeOfComposedCharacterSequenceAtIndex:i];
NSString *str = [strElement substringWithRange:range];
[array addObject:str];
i = range.location + range.length;
}
// Log the results. Build the results into a mutable string to avoid
// the ugly Unicode escapes shown by simply logging the array.
NSMutableString *res = [NSMutableString string];
for (NSString *str in array) {
if (res.length) {
[res appendString:#", "];
}
[res appendString:str];
}
NSLog(#"Results: %#", res);
This outputs:
Results: ઐ, તિ, હા, સિ, ક, , પ્ર, કા, શ, ન, , ક્રો, ધ

Print symbols that were met in the text, no duplicates

I have been struggling with this question for couple days now. Really need your help and opinion.
We have a string, that holds a text:
NSString *contentOfFile = [[NSString alloc] initWithString:#"This is string#1"];
Now I have to log symbols, that were met in this string without duplicates. Result should look like this:
whitespace symbol here
#
1
g
h
i
n
r
s
t
I know that this is solved very simple in C code using char set and iterators but I am looking for the same simple and elegant way of handling this operation in objective-c.
I was thinking of using NSCharacterSet on the string somehow but I have a lack of knowledge in objective-c so I need your help guys please. Thanks in advance to everyone who replies.
Take advantage of a trait of NSSet: Its members are distinct.
NSString *contentOfFile = #"This is string#1";
NSMutableSet *set = [NSMutableSet set];
NSUInteger length = [contentOfFile length];
for (NSUInteger index = 0; index < length; index++)
{
NSString *substring = [contentOfFile substringWithRange:NSMakeRange(index, 1)];
[set addObject:substring];
}
NSLog(#"%#", set);
However, there's one remaining problem, and that is the members of a set are also unordered. Fortunately, arrays are ordered. So if you change the last line:
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"self" ascending:YES];
NSArray *array = [set sortedArrayUsingDescriptors:#[sortDescriptor]];
NSLog(#"%#", array);
If case insensitivity is important to you, there unfortunately is no 'case-insensitive' option for NSSet. However, you could convert your source string to all lowercase, like this:
NSString *contentOfFile = [#"This is string#1" lowercaseString];
and this would give you results exactly matching your sample output.
// Create the string
NSString *contentOfFile = #"This is string#1";
// Remove all whitespaces
NSString *whitespaceRemoval = [contentOfFile stringByReplacingOccurrencesOfString:#" " withString:#""];
// Initialize an array to store the characters
NSMutableArray *components = [NSMutableArray array];
// Iterate through the characters and add them to the array
for (int i = 0; i < [whitespaceRemoval length]; i++) {
NSString *character = [NSString stringWithFormat:#"%c", [whitespaceRemoval characterAtIndex:i]];
if (![components containsObject:character]) {
[components addObject:character];
}
}

How to append NSMutable strings into a UILabel

This is my first question to Stack Overflow. I have been using this site for a while and have used it's resources to figure out answers to my programming questions but I'm afraid I can't find the answer I'm looking for this time.
I've created these five strings:
//List five items from the book and turn them into strings
//1 Josh the Trucker
NSString *stringJosh = #"Josh the Trucker";
//2 The Witch from the Remote Town
NSString *stringWitch = #"The Witch from the Remote Town";
//3 Accepting the curse rules "Willingly and Knowingly"
NSString *stringRules = #"Accepting the curse rules, --Willingly and Knowingly--";
//4 Josh's time left to live--Five Days Alive Permitted
NSString *stringFiveDays = #"Josh's time left to live--Five Days Alive Permitted";
//5 The Fire Demon Elelmental
NSString *stringDemon = #"The Fire Demon Elelmental";
Then, I've put them in an array:
//Create an array of five items from the book
NSArray *itemsArray = [[NSArray alloc] initWithObjects:
stringJosh,
stringWitch,
stringRules,
stringFiveDays,
stringDemon,
nil];
Then, I created this mutable string where I need to loop through the array and append the items to a UIlabel.
NSMutableString *itemsString = [[NSMutableString alloc] initWithString:
#"itemsArray"];
Here's the loop, which displays the items in the console log.
for (int i=0; i<5; i++)
{
NSLog(#"Book Item %d=%#", i, itemsArray[i]);
}
My question is, how do I append these items into the UIlabel?
These functions are in my appledelegate.
In my viewDidAppear function (flipsideViewController) I have:
label8.text =""----thats where the looped info needs to go.
How do I do this?
I feel I need to put them together and append where the NSLog should be...but how do I transfer that info to the textlabel?
I hope I explained myself.
We haven't done ANY append examples, I guess this is where I need to get answers from the "wild"
This is the wildest coding environment I know so I'm hoping I can find some direction here.
Thanks for taking a look!
Once you have all your strings that you want to concatenate in NSArray you can combine them with single call (with whatever separator you want):
NSString *combinedString = [itemsArray componentsJoinedByString:#" "];
If you need more complex logic you can use NSMutableString to create result you want while iterating array, i.e.:
NSMutableString *combinedString = [NSMutableString string];
[itemsArray enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
[combinedString appendFormat:#"Book Item %d=%# ", idx, obj];
}];
Note also that it is better to iterate through collections using fast enumeration or block enumeration rather than using plain index-based for loop.
NSMutableString *labelText = [NSMutableString string];
int i = 0;
for (NSString *item in itemsArray)
[labelText appendFormat:#"Book Item %d=%#\n", i++, item];
label8.text = labelText;
DO this
UILabel *mainlabel;
mainlabel.text = [origText stringByAppendingString:get];
Add your text to mainlabel.. orig text is mutable string or else in forloop just append array object at index text to label.put above line of code in forloop

Resources