I have a problem when setting a uibutton property:
[self.btnName.titleLabel setFont:[UIFont fontWithName:#"gillsansstd" size:16.0]];
It will disturb the text alignment, text shows top of button.
Why does setFont disturb the text alignment of uibutton?
Following Code set center (horizontally) of Alignment of UIButton titile .
myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
Other Way
[myButton.titleLabel setTextAlignment:UITextAlignmentCenter];
This is what you need.
[btnName.titleLabel setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
You can vertically align the text in a button.
Choose Edge Inset from xib file.
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.
However, I've applied different patches still I'm not able to fix placeholder text position and text color for UISearchbar.
But when I type something, it's showing up at a proper place.
What's the reason?
This is how you can change the colour of your placeholder text:
UITextField *searchField = [self.searchBar valueForKey:#"searchField"];
searchField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Some Text"];
UILabel *placeholderLabel = [searchField valueForKey:#"placeholderLabel"];
placeholderLabel.textColor = [UIColor blueColor];
As for the position, by default, placeholder text is vertically centre positioned in the UISearchBar. From your screenshot, it appears to me that there are few new line characters in the end of the text.
there's a category created on UITextField, inside it, the placeholder text is drawing. I just corrected the frame and now everything works fine, even that solve the color issue as well.
I have a UIButton and the text is centered within the button normally until the text string gets longer and it starts to scale down the size of the text. Then the text seems to stick to the bottom of the textField instead of staying centered in the button.
self.titleBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
self.titleBtn.titleLabel.minimumScaleFactor = .2;
[self.descriptionBtn setTitle:#"T" forState:UIControlStateNormal];
//works normally
but
[self.descriptionBtn setTitle:#"This a longer title" forState:UIControlStateNormal];
//is vertically lower than center
As noted here :)
You need to user baselineAdjustment propery of the UILabel
label.baselineAdjustment = UIBaselineAdjustmentAlignCenters
Swift 3
label.baselineAdjustment = .alignCenters
For Button
button.titleLabel?.baselineAdjustment = .alignCenters
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.
I have a problem with UINavigationBar in iOS 6: if navigation bar has too long title, then second (there are two button items) of right bar button items becomes hidden. iOS 7 is okay (must be fixed)
How to prevent such behaviour?
For this you can customize the title label of UINavigationBar. You can set its minimumFontSize property so make the text adjustable.
OR
For iOS 6 you can use below code, so that you can provide a custom label:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
label.textAlignment = UITextAlignmentCenter;
[label setFont:[UIFont boldSystemFontOfSize:16.0]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:text];
[self.navigationController.navigationBar.topItem setTitleView:label];
I would add a titleView with an embedded UILabel inside to have full control of how the title is displayed and how much it can "grow".
This question is the same as Back button title missing with long screen title in iOS 7
And the answer is the same answer I gave there: https://stackoverflow.com/a/22029442/341994
I quote that answer:
Make your screen title smaller. You can take control of it by using a titleView that's a UILabel. The advantage is that you can set its size, and that it can truncate its text and/or make the text occupy two lines if the text is too big (rather than just growing, as the title does).