Set Default Language In TextField On Tapping Button In IOS

Inwizards-blog-img

Create a new Project:

-Open Xcode , create a new “Single Page Application” and select Objective c as the programming language.

-Open the ViewController.h class and add a new UITextField and NSString instance variable below the class declaration in ViewController.h.

@property(nonatomic, retain) NSString *str_defaultLang;



@property(nonatomic,retain) IBOutlet UITextField *txt_def_lang;

Create method signature on Button in ViewController.h

-(IBAction)action_language:(UIButton*)sender;

In StoryBoard

Add a TextField and Button in your Storyboard class

Search in the component library(It should be in the lower right corner) for TextField and then drag and drop it on the view.

Search in the component library(It should be in the lower right corner) for UIButton and then drag and drop it on the view.

set button title:- “Set Default Language

Connect the Interface Builder Outlets

in ViewController.m

First synthesize property in ViewController.m

@synthesize str_defaultLang,txt_def_lang;

we will write this line in the ViewDidLoad method in ViewController.m:

txt_def_lang.userInteractionEnabled = false;

Implement button action method inside ViewController.m:-

-(IBAction)action_language:(UIButton*)sender{

NSString * languageID = [[NSLocale preferredLanguages] objectAtIndex:0];

NSRange range= [[NSString stringWithFormat:@”%@”,[[NSLocalelocaleWithLocaleIdentifier:languageID]displayNameForKey:NSLocaleIdentifiervalue:languageID]] rangeOfString: @” “ options:NSBackwardsSearch];



if (range.location != NSNotFound) {

str_defaultLang = [[NSString stringWithFormat:@”%@”,[[NSLocalelocaleWithLocaleIdentifier:languageID]displayNameForKey:NSLocaleIdentifiervalue:languageID]] substringToIndex: range.location];

} else {

str_defaultLang = [NSString stringWithFormat:@”%@”,[[NSLocalelocaleWithLocaleIdentifier:languageID]displayNameForKey:NSLocaleIdentifiervalue:languageID]];

}

str_defaultLang = [NSString stringWithFormat:@”%@”,[[str_defaultLangcomponentsSeparatedByString: @” “] objectAtIndex:0]];

txt_def_lang.hidden = false;

txt_def_lang.text = str_defaultLang;

}



and now build and run code.

Tap on button ”Set Default Language” show your device default language in textField area

Set Default Language In TextField On Tapping Button In IOS

Post navigation


0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x