How to use custom font without size? - ios

I want to use a custom font in whole app (ios 5). I used the following code
[[UILabel appearance] setFont:[UIFont fontWithName:#"MyCustomFont" size:17.0]];
The problem with this is that it overrides all font sizes in all views.
How can i avoid it?

Create category for UILabel and add the following code over there.
#implementation UILabel (CustomFontLabel)
-(void)awakeFromNib{
float size = [self.font pointSize];
self.font = [UIFont fontWithName:#"MyCustomFont" size:size];
}
#end

Related

iOS Change system font to custom font globally

I want to change all system font to custom font at one place with storyboard and xib.
I have tried below code to change font.
[[UILabel appearance] setFont:[UIFont fontWithName:#"fontName" size:17.0]];
[[UITextField appearance] setFont:[UIFont fontWithName:#"fontName" size:17.0]];
When I used above code its working fine but its changing size also.
I just want to change font family and keep size as it has in storyboard or xib.
Please provide me some good solution.
I think you can keep using the same statements, but instead of setting the size using a literal of your choice, just use the current system font size like this:
float currentSystemFontSize = [UIFont systemFontSize];
[[UILabel appearance] setFont:[UIFont fontWithName:#"fontName" size:currentSystemFontSize]];
This should keep the system's font size and only change the font family.
Check this answer for more information about how to get system font size. Good luck!
call below methods in ViewDidLoad():
[self setCustomFontFamilyNotSize:#"CustomFontName" forView:self.view andSubViews:YES];
Method Dealarations:
-(void)setCustomFontFamilyNotSize:(NSString*)customFontFamily forView:(UIView*)view andSubViews:(BOOL)isSubViews
{
if ([view isKindOfClass:[UILabel class]])
{
UILabel *lbl = (UILabel *)view;
[lbl setFont:[UIFont fontWithName:customFontFamily size:[[lbl font] pointSize]]];
}
if ([view isKindOfClass:[UITextField class]])
{
UITextField *txt = (UITextField *)view;
[txt setFont:[UIFont fontWithName:customFontFamily size:[[txt font] pointSize]]];
}
}

How to Resize UITextField Placeholder Text to Fit UITextField width?

Can Change UITextField Placeholder Text Color as follows:
[txtField setValue:[UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0]
forKeyPath:#"_placeholderLabel.textColor"];
Similarly is there any way to Resize UITextField Placeholder Text Font to Fit UITextField width?
Note: I have text fields in custom cell of table view and also using autolayout in custom cell.
I met the same issue, here is the solution:
UILabel *placeHolderLabel = [self.registrationCodeTextfield valueForKey:#"_placeholderLabel"];
placeHolderLabel.adjustsFontSizeToFitWidth = YES;
Here is one coding that i used in ViewDidLoad to set the placeholder of textfield of desirable size.
-----------------------------------code-----------------------------------------
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
textfieldUsername.leftView = paddingView;
textfieldUsername.leftViewMode = UITextFieldViewModeAlways;
[[UITextField appearance] setTintColor:[UIColor whiteColor]];
[textfieldUsername setValue:[UIColor lightTextColor] forKeyPath:#"_placeholderLabel.textColor"];
(Nj)
You have the placeholder text and you have the frame of the uitextfield .
From these two you can calculate the font size which fits and assign as a Attributed Placeholder String.
If calculated font size is less than your limit , then use that font size other wise use the limit
Here is code
textField.placeholder = #"Pavankumar";
[textField setValue:[UIColor greenColor]
forKeyPath:#"_placeholderLabel.textColor"];
see below code it change font of the placeholder
textField.attributedPlaceholder = [[NSAttributedString alloc]initWithString:#"Pavankumar" attributes:#{NSForegroundColorAttributeName:[UIColor greenColor],NSFontAttributeName:[UIFont fontWithName:#"HelveticaNeue-light" size:12.0]}];

How to get the current font size of an UILabel

I have adjusted the UILabel font size according to the width in this way.
[btnPending.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:15]];
[btnPending setBackgroundColor:[UIColor clearColor]];
btnPending.titleLabel.adjustsFontSizeToFitWidth = YES;
btnPending.titleLabel.numberOfLines = 1;
btnPending.titleLabel.lineBreakMode = NSLineBreakByClipping;
[_vwTabBtnParent addSubview:btnPending];
And its working fine. But now I want to get the current font size of that UILabel. How can I do this in objective-c.
Please help me,
Thanks
Then to get the font name and size all you need is
NSString *fontName = self.label.font.fontName;
CGFloat fontSize = self.label.font.pointSize;
Try below method:
[label.text sizeWithFont:label.font
minFontSize:label.minimumFontSize
actualFontSize:&actualFontSize
forWidth:label.bounds.size.width
lineBreakMode:label.lineBreakMode];
Note: Deprecated in iOS 7 with no alternative.

how to change font size of label without changing font family in xcode

how to change font size of label without changing font family in xcode6 ?I am using following line of code:
lblMain.font = [UIFont systemFontOfSize:22];
Here's an example:
lblMain.font = [UIFont fontWithName:#"HelveticaNeue" size:22];
Or - you can even use the current font:
lblMain.font = [lblMain.font fontWithSize:22];
NSString *fontName = lblMain.font.fontName;
CGFloat fontSize = lblMain.font.pointSize;
[lblMain setFont:[UIFont fontWithName:fontName size:NEWSIZE]];
Use following code, if you just want to change font size keeping the same font family:
lblMain.font = [UIFont fontWithName:lblMain.font.fontName size:22.0f];
OR
You can also use this code:
lblMain.font = [lblMain.font fontWithSize:22.0f];
[lblMain setFont:[UIFont fontWithName:#"HelveticaNeue" size:17]];

uitextview font not changing with fontWIthName

I'm trying to change the font for a UITextView with textview.font = [UIFont fontWithName:etc];
But for some reason it isn't doing anything and it always displays the same font and size.
Here's the code:
UITextView *descriptionTextView = [[UITextView alloc] initWithFrame:rect];
descriptionTextView.editable = NO;
descriptionTextView.scrollEnabled = NO;
descriptionTextView.text = description;
titleTextView.font = [UIFont fontWithName:#"Veranda" size:17.0]; //<- this line isn't working
[cell.contentView addSubview:descriptionTextView];
Any idea what the probem might be?
Thanks a bunch!
May be the error in that you change font of other text field?
You operate with textview named 'descriptionTextView', but change font of 'titleTextView'
Or that default font is named "Verdana", not "Veranda"

Resources