I load view from bundle and try to set custom font for label in it, but nothing changes.
Outlets for this view and labels are in file's owner.
Here's my code:
self.rikView = (RIKMainToolbar*)[[[NSBundle mainBundle] loadNibNamed:#"RIKMainToolbarPort" owner:self options:nil] objectAtIndex:0];
label.font = [UIFont fontWithName:#"PFDinTextCondPro-Medium.ttf" size:46];
What am I doing wrong?
UPD: I use this font in iPhone app, but there it's using for label in ViewController and sets in viewDidLoad function. And it works. So, I consider that problem is in loadWithNibName or in iPad.
Solved, thanks to #voromax. The problem was that labels' outlets belonged to file's owner. I've replace it with class and everything works)
I'm not sure I follow your code exactly, but assuming you're making the label.font assignment in initWithFrame of RIKMainToolbarPort, try moving it to the awakeFromNib method.
Open the FontBook application, and select "view font info". Copy the post-script-name of your desired font, and use that in your Obj-C code
You are not using the correct font name.
You seem to be using the font file's name, NOT the font name.
For example, in my current app I have the file Opal_1.ttf. However, I use "Opal" as the font name.
Check the example here: https://stackoverflow.com/a/1384206/855738
Related
Current Setup
I have a subclass of a UIView that loads a content from a xib file - call it a XibOwnerClass. Also, among other classes, there is a class called Triangle which helps me to create triangles with or without a border, with different stroke or fill color etc. That class is designable in storyboard and some of its properties are defined as IBInspectable.
Currently, in my xib file, I use this triangle view and setup its inspectable properties through IB. And that is really cool and convenient... So if I look into xib, I will actually see the triangle view among other views.
So, lets go further. In order to use this XibOwnerClass, I drag the UIView element to the storyboard, and change its custom class to XibOwnerClass, so I get my designable properties specific for XibOwnerClass. So, I can setup all things in IB and when I run the app, everything works.
The problem
Even if this works, I wonder if there is a way, to have multiple views (of class XibOwnerClass) dragged on a storyboard, and to be able to configure all of them individually trough Interface Builder?
Currently when I drag the UIView and change its custom class to XibOwnerClass I see nothing. I mean, the view is there, and it has its standard properties + inspectable properties. But I can't see triangles defined in xib in this new view. Is there a way to do this?
I am aware that xib is reused in my case (and it is meant to be used like that), so if I change something in a xib, all views that load from it will be affected. But is there a way multiple views to load from the same xib, but when loaded, to see them & setup them individually?
Here is how I load from xib:
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
if ((self = [super initWithCoder:aDecoder])){
UIView *myView = [[[NSBundle mainBundle] loadNibNamed:#"MyXib" owner:self options:nil] firstObject];
if (myView){
myView.frame = CGRectMake(myView.frame.origin.x, myView.frame.origin.y, self.frame.size.width, self.frame.size.height);
[self addSubview:myView];
//Initialize other stuff
[self commonInit];
}
}
return self;
}
Then in awakeFromNib: I use the values of inspectable properties...
EDIT:
Also I don't have initWithFrame: implemented, but I thought that it is not needed if load from nib (because it is not executed). But maybe this is needed while design time ?
can't really say what's wrong when you don't share class codes. but i guess you didnt use prepareForInterfaceBuilder. in your XibOwnerClass override prepareForInterfaceBuilder and call setups for view inside it for example change background colour or so.
override func prepareForInterfaceBuilder() {
//prepare setups or things to show in storyboard
}
Okay, I have solved it. It turned out that I shouldn't use Main Bundle but rather bundle for that class, like described here.
So here is the code:
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
#Mohammadalijf's answer in general is correct. It is just not a direct solution for my issue. But later on, when I fixed bundle thing, I used prepareForInterfaceBuilder like he said, and initialized things there so everything was available at design time.
Some useful stuff related to all this can be found here as well.
I'm trying to add 2 different font sizes for iphone and ipad layouts using size classes. It works cool with a default System font but doesn't work with custom font(I'm using PragmataPro in my project). If I add the second size for wR hR then font looks correctly in interface builder(I even checked xml) but in simulator and on device it becomes System instead of PragmataPro. But if I remove wR hR(or whatever layout I'm using for another size) then font shows correctly. Any idea how to solve this issue?
Thanks!
Subclass UILabel and override "layoutSubviews" method like:
- (void)layoutSubviews
{
[super layoutSubviews];
// Implement font logic depending on screen size
self.font = [UIFont fontWithName:#"CustomFont" size:self.font.pointSize];
}
Follow the link (it is a step-by-step from Apple):
https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/ChangingtheFontforaSizeClass.html
The 'custom' is there to define one single setting per type class.
If you don't define a 'custom' library with all the variations you want than I don't really think there to be a solution, mainly because by 'custom' you mean ONE SINGLE COMPONENT OF A GIVEN LIST OF CHOICES...
I'm currently working on an iOS project which has tons of labels/buttons/controls spread over dozens of scenes.
Most of those controls were created using Interface Builder.
So, it's now my job to make sure that every control (especially the labels) is formatted with the correct font family, which is not the case at the moment because many devs simply forget to change the font (our font must be set programmatically since it's not included in IB) after adding the control in IB.
Is there a way to change the system font temporarily so it's easier to see where font-settings have been forgotten?
I've searched for:
Changing the system font programmatically
Changing the font in Xcode somewhere
Changing the font in the iOS simulator (maybe as a debugging option)
But I was unsuccessful so far. I can't be the only one with this kind of problem - it's just naturally tedious to set every single control font programmatically.
The only thing I could imagine is like overriding the base UILabel's drawing method with a custom font (wingdings anyone?), but that seems a bit excessive?
You can try creating a category, which overrides the systemFontOfSize: method of UIFont or use method swizzling (you can find out more about method swizzling here: http://cocoadev.com/MethodSwizzling). Both are extremely ugly and shouldn't be used in production, but should be fine for testing purposes.
Here is an example category of UIFont:
#interface UIFont (SysFont)
#end
#implementation UIFont (SysFont)
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize {
return [UIFont fontWithName:#"YourFont" size:fontSize];
}
#end
I read many tutorials explaining how to add custom fonts to iOS apps.But in every tutorials its done through code.I mean, if i need change the font of 10 labels,i have to write code for each labels.I would like to know is there any method to add custom fonts to my project and then select that font from the storyboard ,i mean from the attributes list of label object..
Please help me in solving this....
That cannot be achieved through the interface builder only. However you can create a custom class subclassing UILabel. You define the .m file as follows:
#implementation CustomLabel
-(void) awakeFromNib{
[super awakeFromNib];
self.font = [UIFont fontWithName:#"<your custom font's file name>" size: self.font.pointSize];
//set other settings of the custom label here (colour, etc.)
}
#end
Then in the .xib file whenever you use a UILabel, set the class to be CustomLabel.
This can be done in Storyboard or Interface Builder with the use of very simple categories and user defined runtime attributes.
Something like this:
Please check this response for more details on how to achieve it.
Try This One:
UIFont *font = [UIFont fontWithName:#"YourFontName(without Extension)" size:30.0f];
self.yourLabel.font=[font fontWithSize:28];
add your font name in your projectname.plist like this
Fonts provided by application ---->Array------>2(items)
item0 --------->string----->urfontname with extension(ttf/otf) also
item1 --------->string----->secondfont name with extension(ttf/otf) also
This could now be done through Interface Builder in Xcode 6.
Now when you add custom font to your project and after making sure it is properly setup in your target settings, you should see your font in the regular font selector. Simply select Font > Custom, Family > your custom font should be listed here.
I currently have a bug after selecting the font in beta5 but this is how to do it. I installed Xcode 6 only for this feature (far more convenient than defining it programmatically everywhere).
In my app I have two screens - first to show a user profile, the second - to edit the profile information. They are similar. I have completed the xib file for the first screen.
What's the best way to reuse it on second screen?
You should encapsulate the related elements as a custom view class. You can tackle this problem by creating views with code instead of just xibs, and I would recommend this.
But, if you would prefer to use a xib, you can create one that models the stuff you want to reuse. And then in your view controller call some code like this:
UIView* aView = [UIView alloc] initWithFrame .....];
[[NSBundle mainBundle] loadNibNamed:#"MyReusableComponent" owner:aView options:nil];
UILabel* someLabel = aView.injectedLabel; //this is alive after loading the xib
[self.view addSubView:aView];
When you create your xib, your need to set the Files Owner to a class that will respond to the setters for the properties that will be injected. (Eg your new view class). This way you can wire up the references.
For more information, look at Apple's examples of loading table cells from a xib - this is the same technique. When you load a xib and specify the owner, it will inject the values from the xib into the owner, in this case a custom view.
Dou you mean that you enter the view controller's edit mode and reuse those those elements you have created ?
Enabling Edit Mode in a View Controller
You could use UITextFields (instead of UILabels you may have logically used for show) that you change in appearance, and switch enabled on/off. As a minimal example:
Show:
self.textField.borderStyle = UITextBorderStyleNone;
self.textfield.enabled = NO;
Edit:
self.textField.borderStyle = UITextBorderStyleBezel;
self.textfield.enabled = YES;
You could of course do more on appearance, than just these basics.
In Xcode: Go to file > duplicate.
Then name your duplicated xib something like "editProfile" This will give a duplicate of your first xib that you can adjust as necessary