Setting font of UILabel with Interface Builder comes out Helvetica - iphone

I'm having a problem setting the font for UILabels and UITextViews with Interface Builder.
I'm trying to set the font to Gill Sans.
If I set it programmatically it works fine, like this:
myLabel.font = [UIFont fontWithName:#"Gill Sans" size:24.0];
But if I try setting it with Interface Builder, I get the same behaviour described in this question here iPhone SDK: Interface Builder label font, only shows when editing label, but the Gill Sans is supposedly available on the iPad (and it is, since it works if I set it by code). And if I run it and do this:
NSLog(#"%#", myLabel.font.fontName);
it prints out "Helvetica".
Usually I wouldn't mind setting it programmatically, but the problem is that this particular class is used in several different places with different nib files to provide different layouts, so I can't have it hardcoded to always use the same font family. And subclassing it for each time it appears would be a huge pain, specially because I want to enable designers to create and change all the layout with interface builder whenever they want, and if they have to tell me what font they want every time so I can hardcode every particular case, that would be very awkward.
Has anyone experienced this problem before?
Maybe Interface Builder is limiting me to use the iPhone fonts even though the xib file's target is already set to iPad, but how do I convince it that I'm targeting the iPad?
Thanks in advance,
filipe

Ok, apparently this has been reported as a bug with IB 3.2.3:
https://devforums.apple.com/message/236134#236134
This guy says it used to work on 3.2.2, so I'll see if I can downgrade, or I'll just wait for a fix from Apple.

You can only use fonts that are present on the iPhone OS.
Check this answer for a way to embed custom fonts in your app.

Check out this post, if you do not want to create any IBOutlet.
https://stackoverflow.com/a/6620938/400909

Check out IBCustomFonts category I wrote:
https://github.com/deni2s/IBCustomFonts
IBCustomFonts category allows you to use custom fonts from Interface Builder (IB) when building your iOS apps.
Apps using IBCustomFonts category are approved by Apple App Store (as of September 2013).
No need to use IBOutlets, subclassing of UILabels and UIButtons or change fonts in code.
Tested on iOS6 and iOS7.

Cleaning all targets works few times. I had to quit IB and xcode to get the fonts in my last instance. I'm using xcode/IB 3.2.5 and I used chalkduster 13pt
HIH

If you use the subclassing UILabel approach and also want to keep the weight set in IB then use can do some thing like below. I couldn't find a better way to find out if the existing font is bold, italic or regular
- (void)awakeFromNib
{
[super awakeFromNib];
NSString *weight = [self.font.fontName substringFromIndex:[self.font.fontName length] - 5];
if ( [weight isEqualToString:#"-Bold"] )
{
self.font = [UIFont fontWithName:#"MyriadWebPro-Bold" size:self.font.pointSize];
}
else if ( [weight isEqualToString:#"-Italic"] )
{
self.font = [UIFont fontWithName:#"MyriadWebPro-Italic" size:self.font.pointSize];
}
else
{
self.font = [UIFont fontWithName:#"MyriadWebPro" size:self.font.pointSize];
}
}

I created a UILabel subclass that allows you to set a custom font in interface builder:
https://github.com/IntrepidPursuits/IPCustomFontLabel

Related

UIButton.titlelabel setfont doesn't work

I have added button in interface builder and referenced them, then I want to change their title font, but it doest work.
When I use:
[_button setFont:[UIFont fontWithName:APP_FONT_FUTURASTD_LIGHT size:24.0f]];
it WORKS, but this code is depreciated. So I have looked online to see what to use instead of this code, I found this code to use:
[_button.titleLabel setFont:[UIFont fontWithName:APP_FONT_FUTURASTD_LIGHT size:24.0f]];
_button.titleLabel.font = [UIFont fontWithName:APP_FONT_FUTURASTD_LIGHT size:24.0f];
but NEITHER of this work. I have no idea what to do with it or what is wrong. I have tried to set globally appearance of UIButton, that works too, but I cant use it as I need that font only on some buttons.
Button is as:
#property (nonatomic, weak) IBOutlet UIButton *button;
and added in storyboard.
Font is defined:
#define APP_FONT_FUTURASTD_LIGHT #"FuturaStd-Light"
and added to Info.plist section "Fonts provided by application".
I have found out what the problem was. The other programmer who has started this project has set
[[UIButton appearance] setFont:[UIFont fontWithName:APP_FONT_FUTURASTD_BOLD size:12.0f]];
in CSAppDelegate.m and so all my customizations were overwritten by this.
So to anyone who configures buttons font globally like this, you will not be able to then customize font of some buttons differently.
And second advice, if you continue work after someone else make sure that they give you some technical documentation or to explain to you what how and where they did.
EDIT
You can however set appearance for one view in viewWillAppear and then set it back to what you have in your CSAppDelegate.m in viewDidAppear of the view.
EDIT
Now this code is depreciated so you have to use this instead:
[[[UIButton appearance] titleLabel] setFont:UIFont];
The Problem seems to me is the Font name. The Font Name you are using in the code is not the exact name.
You Can Use the Following code to get List Of all Fonts name supported by your app
// List all fonts on iPhone
for (NSString* family in [UIFont familyNames])
{
NSLog(#"%#", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
NSLog(#" %#", name);
}
}
from the Console of xcode you can find out the exact name of the font that you added in your application
Please remove the AutoLayout checkmark if You used with Xib.It will works fine.
I think its helps to you.

UILabel - string as text and links

I have a UILabel whose text I am getting from a server. Some of the text is to be identified as links, and on touching those links some action should be performed. e.g.
NSString *str = #"My phone number is 645-345-2345 and my address is xyz";
This is the complete text for UILabel. I have only one UILabel for displaying this text (Text is dynamic. I just gave an example.). On clicking these links I need to perform actions like navigating to some different screen or make a call.
I know that I can display such text with help of OHAttributedLabel. And the links can be displayed as follows :
[label1 addCustomLink:[NSURL URLWithString:#"http://www.foodreporter.net"] inRange:[txt rangeOfString:someString]];
But I wonder how can I make these text links perform some action like navigation to different screen or making a call.
Let me know if more explanation is required.
You can add custom actions to any of the available UILabel replacements that support links using a fake URL scheme that you'll intercept later:
TTTAttributedLabel *tttLabel = <# create the label here #>;
NSString *labelText = #"Lost? Learn more.";
tttLabel.text = labelText;
NSRange r = [labelText rangeOfString:#"Learn more"];
[tttLabel addLinkToURL:[NSURL URLWithString:#"action://show-help"] withRange:r];
Then, in your TTTAttributedLabelDelegate:
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
if ([[url scheme] hasPrefix:#"action"]) {
if ([[url host] hasPrefix:#"show-help"]) {
/* load help screen */
} else if ([[url host] hasPrefix:#"show-settings"]) {
/* load settings screen */
}
} else {
/* deal with http links here */
}
}
TTTAttributedLabel is a fork of OHAttributedLabel.
If you want a more complex approach, have a look to Nimbus Attributed Label. It support custom links out-of-the-box.
You can use UITextView with Phone numbers and links detection YES, scrolling disabled YES user interaction enabled YES, instead of UILabel.
My project has successfully used OHAttributedLabel for this. Check out the
-(BOOL)attributedLabel:(OHAttributedLabel*)attributedLabel shouldFollowLink:(NSTextCheckingResult*)linkInfo;
method in OHAttributedLabelDelegate (link). It allows you to decide what happens when a link is clicked. If you look at the source for the example from the OHAttributedLabel project, it's used to display an alert. If you returned NO in this case (to keep the default action from happening too), you could just do whatever you wanted like navigation, etc.
Note however that this requires that you can determine the action correctly just from the text. For our project, we used a slightly fancier solution, where the server sent us text with tags in them and a list of commands to perform for each tag.
There a project called FancyLabel that is about what you need. It might need some customization though.
Also, I think Three20 has this functionality, but it might be an overkill if you don't already use it.
There's also a much simpler solution, if all of your links are phones \ addresses \ urls. You can simply use a UITextView instead of a UILabel. It has auto detection of phones, address, etc. (just check the boxes in IB)
You can also have custom actions in response to click events on those links by overriding openURL, as explained here
Is there a specific reason that you must use a UILabel instead of a UITextView?
Note that a lot of the implementations of attributed labels inherit from UIView or don't implement all of UILabel's functionality.
You can use custom button to give a look like of link ..Also you can add gesture on the custom label if you dont want to use button ..
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(userTappedOnLink:)];
// if labelView is not set userInteractionEnabled, you must do so
[labelView setUserInteractionEnabled:YES];
[labelView addGestureRecognizer:gesture];
I'm not a fan of being forced to use UITextView or a third party lib when all I need is a lightweight label that renders links (and that tells me when they're tapped!)
Here's my attempt at a lightweight UILabel subclass able to detect link taps. The approach is different from others I've seen in that it gains access to the UILabel's shared NSLayoutManager via a delegate callback added to NSTextStorage via a category extension. The beauty is that UILabel performs its native layout and drawing - other UILabel replacements often augment or replace the native behavior with an additional NSLayoutManager/NSTextContainer.
Probably App Store safe, but somewhat fragile - use at your own risk!
https://github.com/TomSwift/TSLabel

Is it possible to create universal app without separate xib except MainWindow.xib..?

Since last few days i have been searching and think about universal app conversation. I came to know that in Xcode 3.0 there is the UIViewAutoResizingMask property and you can convert your app into universal app with single xib. So, if anybody know this way than please help me.
Yes.
If you can't adjust the interface using springs and struts make device-based adjustments in code. E.g.
if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone) /* iPad */
{
self.someView.center = CGPointMake(100, 100);
}
else /* iPhone, iPod */
{
self.someView.center = CGPointMake(200, 200);
}
I believe that with objects such as tableview, navigation bars and tab bars etc, when in universal mode, these are automatically resized if using the same view. The only problem I might see is those objects such as buttons and the resizing it relocating of these. I myself am not sure about this, but I'm sure that there's a way of doing so in the same view.

Preview rounded image in iphone

can anyone tell me how can I create an image preview inside a customized cell with the aspect of the ones loaded in the mms'.
I've been trying to do it by changing values in IB and I haven't been able to.
Thanks a lot!
(source: iphonehelp.in)
Thanks for the three answers.
Doing
cell3.imageView.layer.masksToBounds = YES;
cell3.imageView.layer.cornerRadius = 16;
cell3.imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
cell3.imageView.layer.borderWidth = 1.0;
I was able to do part of what I want. However, I still can't put that "glow" look on the image. Does anybody now which property should I control?
Thanks a lot (and thanks again).
Sure. Add the QuartzCore framework to your project, then:
#import <QuartzCore/QuartzCore.h>
// ...
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 4;
Note that the cornerRadius property is only available in OS 3.0 and later; if you're supporting an older version (which you probably don't have to be), you'll need to do some Core Graphics work.
will creating an image in an image editor and then setting it as the cell's background not work?
This might be useful for you
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
If your image is in an image view and you're targeting OS 3.0+, you can set imageView.layer.cornerRadius.

Repeating background image in native iPhone app

Short of putting a UIWebView as the back-most layer in my nib file, how can I add a repeating background image to an iPhone app (like the corduroy look in the background of a grouped UITableView)?
Do I need to create an image that's the size of the iPhone's screen and manually repeat it using copy and paste?
Apparently a UIColor is not necessarily a single color, but can be a pattern as well. Confusingly, this is not supported in Interface Builder.
Instead you set the backgroundColor of the view (say, in -viewDidLoad) with the convenience method +colorWithPatternImage: and pass it a UI Image. For Instance:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:#"gingham.png"]];
}
Of course, don't forget to add the image file to your application bundle.
There are also some built-in background pattern "colors":
groupTableViewBackgroundColor
viewFlipsideBackgroundColor
Because the are used globally across all iPhone apps, you incur the double-edged sword of an OS update updating the look and feel of your application (giving it a fresh new look that may or may not work right).
For instance:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
}
You should have a look at the QuartzDemo iPhone example code from Apple, specifically QuartzImageDrawing.m. Should use the following method call.
CGContextDrawTiledImage
You can even have an animated tiled background images that move. :D
Apps Amuck has a simple tutorial that show you how to do this on their site.
31 Days of iPhone Apps - Day 16: World Tour
You should use colorWithPatternImage