UIButton.titlelabel setfont doesn't work - iphone

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.

Related

Make a UITextView object editable

This is obviously an extremely "noobish" question. I'm aware that UITextView has a Boolean editable property, (since I'm not entirely illiterate) so the answer to my question seems really straightforward. But...
I create a UITextView programatically like this:
UITextView* input = [[UITextView alloc] initWithFrame: CGRectMake(0, 0, 100, 30)];
input.backgroundColor = [UIColor whiteColor];
input.editable = YES;
input.text = #"Type something here...";
[input setUserInteractionEnabled: YES];
....
[mainView addSubview: input];
But when I run the iPhone simulator with Xcode, clicking on the input field doesn't do anything. I can't input any text, the virtual keyboard doesn't appear, etc.
So, what am I doing wrong here? Why isn't the UITextView object actually editable in the Xcode iPhone emulator?
I just ran your code in the viewDidLoad command of a sample app that I test code in and it works fine. In my case, I have a UIWebView that I added via Interface Builder which your UITextView pops on top of. I click it and the keyboard pops up.
Is it possible that you may be creating some other object that is begin added to the view after your textfield that is covering it up? Any other view objects being added in the .... part of your sample code?

How to increase the size of UITabBarItem in iphone?

I want to increase the Text size of UITabBarItem in my application.It is not visible clearly with its default color and size.
I tried this code but give me error -->UITabBar for instant message does not declare method with selector 'setTitleTextAttributes'.
Does any know how to do it?
[yourTabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[NSValue valueWithUIOffset:UIOffsetMake(0,0)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Helvetica" size:18.0], UITextAttributeFont, nil]
forState:UIControlStateNormal];
I think the default size is fully conveninet for the user. Anyway you want, maybe you should make your own <Tabbar> with UIView, UIButtons and UITabbar-style images.
setTitleTextAttributes:forState: is only available in iOS 5.0 or later. Please refer to UIBarItem Class Reference (UITabBarItem is subclass of UIBarItem). For prior versions of iOS, I think you'd better create your own customized tab bar.
And you may also want to try other methods from the answers in Changing font size of tabbaritem.
For that you should create dynamic tab bar using UITabbar class.
.Using this You can allow own size text ,image,color.

UITextField's custom font changes while in edit mode

I have a UITextField that is loaded from a xib. In it's view controllers viewDidLoad method, I set the font to a custom value which is set up correct in the .plist file and everything. It displays fine except for when it is in edit mode, at which point the font switches from my custom font to the default font, which I believe is Helvetica. This is jarring, and I'd like to keep the custom font throughout. I've looked around and I don't see any immediate solution, the only thing I've tried is resetting the textField.font property in the textFieldShouldBeginEditing and textFieldDidBeginEditing delegate methods, neither of which did anything.
Edit: I've been asked for the code, it really only is one line but here goes.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.myTextField.font = [UIFont fontWithName:#"MyFont" size:18];
}
I've also tried resetting the font in the following two methods:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
textField.font = [UIFont fontWithName:#"MyFont" size:18];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
textField.font = [UIFont fontWithName:#"MyFont" size:18];
return YES;
}
This does nothing, the font still changes while editing takes place, but then changes back to the custom font once the keyboard is dismissed.
Second edit:
Well, I just did something I probably should have tried before, and used a couple of different font files. Both of those fonts worked fine, but for whatever reason the custom font file I was using is causing the problem, despite it working normally in all other situations.
I had this problem with an otf font (in fact I've always had problems with otf fonts...) so now I use only ttf fonts, which work great. Check out this font converter website if you need to convert fonts from otf.
Try this:
- (void)textFieldDidChange:(UITextField *)textField {
textField.font = [UIFont fontWithName:#"MyFont" size:18];
}
Or as an alternative to fhat create an IBAction, connect it to the textField with Editing Changed and add this code to your .m:
- (IBAction)textFieldEditingChanged:(id)sender {
textField.font = [UIFont fontWithName:#"MyFont" size:18];
}
From my experience, the problem is with the font file. I found that the only solution was to find an alternative font and switch.

Setting font of UILabel with Interface Builder comes out Helvetica

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

Using UISegmentedControl as button

In my code I am using a UISegmentedControl as a "button" with only ONE segment and the momentary property set to YES. In versions of the SDK prior to iOS 4, this was not a problem, but it appears that now iOS 4 requires that there be at least 2 segments. The following code throws an exception:
NSArray *titles = [NSArray arrayWithObject:#"Button Title"];
myButton = [[UISegmentedControl alloc] initWithItems:titles];
and now in Interface Builder you cannot even create a UISegmentedControl with less than 2 segments. It logs the following error when building:
"The number of segments property of a segmented control must be greater than or equal to 2."
I'm kinda stumped. Any work arounds for this? I tried to create a UISegmentedControl with two buttons and then remove one programmatically and that "works" as it doesn't cause the app to crash. I get a button in iOS 3 and nothing in iOS 4. Any ideas?
Have you tried this:
[myButton removeAllSegments];
[myButton insertSegmentWithTitle:#"Press this" atIndex:0 animated:NO];
Really strange. It still works fine for me both in iOS4 simulator and device (this is a real working snippet from my code):
NSArray *laterContent = [NSArray arrayWithObjects: #"Maybe later", nil];
UISegmentedControl *later = [[UISegmentedControl alloc] initWithItems:laterContent];
CGRect frame = CGRectMake( 20,
98,
self.alert.bounds.size.width/2 - 30,
30);
later.frame = frame;
later.selectedSegmentIndex = -1;
[later addTarget:self action:#selector(laterAction:) forControlEvents:UIControlEventValueChanged];
later.segmentedControlStyle = UISegmentedControlStyleBar;
later.tintColor = [UIColor colorWithRed:130.0f/255.0f green:74.0f/255.0f blue:54.0f/255.0f alpha:0.8f];
later.momentary = YES;
later.alpha = 0.9;
It's not exactly a code-related solution but: I hit a similar issue and ended up drawing my own similar looking resources in Photoshop. It was not terribly difficult to do and removed a particular bad "code smell", IMO.
I found if you have a previous project with a single-segment UISegmentedControl, you can open both that project and your new one in Interface Builder and drag (or copy/paste) the single-segment UISegmentedControl to your new view controller. It will work fine in both your app and Interface Builder, just don't change the number of segments from 1 to anything else as it won't let you go back. I'm using Xcode 4.6.2 and iOS 6.
The editor in Interface Builder won't let you change the number of segments to be less than 1, but you can make a segmented control in IB by editing the .xib xml manually.
Right click on the .xib containing the segmented control
Choose Open As -> Source Code from the popup menu.
Find "<segments> which is the beginning of the xml array of segments. The whole thing should look like:
<segments>
<segment title="Segment 1 Title"/>
<segment title="Segment 2 Title"/>
</segments>
Just delete <segment title="Segment 2 Title"/> so there is only one segment element.
Right click the .xib again and choose Open As -> Interface Builder - iOS to go back to interface builder.
You should also probably set the segmented control to "momentary" mode.
I don't get any errors compiling or running this. Of course, this is a hack, and may break things in some circumstances or in a future iOS release.
Well two possibilities:
1) Create a button and the set background image as the single dot of the UISegmentedControl
If your SegmentedControl is a class variable just replace the #property
#property (nonatomic, retain) IBOutlet UIButton *button;
In the viewDidLoad-function add the following
-(void) viewDidLoad
{
[super viewDidLoad];
...
self.button = [UIButton alloc] init];
[self.button setBackgroundImage:[UIImage imageNamed:#"segmentedDot.png"] forState:(UIControlState)UIControlStateNormal];
}
2) Set the amount of segments to three of your UISegmentedControl and afterwards set the width to 20 - now only the dot in the middle will be shown.
Dont forget, if the user interacts with the UISegmentedControl, set the currentElement again to the second segment, else the dot will be in light grey instead of white state.
3) Place a button or a small view over the unwanted second dot of the UISegmentedControl in InterfaceBuilder. Make sure the backgroundcolor is even.
When you are using a button set the state for "user interaction" in attribute inspector to disabled. As type I would chose "custom" since you won't have some borders in your button ;)
Now male again sure, that always the first dot is the active Element.
However I think solution one should be the way you should go, since Apple thought something about it, when they disabled the 1-dot-SegmentedControl. Since you are using the Control as a button the Element you are looking fpr should be a button. ;)
There's no workaround in iOS 4. If you need this functionality, file a bug (enhancement request) at bugreport.apple.com.
You can also use removeSegmentAtIndex:animated:. If you create a segmented control in a storyboard or xib with two segments, you can remove one like this:
[self.sortButton removeSegmentAtIndex:1 animated:NO];