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];
Related
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;
}
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
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 :
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;
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.