2 lines (multiline) picker for long text - ios

why can i set the Row hight or multi lines for a picker,
the problem i have long texts for my picker, or gives an line break in picker... xcode 5 ...
i get the values for the picker from an array.
my code for my custom Picker is:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *label = [[UILabel alloc] init];
CGRect frame = CGRectMake(0,0,265,40);
label.backgroundColor = PickColor;
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18 ];
label.textAlignment = NSTextAlignmentCenter;
if(component == 0)
{
label.text = [_vergehen objectAtIndex:row];
}
return label;
}
thanks for help

Try this code
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 50;
}
then
- (UIView *)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 310, 50)];
lblTitle.numberOfLines=2;
lblTitle.textColor=[UIColor blackColor];
lblTitle.font=[UIFont systemFontOfSize:14];
lblTitle.text=[selectionList objectAtIndex:row];
return lblTitle;
}

Because you're creating a label, you might try this property of UILabel:
// UILabel wraps text across multiple lines
label.lineBreakMode = UILineBreakModeWordWrap;
If you set label.numberOfLines = 0, you should be able to get any number of lines placed within the label.

UILineBreakModeWordWrap is deprecated deprecated in iOS 6.0
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;

Related

How to change the font family & style of textview in iOS?

I want to change the font style & family of the UITextView .Actually i will pick the font family from picker then change the font family of UITextView.Please tell me how to do this?
in that assumption I submit my answer, customize yourself else if any queries ask here
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component{
// here you can get index for selected Row
[yourTextView setText:[pickerArray objectAtIndex:row]];
[yourTextView setFont:[UIFont fontWithName:#"Helvetica Neue" size:18.0f]];
yourTextView.textColor = [UIColor whiteColor];
}
for picker tutorial
try this for showing name of family fontname in picker
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* labelFontName = (UILabel*)view;
if (!labelFontName ){
labelFontName = [[UILabel alloc] init];
labelFontName .adjustsFontSizeToFitWidth = YES;
labelFontName .TextAlignment = NSTextAlignmentCenter;
labelFontName .font = [UIFont fontWithName:#"Arial" size:18];
}
NSString *stringForTitle= [NSString stringWithFormat:#"%#", [arrayOfYourPickerElements objectAtIndex:row]];// Fill the label text here
labelFontName .text = stringForTitle;
return labelFontName ;
}
after selecting fontName:
yourTextView.font = [UIFont fontWithName:label size:17];

How to get Custom UIPickerView?

I want to achieve the following functionality in Custom UIPiker as shown in below picture.
I want to change the text colour of selected area only like above.
Add lable in your pickerview in your viewDidLoad method as below.
Define label and myPickerView both in ViewController.h file
- (void)viewDidLoad
{
myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];
label = [[UILabel alloc] initWithFrame:CGRectMake(145, 76, 36, 36)];
//label.text = #"Label";
label.font = [UIFont boldSystemFontOfSize:20];
label.layer.cornerRadius = 18;
[label setTextColor:[UIColor whiteColor]];
label.backgroundColor = [UIColor blueColor];
label.textAlignment = NSTextAlignmentCenter;
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake (0,1);
[myPickerView addSubview:label];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
in pickerview delegate set label title.
#pragma mark - PickerView delegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
[label setText:[NSString stringWithFormat:#"%d",row]];
NSLog(#"%#",[NSString stringWithFormat:#"%d",row]);
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSLog(#"%#",[NSString stringWithFormat:#"%d",row]);
[label setText:[NSString stringWithFormat:#"%d",row]];
return [NSString stringWithFormat:#"%d",row];
}
Your OuytPut is :

UIPicker single component to display multiple arrays

first comment so I hope Im doing this right.
I'm having a fight with a UIPicker. I am trying to display 2 columns of data in a single picker component. The reason I have chosen to do this within 1 component is that I want the arrays to scroll together, which I can't get multiple components to do.
The trouble is, I can't get this way to work either, as titleForRow and viewForRow will only return one value (as per the rules of C). I tried making them output arrays and dicts but that caused data type errors.
I can get it to work just fine using 1 component, 1 array with viewForRow but that only allows justification the whole field, not parts of the string.
The below code works great and gives a correct answer with return label2; and if changed to return label1 is correct also , How do I get both to display?
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label1;
{
label1 = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 150.0f, 130.0f, 60.0f)];
label1.textAlignment = NSTextAlignmentLeft;
label1.text = [_firstList objectAtIndex:row];
}
UILabel *label2;
{
label2 = [[UILabel alloc] initWithFrame:CGRectMake(100.0f, 10.0f, 175.0f, 100.0f)];
label2.textAlignment = NSTextAlignmentCenter;
label2.text = [_secondList objectAtIndex:row];
}
return label2;
}
Setup your UIPickerView to have one component.
Now assuming that your two array _firstList and _secondList have the same number of objects in them, you have two options:
Use the simple pickerView:titleForRow:forComponent: method to return a single string build from the two values:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [NSString stringWithFormat:#"%# - %#", _firstList[row], _secondList[row]];
}
Of course you can format the two strings as needed. This is one example.
Use pickerView:viewForRow:forComponent:reusingView: like you tried but return a single view that has two labels added to it.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *label1;
UILabel *label2;
if (view) {
label1 = (UILabel *)[view viewWithTag:1];
label1 = (UILabel *)[view viewWithTag:2];
} else {
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 0.0f, 130.0f, 40.0f)];
label1.tag = 1;
label1.textAlignment = NSTextAlignmentLeft;
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(100.0f, 0.0f, 175.0f, 40.0f)];
label2.tag = 2;
label2.textAlignment = NSTextAlignmentCenter;
[view addSubview:label1];
[view addSubview:label2];
}
label1.text = _firstList[row];
label2.text = _secondList[row];
return view;
}
Note how this properly makes use of the reused view.

Change UIPickerView pickerView:viewForRow: attributes based on a certain condition

I am trying to change the text color in viewForRow based on a certain condition. When I press a button the view changes to a different color but I would also like to change the colour of the text in the picker. I use 'viewForRow' because I have a custom view.
//adds subcategories to the wheel
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)];
label.backgroundColor = [UIColor clearColor];
if (!self.isBackgroundWhite)//Boolean that changes when the background changes
{
NSLog(#"Set white text");
label.textColor = [UIColor whiteColor];
}else{
label.textColor = [UIColor blackColor];
}
if (row == 1) {
label.text = [NSString stringWithFormat:#" %#",[self.servicesArray objectAtIndex:row]];
label.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18];
}else{
label.text = [NSString stringWithFormat:#" -%#",[self.servicesArray objectAtIndex:row] ];
label.font = [UIFont fontWithName:kAppFont size:16];
}
return label;
}
EDIT: Thanks to #Rich I was able to spot my problem, I just need to call [self.pickerView reloadAllComponents];
If you only want to change the text color just use the other delegate method pickerView:attributedTitleForRow:forComponent:
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *title;
UIFont *font;
if (row == 1) {
title = [NSString stringWithFormat:#" %#",[self.servicesArray objectAtIndex:row]];
font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18];
} else{
title = [NSString stringWithFormat:#" -%#",[self.servicesArray objectAtIndex:row] ];
font = [UIFont fontWithName:kAppFont size:16];
}
UIColor *textColor;
if (self.isBackgroundWhite) {
textColor = [UIColor blackColor];
} else {
NSLog(#"Set white text");
textColor = [UIColor whiteColor];
}
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.alignment = NSTextAlignmentLeft;
NSDictionary *attributes = #{NSForegroundColorAttributeName : textColor,
NSFontAttributeName : font,
NSParagraphStyleAttributeName : paragraphStyle};
return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}
Also a note that you should call [self.pickerView reloadAllComponents]; when changing isBackgroundWhite. I would do this by overriding the setter for backgroundWhite and when the boolean value changes reload the picker view.
EDIT:
There appears to be a 'bug' (intentional or not) in iOS 7 (works fine on iOS 6) with setting the UIFont on an NSAttributedString returned from the pickerView:attributedTitleForRow:forComponent: method. So while the above works for the text color the font does not change. Luckily you can get around this with the code in the question. See this answer for more info.

UIPickerView - multi-line rows - need layout advice

I want to make picker with 2 lines, I tried it like a link , but I can not understand what I need to do: create a view and 2 label on it, when I add labels coordinates to code, but selection field still like it as default. How can I change selection field size? And text in selection field have bigger size when other lines. Sorry for my english.
- (UIView*)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UIView* v;
if (view)
v = view;
else
{
v = [[UIView alloc] init] ;
UILabel* l1 = [[UILabel alloc] init];
l1.tag = 11;
[v addSubview: l1];
UILabel* l2 = [[UILabel alloc] init];
l2.tag = 12;
l2.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[v addSubview: l2];
}
UILabel* l1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 110, 35)];
l1.font = [UIFont systemFontOfSize:22]; // choose desired size
l1.text = [NSString stringWithFormat: #"row %d line 1", row];
l1.tag = 11;
[v addSubview: l1];
UILabel* l2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 34 , 110, 35)];
l2.font = [UIFont systemFontOfSize:14]; // choose desired size
l2.text = [NSString stringWithFormat: #"row %d line 2", row];
l2.tag = 12;
[v addSubview: l2];
return v;
}
UPDATE
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return yourViewHeight;
}
- (UIView*)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UIView* v;
if (view)
v = view;
else
{
v = [[UIView alloc] initWithFrame:CGRectMake(0, 34, 110, 35)] ;
UILabel* l1 = [[UILabel alloc] init];
l1.tag = 11;
[v addSubview: l1];
UILabel* l2 = [[UILabel alloc] init];
l2.tag = 12;
l2.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[v addSubview: l2];
}
UILabel* l1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 110, 35)];
l1.font = [UIFont systemFontOfSize:22]; // choose desired size
l1.text = [NSString stringWithFormat: #"row %d line 1", row];
l1.tag = 11;
[v addSubview: l1];
UILabel* l2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 34 , 110, 35)];
l2.font = [UIFont systemFontOfSize:14]; // choose desired size
l2.text = [NSString stringWithFormat: #"row %d line 2", row];
l2.tag = 12;
[v addSubview: l2];
return v;
}
http://i.picresize.com/images/2013/12/11/IiYqd.png
Hope It would work...
Implement this delegate method and return your view's height
(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return yourViewHeight;
}
Set frame for your label and your view..Frame of your label does not exceed the view's frame
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
//1. Create your view and set frame for the view
//2. Create your label 1 and label 2 set the frame for your labels
//3. Add and return your view
}
You should implement the pickerView:rowHeightForComponent: picker view delegate method and return a height tall enough for your custom view.
You should also set the height of v in your pickerView:viewForRow:forComponent:reusingView: method.
And lastly, if you are reusing a view, don't keep adding more labels. They will already be there from the reused view.
Try this code
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 50;
}
than after
- (UIView *)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 310, 50)];
lblTitle.numberOfLines=2;
lblTitle.textColor=[UIColor blackColor];
lblTitle.font=[UIFont systemFontOfSize:14];
lblTitle.text=[selectionList objectAtIndex:row];
}
return lblTitle;
}

Resources