I have a UIBarButtonItem in a UIToolbar at the top of my iPad app (iOS 5.1.) I have its width set to 65 in Interface Builder. It is of style 'bordered' and identifier 'custom.' The text label and tint changes when pressed:
[btnA setTitle:#"State A"];
[btnA setTintColor:[UIColor STATE_A_COL];
And so on, taking on various labels and colors. This worked fine, the button didn't resize even though the titles for the various states are quite different in length.
I then added this code to set the font, on startup:
UIFont * futura = [UIFont fontWithName:#"Futura" size:13];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:futura
forKey:UITextAttributeFont];
[btnA setTitleTextAttributes:attributes forState:UIControlStateNormal];
Now, the button is sized to fit the width of the title it has on startup. It changes size as the titles change. How can I lock the size? I don't understand the interplay here; I thought all I'd done was change the title font attribute, not anything else about the button.
I have also tried explicitly setting the width property:
[btnA setWidth:65.0];
Again to no avail.
I got the UIBarButtonItem to stop resizing by using the possibleTitles property, to give a hint as to the desired maximum width.
[btnA setPossibleTitles:[NSSet setWithObjects:#"State A', #"B", #"Final state", nil]];
This worked but I can't explicitly set the width to the size I want, so I'm leaving the question open.
Related
I want to make application in which i want to make a login screen. It has button having name "Log inn with Facebook". I want font size of Facebook greater than Log in with. How can I set it in main storyboard?Can anyone plz help me?
Follow these steps-
1.Select button
2.Change its type to custom in Attributes inspector
3.Change its title from Plain to Attributed
4.Change the part of title which you want to be set bold
A solution would be to have a UIView with two different UILabel as subviews ( your "Log in with" label and your "Facebook" label ).
Then, above this UIView you could have a UIButton with a white background and an alpha equal to 0.01 so the button would be touchable but invisible.
This UIButton would be the functional part of your UI.
NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:#"Login with Facebook"];
[yourbutton.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
// Set the font to bold from the beginning of the string to the ","
[titleText addAttributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] range:NSMakeRange(0, 10)];
// Set the attributed string as the buttons' title text
[yourbutton setAttributedTitle:titleText forState:UIControlStateNormal];
Instead of creating NSAttributedString create NSMutableAttributedString then you can just set the string like this.
I have a very strange bug trying to horizontally center text in UITextField object in iOS 7+ app.
I have set up a fresh project to isolate the bug and to make sure I'm not doing something wrong.
In viewDidAppear: method I alloc/init a new UITextField instance like this:
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(60.0f, 100.0f, 200.0f, 200.0f)];
tf.backgroundColor = [UIColor orangeColor];
tf.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:tf];
That's all. When I run this sample app on iPhone Retina (4-inch 64-bit) simulator I get following result:
After typing in some letters I get this:
Do anyone know what's is happening? The text should be centered while typing.
I didn't come across this bug till now. It is iOS related or am I doing something wrong?
P.s.: It is happening on iDevices also.
Thanks in advance.
EDIT:
Adding:
tf.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
tf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
doesn't help either.
EDIT 2:
Like Inder Kumar Rathore suggested I tried to set a height to 20.0f like so:
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(60.0f, 100.0f, 200.0f, 20.0f)];
and it surprisingly works. But it doens't solve the issue. I discovered that increasing height from 20.0f up makes issue only worse. I think it has something to do with line-height/text field height ratio. I don't know..
I think UITextField expects the vertical size of the font to be equivalent to the height of the text field. If you imagine a font at a point size that will fill the vertical height of your text field at 200 points, you will only be able to type a few characters before it fills the horizontal width and switches from the centered behavior to the right-aligned behavior. This seems to be the behavior in your "bug" above, where you can only type a few characters before it switches behaviors.
I think the best thing to do is to make your text field just high enough to fit your font, and vertically center it in a containing UIView.
I'm using a custom UIView as the titleView in a UINavigationBar. The UIView's only subview is a UIToolbar to which I've added two UIBarButtonItems. One contains an image (the twitter icon in the screenshot below) and the other contains title text.
On iPhone, this all looks great, but on iPad (using the same xib files and code) the vertical alignment is off. Here it is on iPhone:
And here it is on iPad (notice that the twitter icon isn't vertically aligned with the text in other button item):
The code I'm using to set the title is:
NSDictionary *textAttributes = #{NSFontAttributeName: [UIFont systemFontOfSize:17.f],
NSForegroundColorAttributeName: [UIColor whiteColor]};
[self.caseIdButtonItem setTitleTextAttributes:textAttributes
forState:UIControlStateNormal];
self.caseIdButtonItem.title = [NSString stringWithFormat:#"%#%#", NSLocalizedString(#"Case #", nil), self.caseId];
The above lines get called in my view controller's viewDidLoad.
How can I ensure that the bar button items are all aligned vertically in my UIToolbar?
I was able to fix this by explicitly setting the UIView's frame height to 44.
Apple's voice over mispronounces the title of one of my views, which is inside a UINavigation Controller.
In other parts of the app I have added a custom accessibility label to help it pronounce the company name correctly. How can I set the accessibility label of a UINavigationBar?
This works in iOS 8.2. In viewDidLoad:
self.navigationItem.accessibilityLabel = #"My accessible label";
When a navigation controller transitions to the view controller, the accessibilityLabel is read instead of the view controller title.
I couldn't add an accessibility label, but I found a workaround:
I replace the navigationItem's title View with a UILabel that has accessibility set up.
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = #"myTitle";
[titleLabel setAccessibilityLabel:#"myCustomAccessiblityLabel"];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20.0]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel sizeToFit];
self.navigationItem.titleView = titleLabel;
I'm not sure why setting the accessibility label doesn't work, but the above code works for my needs.
Since UINavigationBar inherits from UIView, you should be able to set its accessibilityLabel property. Try: yourUINavigationBar.accessibilityLabel = #"title";.
Also, you may need to ensure sure it is marked as an accessibility element with yourUINavigationBar.isAccessibilityElement = YES; (and is not inside another view which is also marked as an accessibility element). (I'm guessing this last bit may be the issue, since it appears that you already knew about accessibility labels. You can use the Accessibility Inspector in the simulator to see if this is the case by looking at the box around the element when you tap it to see if it's around something bigger than the navigator bar.)
There a similar workaround which I have tested on iOS 11 and 12 that works as expected. For the navigationItem.titleView set a UILabel object and then set your accessibility identifier for the navigationItem.titleView.
self.navigationItem.titleView = YOUR_CUSTOM_UILABEL;
accessibilityIdentifier part
self.navigationItem.titleView.accessibilityIdentifier = YOUR_ACCESSIBILITY_IDENTIFIER;
You can see your identifier with using Xcode's Accessibility Inspector.
As you can see in the picture below, my UIViewController IS NOT a UINavigationController, it's a common UIViewController. What I did is I put a UINavigationBar using interface builder and above it I put a UIImage. The problem is that I want to change the font of this UINavigationBar. Anyone would have a clue on how to do it?
Usually, with a common UINavigationController I use the following code:
// this will appear as the title in the navigation bar
self.label = [[UILabel alloc] initWithFrame:CGRectZero];
self.label.backgroundColor = [UIColor clearColor];
self.label.font = [UIFont fontWithName:#"Copperplate" size:22];
self.label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
self.label.textAlignment = UITextAlignmentCenter;
self.label.textColor = [UIColor whiteColor]; // change this color
self.label.text = [self.navigationItem title];
self.navigationItem.titleView = label;
[label sizeToFit];
Well it should work the same way. I think you just need an IBOutlet for the UINavigationBar, or only for the UINavigationItem (the title for your UINavigationBar) and that's it.
Storyboard Solution
There's nothing wrong with the answer above but a really simple way to do this is to select the Navigation Bar in the storyboard. Then change the Title Font in the attributes inspector.
Nota Bene
This technique is also really useful when you want to change the font
across an entire set of views whenever you are using a navigation
controller. (Just change it in one place). Xcode 7.1.1 has a couple of bugs. One of those requires that you toggle the Bar Tint from the default to another color (you can always reset it to the default if needed) in order to see the font change.
Custom Fonts
The above is currently not working when selecting a custom font (as of Xcode 7.1.1).
Please see the following SO Answer for a workaround if you need a
custom font. (tldr; add an outlet to a button or label, change the
custom font on that control, set that control as the
UINavigationItem.titleView).