Xcode 5 wrong font size on UITextField after editing - ios

I have UITextFields with custom font and size. Everything worked fine till I changed to Xcode 5 to fix all the changes with new iOS/Xcode. Now when I check my UITextFields they have the right font on placeholder and while editing, but when I stop editing the font size gets bigger. So why now with Xcode 5 it doesn't work?
Screenshots: link
Code to set font hasn't changed:
[_eventName setFont:[UIFont fontWithName:APP_FONT_FUTURASTD_LIGHT size:16]];

Have you made changes to the appearance proxy for UILabels? I'm having a similar issue where it appears as if UITextField's text is rendered as a UILabel after editing is completed. Overriding my appearance proxy settings on UILabel fixed this issue for me.

I too had an issue when inserting rows in a table - the text field in the table cell would use the UILabel proxy font, not the font set on its font property. Scrolling off the screen and back on would then show the correct font.
The only way I was able to fix this was to override UITextField, add a custom drawRect - in here I had to set self.font to nil, then reset self.font to the value I desired.
Chris

Now you need to use sizeWithAttributes: instead, which now takes an NSDictionary. Pass in the pair with key UITextAttributeFont and your font object like this :
[_eventName.text sizeWithAttributes:#{NSFontAttributeName:[UIFont fontWithName:APP_FONT_FUTURASTD_LIGHT size:16]}]

I had this problem, and just like described by chrisoneiota the problem is that the UITextField uses a UILabel for some situations. I solved it also by overriding the UITextField, but instead of implementing drawInRect: I do:
- (void)addSubview:(UIView *)view {
if ([view isKindOfClass:[UILabel class]]) {
UILabel *label = (UILabel *)view;
label.font = self.font;
}
[super addSubview:view];
}
This appears to work well.

Related

Using large font sizes on UISearchBar prevents the text from scrolling

I am changing my UISearchBar's font with:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.leftBarButtonItem = self.leftButton;
self.navigationItem.titleView = _searchBar;
//Change font size
UITextField *txfSearchField = [_searchBar valueForKey:#"_searchField"];
[txfSearchField setDefaultTextAttributes:#{
NSFontAttributeName: [UIFont fontWithName:#"Montserrat-Regular" size:30],
}];
}
but it has a side effect of preventing the text from scrolling when the text is longer that the textfield's size.
In the following screenshot I have written "This is a test":
However if I use "Helvetica" instead of "Montserrat-Regular" the result if fine:
Edit: Turns out this is a font size issue. The problem happens with any font (even Helvetica) if you set it above a "certain" size. This size is different for every font. I think when the font's height is larger than the textfield's height so that it gets "cropped" this is when the problem happens.
As a temporary workaround I have used a smaller font size, using Montserrat-Regular. As an alternative solution I want to try and increase the textfield's height and see if that works.

attributedText not setting with UITextView

I am trying to set custom font.
Font is working with UILabel.
When I tries to use for UITextView, its not working. UITextView is taking default font.
Any idea how to use custom font for UITextView.
Code I am using is as below.
attributedString = [[NSMutableAttributedString alloc] initWithString:text
attributes:#{ NSFontAttributeName : [UIFont fontWithName:#"GEDinarOne-Medium"
size:15], NSLigatureAttributeName: #2}];
abtUsText.attributedText = attributedString;
abtUsText.textAlignment = NSTextAlignmentRight;
abtUsText.textColor = cb60756;
I am using attributedString to get working with iOS 6.
iOS 7 is giving proper font, but iOS 6 is using default font for UITextView only.
Edit 1
I am using arabic font as GEDinarOne-Medium
Edit 2
This is not duplicate of what is mentioned above as my case is with arabic font and not english font. Mentioned in duplicate works with english custom fonts only.
After I investigate custom font (especially arabic) is not working UITextView using attributedString, below is what I did.
I just wanted to display the text in UITextView (for About Us in my app) as it has long text and I wanted scrolling option.
Below is what I did.
Add UIScrollView
Add UILabel inside UIScrollView
Assign text to UILabel.
abtUsLabel.attributedText = attributedString;
Make label sizeToFit
[abtUsLabel sizeToFit];
This is very important step, else it won't work.
Set scrollview contentSize as per label height.
[abtUsScrollView setContentSize:CGSizeMake(abtUsScrollView.frame.size.width, abtUsLabel.frame.size.height)];
Done... now if the Label is longer, we can scroll it.

Only some UIButtons work with customisation

I'm currently developing an iPad application and want to apply a custom font to the UIButtons on a certain screen. I have noticed similar problems with other screens, namely that the text on some (seemingly random) UIButtons disappears. In this case, the custom font is being applied to some buttons but not others, again there doesn't seem to be any pattern as to which buttons work and which don't. I've attached a screenshot below to try to give you an idea of what exactly I mean.
As I've mentioned, on some other screens I have noticed text completely disappearing from some buttons and have had to replace these with images featuring the text instead.
All buttons are created in Interface Builder. They use attributed text to allow multiple lines and centred alignment. Any help would be much appreciated.
edit - my code is like the following:
for (UIView *sub in view.subviews) {
UIButton *btn = (UIButton *) sub;
UILabel *lbl = [btn titleLabel];
[lbl setFont: myFont size: mySize];
}
To use attributed insure the IB items are set to use attributed text, not plain text.
To set the attributed title for a NSButton use:
- (NSAttributedString *)attributedTitleForState:(UIControlState)state
for a NSLabel use:
#property(nonatomic,copy) NSAttributedString *attributedText
Of course you may not need attributed text is all you are doing is just setting a font and alignment.
For multiple lines of text set:
#property(nonatomic) NSInteger numberOfLines
as appropriate.
From the comments:
You can achieve centered and multiline title labels in UIButtons without using NSAttributedStrings by adding the following lines to your for loop:
lbl.textAlignment = UITextAlignmentCenter;
lbl.numberOfLines = 0;

Edit UITextField Font Size with UIStepper

Is there a way to edit a UITextField's font size with a UIStepper? i.e, tap the add button and the UITextField's font size increases by 1 or 5? So far I have tried [textField adjustsFontSizeToFitWidth:value]; with no luck. How could I do this? Also, if I can't control the font size with a UIStepper, how else can I edit the font size? Thanks
I think you should be using:
[textField setFont:[UIFont systemFontOfSize:value]];
UIFont *textFont = [UIFont fontWithName:#"Helvetica-Bold" size:8.0];
[textField setFont:textFont];
Try This...

iPhone: UITextField default text problem

In my custom table cell I want to show some default text in my UITextField like "enter your name" and when user starts to edit the cell I want that text stays there and un-editable, and the editing starts just where the default text ends. Can I do this easily?
I can do that by putting a label on the left and a textfield on the right, but then I have to calculate eachtime how long is the default text(which can be different for each field) and custom create a textfield in that lenght to fit in.
Any suggestions?
I have the label calculate the size. Just set the label's text then have the label size to fit, and slide the UITextField right up to the edge of the label. If I have a trailing space in the label's text it looks great.
Something like this:
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 550.0, 28.0)];
[nameLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:24.0]];
[nameLabel setText:#"Name: "];
[nameLabel sizeToFit];
[self addSubview:nameLabel];
[nameLabel release];
UITextField *nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(nameLabel.frame.size.width + nameLabel.frame.origin.x, 0.0, 550.0 - nameLabel.frame.size.width, nameLabel.frame.size.height)];
Edit to answer the comment about special cases: In special cases such as very long labels you'll have to add additional code. For example you could check the width after the sizeToFit call, and if it exceeds a certain width, rebuild the label with multiple lines, smaller font, etc. Unfortunately, if you need to account for special cases, you'll have to do extra coding work.
Since you're using a UITextField, you can hook into the UITextFieldDelegate delegate methods to check the text of the text field at different states. For example, when the UITextField is becomes editable, and whenever the text in the text field is changed.
The two you might be interested in are:
- (void)textFieldDidBeginEditing:(UITextField *)textField
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Using these, you can easily have the placeholder property of UITextField contain whatever text you would like to use, then when the user begins to edit the field, have textFieldDidBeginEditing: fill in the textfield text with the placeholder text, then monitor the text at every character the user inputs using textField:shouldChangeCharactersInRange:replacementString: to not let the user delete the placeholder text you've input for them manually.

Resources