I have more than 150 xibs in my app, I don't want to update the font for each xib, When I updates the font from settings to BOLD, it disturbs the font size of my app, I want to avoid that.
How can I avoid this?
Thanks in advance.
Hi You could Programmatically set the font as bold. Try This-
In Your View Controller. m -->
- (void)viewDidLoad
{
[super viewDidLoad];
[self makeFontLabel:self.label];
}
- (void)makeFontBold:(UILabel *)label
{
UIFont *labelFont = label.font;
UIFont *boldFont = [UIFont fontWithName:[NSString stringWithFormat:#"%#-Bold",labelFont.fontName] labelFont.pointSize];
label.font = boldFont;
}
Hope it helps you!
Related
I want to keep custom fonts in iOS app. Currently, I'm using iOS default fonts.
Is there any possible way for the same?
How do I include the custom font so that I can use it?
Make a category:
#import "UILabel+Helper.h"
#implementation UILabel (Helper)
- (void)setSubstituteFontName:(NSString *)name UI_APPEARANCE_SELECTOR {
self.font = [UIFont fontWithName:name size:self.font.pointSize]; }
#end
Then in AppDelegate.m:
[[UILabel appearance] setSubstituteFontName:#"SourceSansPro-Regular"];
This will change font in whole app even on UIButton, UILabel, inside UITableViewCell, UICollectionViewCell, UIView, UIContainerView, or anywhere in application without changing the font point size.
This is memory efficient approach rather than enumerating all views in your UIViewController.
Copy your font file into resources
Add a key to your Info.plist file called UIAppFonts. ("Fonts provided
by application")
Make this key an array
For each font you have, enter the full name of your font file
(including the extension) as items to the UIAppFonts array
Save Info.plist
Now in your application you can simply call following method to get the custom font to use with your UILabel and UITextView, etc…
[UIFont fontWithName:#"CustomFontName" size:15];
Check which fonts are available in your resources :
func allFonts(){
for family in UIFont.familyNames(){
println(family)
for name in UIFont.fontNamesForFamilyName(family.description)
{
println(" \(name)")
}
}
}
Change font for whole application in UILabel :
Add code in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[[UILabel appearance] setFont:[UIFont fontWithName:#"YourFontName" size:17]];
...
}
Change font in one viewConroller same as font size set in view design:
- (void)viewDidLoad
{
[super viewDidLoad];
[self setFontFamily:#"YourFontName" forView:self.view andSubViews:YES];
}
-(void)setFontFamily:(NSString*)fontFamily forView:(UIView*)view andSubViews:(BOOL)isSubViews
{
if ([view isKindOfClass:[UILabel class]])
{
UILabel *lbl = (UILabel *)view;
[lbl setFont:[UIFont fontWithName:fontFamily size:[[lbl font] pointSize]]];
}
if (isSubViews)
{
for (UIView *sview in view.subviews)
{
[self setFontFamily:fontFamily forView:sview andSubViews:YES];
}
}
}
1- Drag & Drop Font file .ttf to your project
2- Verify the font is in the project.
By selecting the font, and verifying “Target Membership” in the Utilities area.
3- Add info in plist
4- In delegate add this code
[[UILabel appearance] setFont:[UIFont fontWithName:#"YourFontName" size:11.0]];
[[UITextField appearance] setFont:[UIFont fontWithName:#"YourFontName" size:11.0]];
[[UITextView appearance] setFont:[UIFont fontWithName:#"YourFontName" size:11.0]];
I hope this will help
Ok, from all the other answers you are now very well familier that how to add a custom font (other than iOS default font) into app.
So now what? One thing is still remaining, I'm not sure if you want to use the same size custom font everywhere. If so,
I'm using this way to have custom font in my app.
If your app will have a single custom font then you can use it like this way for entire app.
#define appFontBold(x) [UIFont fontWithName:#"CustomFont-Bold" size:x]
#define appFontLight(x) [UIFont fontWithName:#"CustonFont-Light" size:x]
#define appFontRegular(x) [UIFont fontWithName:#"CustonFont-Regular" size:x]
I've added these macros globally (so can access it from anywhere)
and you can use it like this, yourLabel.font = appFontRegular(12); or yourTextField.font = appFontBold(14); this helps you in following situations,
Whenever you needs to change the custom font, you only need to change font name in macros.
Your code will looks clean, no need write [UIFont fontWithName: size:]; everywhere.
If your app will have more than one custom font then you can use it like this way for entire app.
then also you can define different macros like this,
Then above macro will be the same (as this is the maximum used fonts in app), for other fonts :
#define appFontBoldOtherFont(x) [UIFont fontWithName:#"CustomOtherFont-Bold" size:x]
#define appFontLightOtherFont(x) [UIFont fontWithName:#"CustonOtherFont-Light" size:x]
#define appFontRegularOtherFont(x) [UIFont fontWithName:#"CustonOtherFont-Regular" size:x]
Add your custom font into your project , i.e. Dragged the font file(CALIBRIZ_0.TTF) into XCode project.
Edit Info.plist: Add a new entry with the key "Fonts provided by application".
For each of your files, add the file name to this array
Now set font to your label
yourLabel.font = [UIFont fontWithName:#"Calibri" size:15];
If you want to change all fonts for all UILabel in one place, you need to create a category of UILabel.
for example:
#import <UIKit/UIKit.h>
#interface UILabel (AppFont)
#end
#implementation UILabel (AppFont)
-(void)awakeFromNib
{
self.font = [UIFont fontWithName:#"yourCustomFont" size:self.font.pointSize];
}
#end
after you've created the category, you will need to import this class for all the file, or you can add it to '.pch' file for your project.
same solution for buttons / textview ..
Can anybody please explain how can we make any font family font, bold or unbold + Italic or Non Italic + Underlined or Non underLined. Everywhere I got the method that make the changes but on system font. I even tried giving 2 attributes to NSAttributed string
1. Bold
2. A font family from list of supported font family
But it didnt work. Thanks in Advance.
try this
UIFontDescriptor *fontDescriptor = [[UIFontDescriptor alloc] init];
UIFontDescriptor *fontDescriptorForHelveticaNeue = [fontDescriptor fontDescriptorWithFamily:#"Helvetica Neue"];
UIFontDescriptor *symbolicFontDescriptor = [fontDescriptorForHelveticaNeue fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
UIFontDescriptor *symbolicFontDescriptor1 = [fontDescriptorForHelveticaNeue fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitMonoSpace];
NSString *text = #"iOS 7";
if(some condition){
CGSize fontSize = [text sizeWithAttributes:#{NSFontAttributeName:[UIFont fontWithDescriptor:symbolicFontDescriptor size:17.0f]}];
}
else{
CGSize fontSize = [text sizeWithAttributes:#{NSFontAttributeName:[UIFont fontWithDescriptor:symbolicFontDescriptor1 size:17.0f]}];
}
You can't do it with a few lines of code. A normal font and its bold version are completely separated (ie they're like two unrelated fonts), and if you have a look at iosfonts, the naming is not consistent. Some fonts don't have bold version, and some have several bold versions!
A solution (that requires a bit of effort, but surely works): create a list of pairs of font names, like this
{"ArialHebrew", "ArialHebrew-Bold"},
{"AvenirNext-Regular", "AvenirNext-Bold"},
...
And populate the list of fonts with the regular version (the left one). If the user desires to make it bold, then switch to the bold version (the right one).
My recommendation is to limit the number of choices for user (as iOS always does): you don't need to copy the whole list from iosfonts! just some popular ones are enough.
This may not be sufficiently generic for your purses, but maybe it'll help someone else
+ (UIFont *)whateverInvertedBoldnessFontFromFont:(UIFont *)font pointSize:(CGFloat)pointSize
{
NSString *customFontFamilyName = #"Whatever";
NSString *ibFontName = font.fontName;
NSString *customFontStyle = nil;
if ([ibFontName rangeOfString:#"Bold"].location != NSNotFound) {
customFontStyle = #"Regular";
}
else {
customFontStyle = #"Bold";
}
UIFont *customFont = [UIFont fontWithName:[NSString stringWithFormat:#"%#-%#", customFontFamilyName, customFontStyle] size:pointSize];
return customFont;
}
unfortunately this is quite fragile and works reliably when the source and destination
font families are known
If you want to set it programmatically, you must check with the font supported by xCode (iOS).
and if you want to do bold to any font then you have to use :
UIFont* boldFont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[myLabel setFont:boldFont];
where myLabel is your label name.
I'm trying to make the placeholders in my textfields italic, and since my app is targeting iOS 6.0 or newer, decided to use attributedPlaceholder property instead of rolling something more custom. The code goes as follows:
NSString *plString = #"optional";
NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:plString
attributes:#{NSFontAttributeName : [UIFont fontWithName:#"HelveticaNeue-LightItalic" size:15]}];
for (UITextField *t in myTextfields){
t.placeholder = plString;
t.attributedPlaceholder = placeholder;
}
Yet the styling of the placeholder still is not italic, but the same as regular text, just dimmer. What am I missing to make the NSAttributedString work?
As noted by warren, the styling currently can't be accomplished the way you're trying. A good workaround would be to set up your textfield's font attributes the way you would like your placeholder to look and then change the font of the textfield whenever the user begins typing. It will look like the placeholder and text are different fonts.
You can do this by creating a delegate of the textfield and utilizing shouldChangeCharactersinRange like this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// If there is text in the text field
if (textField.text.length + (string.length - range.length) > 0) {
// Set textfield font
textField.font = [UIFont fontWithName:#"Font" size:14];
} else {
// Set textfield placeholder font (or so it appears)
textField.font = [UIFont fontWithName:#"PlaceholderFont" size:14];
}
return YES;
}
This is almost certainly a bug. The documentation for the attributedPlaceholder property claims that the string will be drawn using a gray color regardless of the foreground color attribute, but this is not the case: you can set both the foreground and background colors. Unfortunately, the font attribute appears to get stripped out and reverted to the system font.
As a workaround, I recommend overriding drawPlaceholderInRect: and drawing the placeholder yourself. Additionally, you should file a Radar on this and include a minimal sample project that demonstrates the bug.
I just stumbled upon this issue myself. Apparently, the placeholder will take whatever font the textfield is being assigned with. Just set the textfield's font and you are good.
For everything else, like the colour of the placeholder, I'd still go back to attributedPlaceholder
iOS8/9/Swift 2.0 - working example
func colorPlaceholderText(){
var multipleAttributes = [String : NSObject]()
multipleAttributes[NSForegroundColorAttributeName] = UIColor.appColorCYAN()
//OK - comment in if you want background color
//multipleAttributes[NSBackgroundColorAttributeName] = UIColor.yellowColor()
//OK - Adds underline
//multipleAttributes[NSUnderlineStyleAttributeName] = NSUnderlineStyle.StyleDouble.rawValue
let titleString = "Search port/country/vessel..."
let titleAttributedString = NSAttributedString(string: titleString,
attributes: multipleAttributes)
self.textFieldAddSearch.attributedPlaceholder = titleAttributedString
}
I would like to change the style of the font (like bold, regular, light, oblique) programmatically. I know I can use the IB, but I would like to change it using programmatically. Need some guidance on this. Sorry if it is a stupid question.
For example, my code looks like this:
lblAge.font = [UIFont fontWithName:#"Helvetica" size:20];
I would like to add the style which is regular in. How do I it?
This should get you going
offerTitle.font = [UIFont fontWithName:#"TimesNewRomanPS-ItalicMT" size:14.0f];//here offerTitle is the instance of `UILabel`
Hope this helps:)
Try this solution:
myLabel.font = [UIFont boldSystemFontOfSize:16.0f];
myLabel.font = [UIFont italicSystemFontOfSize:16.0f];
For regular size:
myLabel.font = [UIFont systemFontOfSize:16.0f];
Hope it helps you.
For iOS 8.2 and above there is + systemFontOfSize:weight: which allows you to specify weighted system fonts.
Look at API of UIFont. Assign created font to the property 'font' of designated object
oneLabel.font=[UIFont fontWithXXX];
static NSString *_myCustomFontName;
+ (NSString *)myCustomFontName:(NSString*)fontName{
if ( !_myCustomFontName ){
NSArray *arr = [UIFont fontNamesForFamilyName:fontName];
// I know I only have one font in this family
if ( [arr count] > 0 )
_myCustomFontName = arr[0];
}
return _myCustomFontName;
}
I have been using a specific font which I've been testing on the iPad directly and it was working fine.
Now all of a sudden, the label or textview using the font are default back to Helvetica.
As a hack I'm setting the font manually, but this is ridiculous
self.taskNameView.font = [UIFont fontWithName:#"BradleyHandITCTT-Bold" size:24.0];
Any idea why all of a sudden, interface builder is freaking out?
I've tried cleaning but that didn't help.
I'm on XCode 4 and working on iOS 4.3
If you just want to override all fonts in your application, maybe a category on UIFont will do the trick?
Try adding this to e.g. your AppDelegate.m:
#implementation UIFont (FontOverride)
+(UIFont*) systemFontOfSize:(CGFloat)fontSize
{
return [UIFont fontWithName:#"radleyHandITCTT-Normal" size:fontSize];
}
+(UIFont*) boldSystemFontOfSize:(CGFloat)fontSize
{
return [UIFont fontWithName:#"radleyHandITCTT-Bold" size:fontSize];
}
+(UIFont*) italicSystemFontOfSize:(CGFloat)fontSize
{
return [UIFont fontWithName:#"radleyHandITCTT-Italic" size:fontSize];
}
#end