altering font size and style in PickerView sections [closed] - ios

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
My font picker is now working fine,
all that is missing is 2 extra sections in the PickerView for font size and style (bold, italic and underlined).
I don't know how to approach changing these values.
This is my picker's code :
#import "ViewController.h"
#interface ViewController ()
{
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
fontNames = [NSMutableArray array];
for(NSString *familyName in [UIFont familyNames]) {
for(NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
[fontNames addObject:fontName];
}
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return fontNames.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
return [fontNames objectAtIndex:row];
}
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view {
UILabel *pickerLabel = (UILabel *)view;
CGRect frame = CGRectMake(0.0, 0.0, 80, 32);
pickerLabel = [[UILabel alloc] initWithFrame:frame];
pickerLabel.backgroundColor = [UIColor clearColor];
pickerLabel.textAlignment = NSTextAlignmentLeft;
pickerLabel.text = [NSString stringWithFormat:#"%#",[fontNames objectAtIndex:row]];
pickerLabel.font = [UIFont fontWithName:[fontNames objectAtIndex:row] size:15];
return pickerLabel;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
_fontLabel.text = [fontNames objectAtIndex:row];
_fontLabel.font = [UIFont fontWithName:[fontNames objectAtIndex:row] size:15];
}
#end

You get the fonts using this loop:
NSMutableArray *fontNames = [NSMutableArray array];
for(NSString *familyName in [UIFont familyNames]) {
for(NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
[fontNames addObject:fontName];
}
}
How you present the picker depends on your UI layout. You might use an UITableView in an UIPopoverController (just google it, you'll find TONS of quite good tutorials) or an UIPickerView.

Related

Not able to use two UIPickerView in the same Controller

I'm trying to use more than two UIPickerViews together in one ViewController. Each UIPickerView has different data array. I am using interface builder to link the pickers up. What I want is, after button click, my PickerView will show up and user can select the data from it.That selected data will come in the text field. It is working fine for first PickerView but when I click for the second PickerView, the value is coming from first array and I am not able to write condition. Here's all my code:
.h file
#import <UIKit/UIKit.h>
#interface Tabbar2ViewController : UIViewController<UITextViewDelegate,UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>{
IBOutlet UIPickerView *genderPickerView;
IBOutlet UIPickerView *pickerViewoption;
IBOutlet UIButton *BrandPickerBtn;
IBOutlet UIButton *genderPickerfBtn;
NSArray *brands;
NSArray *gender;
BOOL isCheckedpickerView;
IBOutlet UILabel *BrandLabelfield;
IBOutlet UILabel *GenderLabelfield;
}
- (IBAction)action:(id)sender;
- (IBAction)genderAction:(id)sender;
#property(nonatomic,retain)NSArray *brands;
#property(nonatomic,retain)NSArray *gender;
#end
.m file
#import "ttViewController.h"
#interface ttViewController ()
#end
#implementation ttViewController
#synthesize countries,size,brands,gender,activity,condition,color;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
isCheckedpickerView=FALSE;
pickerViewoption.hidden=YES;
genderPickerView.hidden=YES;
activityPickerView.hidden=YES;
conditionPickerView.hidden=YES;
sizeandquantityPickerView.hidden=YES;
BrandLabelfield = [[UILabel alloc] initWithFrame:CGRectMake(20, 812, 253, 35)];
BrandLabelfield.textColor = [UIColor lightGrayColor];
BrandLabelfield.text=#"Brand";
BrandLabelfield.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"header_01.png"]];
BrandLabelfield.font = [UIFont fontWithName:#"Open Sans" size:14];
[tabView1 addSubview:BrandLabelfield];
//GenderTextfield with Label //
GenderLabelfield = [[UILabel alloc] initWithFrame:CGRectMake(20, 865, 253, 35)];
GenderLabelfield.textColor = [UIColor lightGrayColor];
GenderLabelfield.text=#"Gender";
GenderLabelfield.font = [UIFont fontWithName:#"Open Sans" size:14];
[tabView1 addSubview:GenderLabelfield];
//ColorTextfield with Label //
ConditionLabelfield = [[UILabel alloc] initWithFrame:CGRectMake(20, 1113, 253, 35)];
ConditionLabelfield.textColor = [UIColor lightGrayColor];
ConditionLabelfield.text=#"Color";
ConditionLabelfield.font = [UIFont fontWithName:#"Open Sans" size:14];
[tabView1 addSubview:ConditionLabelfield];
brands=[[NSArray alloc]initWithObjects:#"Nike",#"Bata",#"Adidas", nil];
gender=[[NSArray alloc]initWithObjects:#"Male",#"Female",#"Unisex", nil];
activity=[[NSArray alloc]initWithObjects:#"BASEBALL",#"BASKETBALL",#"FOOTBALL",#"GOLF",#"RUNNING",#"SKATING",#"SOCCER",#"SPORTS & OUTDOOR",#"TENNIS",#"TRAINING",#"VOLLEY BALL",#"WRESTLING", nil];
// Custom PICKER VIEWS //
pickerViewoption=[[UIPickerView alloc]initWithFrame:CGRectMake(13, 852, 290, 41)];
pickerViewoption.showsSelectionIndicator = YES;
pickerViewoption.delegate = self;
pickerViewoption.dataSource=self;
pickerViewoption.hidden=YES;
[pickerViewoption setBackgroundColor:[UIColor lightGrayColor]];
[tabView1 addSubview:pickerViewoption];
genderPickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(13, 906, 290, 41)];
genderPickerView.showsSelectionIndicator = YES;
genderPickerView.delegate = self;
genderPickerView.dataSource=self;
genderPickerView.hidden=YES;
[genderPickerView setBackgroundColor:[UIColor lightGrayColor]];
[tabView1 addSubview:genderPickerView];
activityPickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(13, 954, 290, 41)];
activityPickerView.showsSelectionIndicator = YES;
activityPickerView.delegate = self;
activityPickerView.dataSource=self;
activityPickerView.hidden=YES;
[activityPickerView setBackgroundColor:[UIColor lightGrayColor]];
[tabView1 addSubview:activityPickerView];
pickerViewoption.tag=1;
genderPickerView.tag=2;
activityPickerView.tag=3;
BrandPickerBtn.tag=000;
genderPickerfBtn.tag=111;
activityPickerBtn.tag=222;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component{
NSString *string;
if (pickerViewoption.tag==1) {
[tabView1 addSubview:pickerViewoption];
string=[self.brands objectAtIndex:row];
}
else {
[tabView1 addSubview:genderPickerView];
string=[self.gender objectAtIndex:row];
}
else if (genderPickerView.tag==2){
[tabView1 addSubview:genderPickerView];
string=[self.gender objectAtIndex:row];
}
else if (activityPickerView.tag==3){
[tabView1 addSubview:activityPickerView];
string=[self.activity objectAtIndex:row];
}
return string;
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
if (pickerViewoption.tag==1){
BrandLabelfield.text=#"";
BrandLabelfield.text=[brands objectAtIndex:row];
BrandLabelfield.textColor=[UIColor whiteColor];
pickerViewoption.hidden=YES;
}
else if (genderPickerView.tag==2){
GenderLabelfield.text=#"";
GenderLabelfield.text=[gender objectAtIndex:row];
GenderLabelfield.textColor=[UIColor whiteColor];}
else if (activityPickerView.tag==3){
ActivityLabelfield.text=#"";
ActivityLabelfield.text=[activity objectAtIndex:row];
ActivityLabelfield.textColor=[UIColor whiteColor];
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
if (pickerViewoption.tag==1)
{
return [brands count];
}
else if (genderPickerView.tag==2)
{
return [gender count];
}
else if (activityPickerView.tag==3)
{
return [activity count];
}
return 0;
}
- (IBAction)action:(id)sender; {
if (isCheckedpickerView==FALSE)
{
pickerViewoption.hidden=NO;
isCheckedpickerView=TRUE;
}
else
{
isCheckedpickerView=FALSE;
pickerViewoption.hidden=YES;
}
}
- (IBAction)genderAction:(id)sender {
if (isCheckedpickerView==FALSE)
{
genderPickerView.hidden=NO;
isCheckedpickerView=TRUE;
}
else
{
isCheckedpickerView=FALSE;
genderPickerView.hidden=YES;
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Currently you are checking wrong condition in delegate methods, replace following 3 methods in .m file :
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component{
NSString *string;
if (pickerView.tag==1) {
[tabView1 addSubview:pickerViewoption];
string=[self.brands objectAtIndex:row];
}
else if (pickerView.tag==2){
[tabView1 addSubview:genderPickerView];
string=[self.gender objectAtIndex:row];
}
else if (pickerView.tag==3){
[tabView1 addSubview:activityPickerView];
string=[self.activity objectAtIndex:row];
}
else {
[tabView1 addSubview:genderPickerView];
string=[self.gender objectAtIndex:row];
}
return string;
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
if (thePickerView.tag==1){
BrandLabelfield.text=#"";
BrandLabelfield.text=[brands objectAtIndex:row];
BrandLabelfield.textColor=[UIColor whiteColor];
pickerViewoption.hidden=YES;
}
else if (thePickerView.tag==2){
GenderLabelfield.text=#"";
GenderLabelfield.text=[gender objectAtIndex:row];
GenderLabelfield.textColor=[UIColor whiteColor];
}
else if (thePickerView.tag==3){
ActivityLabelfield.text=#"";
ActivityLabelfield.text=[activity objectAtIndex:row];
ActivityLabelfield.textColor=[UIColor whiteColor];
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
if (pickerView.tag==1)
{
return [brands count];
}
else if (pickerView.tag==2)
{
return [gender count];
}
else if (pickerView.tag==3)
{
return [activity count];
}
return 0;
}

Images displayed in UIPickerView appear/disappear

I'm having problems while displaying images in a UIPickerView. The images appear and disappear randomly and never show in the "middle" row of the picker. This is the code
#interface ShowImagesPVC () <UIPickerViewDataSource, UIPickerViewDelegate>
#property (strong, nonatomic) NSMutableArray *arrayOfImages;
#end
#implementation ShowImagesPVC
- (void)viewDidLoad
{
[super viewDidLoad];
_arrayOfImages = [NSMutableArray array];
for(int i = 1; i < 36; i++){
NSString *tmpString = [NSString stringWithFormat:#"%d.png",i];
UIImageView *myIcon = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
[myIcon setImage:[UIImage imageNamed:tmpString]];
[_arrayOfImages addObject:myIcon];
}
UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(-5, 0, 315, 200)];
myPickerView.delegate = self;
myPickerView.dataSource = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
return [_arrayOfImages objectAtIndex:row];
}
- (NSInteger)numberOfComponentsInPickerView: (UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _arrayOfImages.count;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 45;
}
#end
This is a pic of the Picker just loaded
and this is a pic of the Picker a bit rolled
There should be the icons seen in the first pic over the middle row, but there is nothing.
The icon size is 64x64, so i tried to make the first one 40x40 (but doesn't change anything because it should be in the middle row in the first pic, but as you can se it's blank)
Instead of making your _arrayOfImages an array of image views, make it an array of images, and then do this in viewForRow:forComponent:reusingView:,
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
iv.image = _arrayOfImages[row];
return iv;
}

Using UIPickerView to display different images upon selection in UIImageView

I have a UIPickerView filled with an array of tree types. I also have an empty UIImageView below that I would like to change upon the selection of the picker view. I got a label to change with the selection but cannot get images to change. Here is what I have so far
EDIT
EDIT : Now working! Correct Code below
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
dataArray = [[NSArray alloc]initWithObjects:#"Birch", #"Elm", #"Maple", #"Oak", #"White Pine",#"Willow", nil];
myPicker.delegate = self;
imageArray = [[NSArray alloc]initWithObjects:#"birch_river.png",#"elm_american.png",#"maple_bigtooth.png", #"oak_bur.png", #"white_pine.png", #"oak_willow.png", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [dataArray count];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
myLabel.text = [dataArray objectAtIndex:row];
myImage.image = [UIImage imageNamed:[imageArray objectAtIndex:row]];
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component
{
return [dataArray objectAtIndex:row];
}
#end
What you can do is, have an array filled with the image names (Ex: IMAGES_ARRAY with image names).
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
myLabel.text = [dataArray objectAtIndex:row];
YOUR_IMG_VIEW = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[IMAGES_ARRAY ObjectAtIndex.row]]];
}

how to disable rolling effect and shadow effect in UIPICKER VIEW in ios?

i am developing the one example of UIPICKERVIEW and i want to disable or off the shadow effect and rolling effect but not able to do it.do you have any idea for that ?
Source code:
pickerViewController.h file
#import <UIKit/UIKit.h>
#interface pickerViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
{
UIPickerView *languageSelect;
NSMutableArray *pickerData;
}
#property (nonatomic, retain) UIPickerView *languageSelect;
#property (nonatomic, retain) NSArray *pickerData;
#end
pickerViewController.m file
#import "pickerViewController.h"
#interface pickerViewController ()
#end
#implementation pickerViewController
#synthesize languageSelect, pickerData;
- (void)viewDidLoad
{
[super viewDidLoad];
pickerData= [[NSMutableArray alloc] initWithObjects:#"English",#"Spanish",#"French",#"Greek",
#"Japaneese",#"Korean",#"Hindi",#"English",#"Spanish",#"French",#"Greek",
#"Japaneese",#"Korean",#"Hindi", nil];
languageSelect = [[UIPickerView alloc] initWithFrame:CGRectMake(10, 50, 300, 1000)];
languageSelect.showsSelectionIndicator = YES;
languageSelect.hidden = NO;
languageSelect.delegate = self;
[self.view addSubview:languageSelect];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component;
{
return [pickerData count];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *pickerLabel = (UILabel *)view;
NSString* labelText = [NSString stringWithFormat:#"%#",[pickerData objectAtIndex:row]];
if (pickerLabel == nil) {
CGSize size = [labelText sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(250, 216) lineBreakMode:NSLineBreakByCharWrapping];
CGRect frame = CGRectMake(0.0, 0.0, 250, size.height + 25);
pickerLabel = [[UILabel alloc] initWithFrame:frame];
[pickerLabel setTextAlignment:NSTextAlignmentLeft];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
[pickerLabel setLineBreakMode:NSLineBreakByWordWrapping];
// trying to mess with the UIView of the row itself... to no avail
//CGRect rowFrame = view.frame;
//NSLog(#"%f",rowFrame.size.height);
//rowFrame.size.height = size.height + 25;
//view.frame = rowFrame;
}
[pickerLabel setText:[pickerData objectAtIndex:row]];
//NSLog(#"%f",[pickerView rowSizeForComponent:component].height);
return pickerLabel;
}
-(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [pickerData objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
{
}
Output:
I want to do something like this...!!
This is a lot of custom work. If you want to achieve that look, you are probably better of trying to use a UITableView. You'll still have to put in quite some effort though.
In pic you can see structure of UIPickerView.
http://i.stack.imgur.com/QFAKV.png
Maybe removing some layers help you
http://i.stack.imgur.com/C2xDO.png

UIPickerView not working in sub UIView

I have a UIPickerView that works perfectly when placed on the main view. However as soon as I place it into a sub UIView (To help with visual placement of elements) the interaction stops completely. The UIPickerView still receives all data correctly, just no interaction.
If I take the UIPickerView back out of the UIView, using storyboard, it works right away.
- (void)viewDidLoad
{
[super viewDidLoad];
// Add provinces into array
[self.provinceView setHidden:YES];
isVisible = NO;
self.provinceArray = [[NSArray alloc] initWithObjects:#"Alberta",#"British Columbia",#"Manitoba",#"New Brunswick",#"Newfoundland and Labrador",#"Northwest Territories",#"Nova Scotia",#"Nunavut",#"Ontario",#"Prince Edward Island",#"Quebec",#"Saskatchewan",#"Yukon",nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [self.provinceArray count];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0,(pickerView.frame.size.width-40), 44)];
label.textColor = [UIColor colorWithRed:0.035 green:0.094 blue:0.345 alpha:1.0];
label.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
label.text = [self.provinceArray objectAtIndex:row];
return label;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
self.provinceLabel.text = [self.provinceArray objectAtIndex:row];
}
Check this post
-->
UIPickerView in UITableView doesn't react to touch in lower half of cell
did u bring the subview to foreground?

Resources