changing font by scrolling UIPickerView - ios

Is it possible to change the font of a text while scrolling down/up a UIPickerView which has all the correct names of the system fonts? So that the text changes its font to the one that is currently selected in the pickerView.

You can set a method, for UIPickerView changes the selection while scrolling up/down, in viewdidload, or method where UIPickerview get present, like
[uorPickerView addTarget:self action:#selector(ValueChanged:) forControlEvents: UIControlEventValueChanged];
You can set the font which has been selected from UIPickerview in ValueChanged Method like
-(void)ValueChanged :(UIPickerView *) sender {
//Lets say you have UILable or UITextfield or UITextview as textView, so u can set your text like,
// fontArray = Array you have provided for UIPickerView datasource
[textView setFont:[UIFont fontWithName:[fontArray objectAtIndex:[sender selectedRowInComponent:0]] size:16]]
// make sure that fontArray has proper value of font family.
}
Hope this help you,
HTH, Enjoy Coding !!

You can get all system fonts and use it as your datasource
for(NSString *familyName in [UIFont familyNames]) {
for(NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"%#", fontName);
}
}
Implementing your delegate, you can create custom views for every row
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view {
UILabel *pickerLabel = (UILabel *)view;
if (pickerLabel == nil) {
CGRect frame = CGRectMake(0.0, 0.0, 80, 32);
pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
[pickerLabel setTextAlignment:UITextAlignmentLeft];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:/*font from datasource*/];
}
[pickerLabel setText:[pickerDataArray objectAtIndex:row]];
return pickerLabel;
}

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];

UIFont size not changing in UIPicker

I'm attempting to change the font and size of the elements in my UIPicker with the following code:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *pickerViewLabel = (id)view;
if (!pickerViewLabel) {
pickerViewLabel= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width - 10.0f, [pickerView rowSizeForComponent:component].height)];
}
pickerViewLabel.backgroundColor = [UIColor clearColor];
pickerViewLabel.text = _startTimeArray[row];
pickerViewLabel.font = [UIFont fontWithName:#"Helvetica Neue Regular" size:(22)];
return pickerViewLabel;
}
I'm able to change the type of font, but the size stays constant no matter what values I put in. Does anyone know why the font size is not changing? Thank you!
You have used wrong font name. Try HelveticaNeue.
You can find all available font names here

UIPickerView set selected row background color (now also for iOS 14)

I found this code here:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = (UILabel*) view;
if (label == nil)
{
label = [[UILabel alloc] init];
}
[label setText:#"Whatever"];
// This part just colorizes everything, since you asked about that.
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor blackColor]];
CGSize rowSize = [pickerView rowSizeForComponent:component];
CGRect labelRect = CGRectMake (0, 0, rowSize.width, rowSize.height);
[label setFrame:labelRect];
return label;
}
But, as stated in the comments, it has a problem:
It colorizes the labels, but not the picker, itself. So, when you roll to one end or the other, there's a blank spot where you can see the white background.
All I need is to change the background colour of the selected row.
I found that in iOS 14 I am now getting a gray background color on the selected item of the picker. Requirement currently is to get rid of that.
So this seems to work.
pickerView.subviews[safe:1]?.backgroundColor = UIColor.clear
("safe" is an operator that does array bounds checking and returns nil if the index is invalid)
I got the idea from here: https://stackoverflow.com/a/53740571/826946
We also can achieve that by subclassing UIPickerView and putting Andy's answer inside layoutSubviews overrided method.
class PickerView: UIPickerView {
override func layoutSubviews() {
super.layoutSubviews()
subviews[safe:1]?.backgroundColor = UIColor.clear
}
}

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.

iOS - Change text font for each segment in UISegmentController

I got a UISegmentedControl called mySegmentedControl
#property (nonatomic) IBOutlet UISegmentedControl *mySegmentedControl;
with 3 segments, and I would change text font for each segment.. Is possible?
Yes you can ----- Try this
for(uint i=0;i<[mySegmentedControl subviews].count;i++)
{
for(UIView *view in [[[mySegmentedControl subviews] objectAtIndex:i] subviews])
{
if([view isKindOfClass:[UILabel class]])
{
if(i==0) // set First segment font
[(UILabel*)view setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:27]];
if(i==1) // set Second segment font
[(UILabel*)view setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:17]];
if(i==2) // set Third segment font
[(UILabel*)view setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:7]];
}
}
}
You can use any images instead of text. This will give you flexible customization for appearance.
NSArray *segemtImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"1st.png"],[UIImage imageNamed:#"2nd.png"],[UIImage imageNamed:#"3rd.png"], nil];
UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:segemtImages];
I don't think that is possible. Your only options are either use a custom open source implementation, or set a custom image with the text for each segment.
I modify #Anand Natan 's code:
In storyboard set the first label "1111111111111" is to change it default width. Because change the label don't change segmentcontroll size.
for(uint i=0;i<[_menuBarTop subviews].count;i++){
for(uint j=0;j<[[[[_menuBarTop subviews] objectAtIndex:i] subviews]count];j++){
UIView* view = [[[_menuBarTop subviews] objectAtIndex:i] subviews][j];
if([view isKindOfClass:[UILabel class]]){
UILabel* label = (UILabel*)view;
if([label.text isEqualToString:#"1111111111111"]){
label.text = LANGLOC(#"videolist_category_choosen");
}else if([label.text isEqualToString:#"2"]){
label.text = LANGLOC(#"videolist_category_followed");
}
break;
}
}
}
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:12], NSFontAttributeName, nil];
[_sgmntStatusUI setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
for ios7 later varsions

Resources