I am starting objective C and i want to make a form with text inputs. I have many Text Fields on my view and i want to check if the values written are good (only numbers, only text).
How can i do that? I found some documentation but not a lot... If anyone has a good ressource to help me to make forms.
I also want to know how to get all values in an array.
Thank you for help and sorry if my questions look terrible and easy.
I know ruby, php, javascript :)
Given a textfield called textfield1, you can access its string content via textfield1.text. This will yield a NSString-object which you can then analyze. You may use NSRegularExpression for this, or loop through 1-char-substrings or whatever you like, read up on NSString for this. A Google Search will also quickly yield you examples on how to use NSRegularExpressions.
Seeing that you're working for iOS, you might want to consider using special controls for your specific needs. For example, rather than using a textfield to request a number, and complain when the user inputs a non-number, present him a slider instead of a textfield.
For your second question, given an array array1 with NSString objects in it, you can loop through them by fast enumeration:
for (NSString* string in array1)
{
// do something with each string in array1
}
Related
I'm trying to implement an Autocomplete on a search bar on a map using Mapkit. I found this :
https://github.com/chenyuan/SPGooglePlacesAutocomplete
Works and is totally perfect except that it uses UISearchDisplayViewController which has been deprecated in ios8 and replaced by UISearchViewController. Is there a way around it or a simpler way than the one mentioned above?
Thanks in Advance
Please try this new repo: https://github.com/hkellaway/HNKGooglePlacesAutocomplete, which is being actively maintained.
Apple provides a full autocomplete for the entire English language (and others) but if you want to implement your own autocomplete it's not too difficult, you simply need the range of words or phrases that you want to suggest and a way of ranking them in order of frequency used.
I've implemented a simple autocomplete in one of my projects that centers around a PredictionString class and an AutopredictCoordinator class.
The PredictionString has a NSString property and a float property which relates to the strings frequency of use by the user. The AutopredictCoordinator then holds an array of prediction strings and responds to requests for the most likely completion of any given string.
I have an app that basically search youTube videos.
This all youTube search and the result videos are in one view controller.
Now, everything works fine and i do get the wanted videos (using JSON) displayed on my view using this API: "https://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=%#"
The problems are:
1. UISearchBar accepts only one word to search with. if i put on the search bar two or more words, the app crashes. (if i don't want that the two words will make my app crash,
then i need to write on the searchBar, i.e "new%20songs" instead of "new songs").
so the question for here is: do i need to detect every space on the seachBar.text and replace it with "%20"?
or there is something more appropriate for that..?
another thing: language- my UISearchBar accepts only english! if i put another language on it-CRASH!
Do anyone have an explanation? i tried to change some of the API rules, but unsuccessfully.
EDIT: i'm not leaving here code, because i don't think it's the problem. the code works fine.. i think it's more like API problem, but of course, i'll post some code if needed.
Thanks to all !!!
To obtain the correct URL string you need to use stringByAddingPercentEscapesUsingEncoding method. So all spaces and characters (other than English) will be replaced by special codes (eg space will be replaced %20, etc.). Example:
NSString *searchStr = #"string with space";
NSString *youtubeURLString = [NSString stringWithFormat:#"https://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=%#", searchStr];
NSString *URLString = [youtubeURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
I am writing an app in which I have two UITextFields...
Starting Text
Destination Text
Now once I place values and hit the search or whatever function I want to call, I Want to reuse these values. The app should record and save these values as cache. And should show them when typing or upon a button click. Is that possible to show them just like Dictionary words show up or Which is more preferable tableView or PickerView? If there is any other please let me know.
Definitely use a UITableView. A UIPicherView is used modally most of the time and not for optional suggestions.
Table view is used in Safari, and a lots of other apps:
As for how to cache the data, you have lots of options. It also depends on how much data you expect.
One easy way would be to simply use NSArray. You can very easily write an NSArray to disk in a plist file and read it back when you need it.
Or you could use Core Data, if you expect lots of data and still want high performance. It will be a lot more difficult though to get used to that API if you've never tried it before. Basically you'll need a simple model with one entity called something like SearchEntry that has a single property text. Then you keep adding new instances to your managed object context and can easily filter the existing values.
Consider the following, I have paragraph data being sent to a view which needs to be placed over a background image, which has at the top and the bottom, fixed elements (fig1)
Fig1.
My thought was to split this into 4 labels (Fig1.example2) my question here is how I can get the text to flow through labels 1 - 4 given that label 1,2 & 3 ar of fixed height. I assumed here that label 3 should be populated prior to 4 hence the layout in the attached diagram.
Can someone suggest the best way of doing this with maybe an example?
Thanks
Wish I could help more, but I think I can at least point you in the right direction.
First, your idea seems very possible, but would involve lots of calculations of text size that would be ugly and might not produce ideal results. The way I see it working is a binary search of testing portions of your string with sizeWithFont: until you can get the best guess for what the label will fit into that size and still look "right". Then you have to actually break up the string and track it in pieces... just seems wrong.
In iOS 6 (unfortunately doesn't apply to you right now but I'll post it as a potential benefit to others), you could probably use one UILabel and an NSAttributed string. There would be a couple of options to go with here, (I haven't done it so I'm not sure which would be the best) but it seems that if you could format the page with html, you can initialize the attributed string that way.
From the docs:
You can create an attributed string from HTML data using the initialization methods initWithHTML:documentAttributes: and initWithHTML:baseURL:documentAttributes:. The methods return text attributes defined by the HTML as the attributes of the string. They return document-level attributes defined by the HTML, such as paper and margin sizes, by reference to an NSDictionary object, as described in “RTF Files and Attributed Strings.” The methods translate HTML as well as possible into structures of the Cocoa text system, but the Application Kit does not provide complete, true rendering of arbitrary HTML.
An alternative here would be to just use the available attributes, setting line indents and such according to the image size. I haven't worked with attributed strings at this level, so I the best reference would be the developer videos and the programming guide for NSAttributedString. https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/AttributedStrings/AttributedStrings.html#//apple_ref/doc/uid/10000036-BBCCGDBG
For lesser versions of iOS, you'd probably be better off becoming familiar with CoreText. In the end you'll be rewarded with a better looking result, reusability/flexibility, the list goes on. For that, I would start with the CoreText programming guide: https://developer.apple.com/library/mac/#documentation/StringsTextFonts/Conceptual/CoreText_Programming/Introduction/Introduction.html
Maybe someone else can provide some sample code, but I think just looking through the docs will give you less of a headache than trying to calculate 4 labels like that.
EDIT:
I changed the link for CoreText
You have to go with CoreText: create your AttributedString and a CTFramesetter with it.
Then you can get a CTFrame for each of your textboxes and draw it in your graphics context.
https://developer.apple.com/library/mac/#documentation/Carbon/Reference/CTFramesetterRef/Reference/reference.html#//apple_ref/doc/uid/TP40005105
You can also use a UIWebView
Could any stackoverflower kindly point me to a quick snippet, algorithm, code to achieve that?
Thaanks.
Though this isn't a complete implementation of what you want, the first thing that popped into my head was Camino's bookmark importer (though it's changed to use NSXMLDocument instead of an NSScanner since I last dug into it).
(MPL license.)