Custom Title View not Centered in iOS 10 - ios

This post is a separate topic but related to Custom Nav Title offset ios 11
I created a new thread because it is a separate issue.
From project: https://github.com/ekscrypto/Swift-Tutorial-Custom-Title-View
To recreate the problem, simply put a button on the existing Root View Controller that pushes another view controller. The "< Back" button scoots the title over, which makes it terribly uncentered. How can I fix this? Thank you.

Simple change required to support earlier versions of iOS; you should properly resize your custom title view to be the expected width its actually going to be. iOS 11 attempts to resize the width of the title view to fit the available space based on the constraints but iOS 10 and below will try to maintain the size of the view as much as possible.
The solution is therefore to open the MyCustomTitleView.xib file, and to set the width of the MyCustomTitleView to something reasonable like 180pt.
Cheers!

For iOS 10 and below you need to set up CGFrame for your attributed titleLabel.
Here is the code example.
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *titleLabel = [[UILabel alloc]init];
NSDictionary *fontAttribute = #{ NSFontAttributeName:[UIFont fontWithName:#"SFProText-Medium" size:15.f]};
NSAttributedString *str = [[NSAttributedString alloc]initWithString:#"YOUR TITLE"
attributes:fontAttribute];
titleLabel.attributedText = str;
[titleLabel sizeToFit]; // This method create a frame
self.navigationItem.titleView = titleLabel;
}
Swift example:
override func viewDidLoad() {
super.viewDidLoad()
let titleLabel = UILabel()
let title = NSMutableAttributedString(string: "Your title", attributes:[
NSAttributedStringKey.foregroundColor: UIColor.blue,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17.0, weight: UIFont.Weight.light)])
titleLabel.attributedText = title
titleLabel.sizeToFit()
self.navigationItem.titleView = titleLabel
}

Related

How to adjust the baseline shift for a UiLabel?

My app is using custom font to display an information for the whole app.
But there is some problems with their base line like in the image below:
I need to fix "X 4" to be centered in vertical red box.
How can I fix this?
Set attributed string with adjusted baseline offset like so in Swift:
let sttrStr = NSAttributedString.init(string: "your text",
attributes:[NSAttributedStringKey.baselineOffset: -10])
textLabel.attributedText = attrStr
or like so in ObjC:
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:#"your string"
attributes:#{ NSBaselineOffsetAttributeName : #-10 }];
textLabel.attributedText = attrStr;
Per SO answer here
You can create a UILabel and set its size same as the red box. And then set it's text alignment as center:
youLabel.textAlignment = NSTextAlignment.Center
Another not so good option is to make a UIButton of same size as red box and set its UserInteractionEnabled to No. This way your text will be positioned in the center horizontally and vertically. You can also set it's setContentVerticalAlignment property

Why is my NSAttributed String losing its attributes when the view reappears?

I am using UIAppearanceProxy in my app, like so:
UILabel *label = [UILabel appearance];
label.font = myAppFont;
label.textColor = myAppTextColor;
Sometimes, though, I want to override the default font to use glyphs from Awesome Font. I do that like this:
NSString *basePos = [NSString stringWithFormat:#"%d %#", (int)self.user.positives, kUpArrow];
NSMutableAttributedString *posString = [[NSMutableAttributedString alloc] initWithString:basePos];
[posString setAttributes:#{NSFontAttributeName:[UIFont fontWithName:#"FontAwesome" size:18]} range:NSMakeRange([posString length] - 1, 1)];
[self.positivesLabel setAttributedText:posString];
This works great when the view is built and appears, but if I navigate to another page (I am using UIPageViewController) and back, my attributes are gone.
I've tried re-setting-up the attributes in:
viewWillAppear:
viewDidAppear:
viewWillLayoutSubviews
viewDidLayoutSubviews
...to no avail. When is this string getting reset to the default? And how can I override that?
A similar thing was happening in to me.
The reason that this was happening was because I had set the font property on the appearance proxy of UILabel, and for whatever reason that overtook the attributes of the attributed string when the view re-appeared.
I fixed this by setting the font property on the UILabel to nil before setting the attributedText. Note that setting the font to any font also fixed this issue; it does not necessarily need to be set to nil.

Text Alignment issue when I set the custom font

When I set the custom font for the segmented control then it changes the vertical text alignment. I am using below code to set the font .
// I dont think these lines are creating any issue but just wanted to paste all the code
self.segmentType.layer.borderColor = navigationTintColor.CGColor;
self.segmentType.layer.cornerRadius = 0.0;
self.segmentType.layer.borderWidth = 1.5;
// These are the lines that are changing the text alignment
UIFont *font = [UIFont fontWithName:ftHelveticaNeueLTPro_Th size:13.5];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
forKey:UITextAttributeFont];
[self.segmentType setTitleTextAttributes:attributes
forState:UIControlStateNormal];
Here is the screenshot of whats is happening . If you observer, the text is not vertically centre aligned .
Please help me . Thank you in advance !!
The below code suggested by #Emmanuel works perfectly fine. You can change the vertical offset to align the text vertically at the center .
[self.segmentType setContentPositionAdjustment:UIOffsetMake(0, 2) forSegmentType:UISegmentedControlSegmentAny barMetrics:UIBarMetricsDefault];
Can you please try it using custom UILabel on custom view on it. Change & modify frame value of either titleLabel or customSegmentView as per convenience on actual view. And add this whole view as subview on your segmented control.
UIView *customSegmentView = [[UIView alloc] init];
UILabel *segmentTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 7.0f,180.0f,22.6f)];
segmentTitleLabel.text = #"your-text";
segmentTitleLabel.backgroundColor = [UIColor clearColor];
segmentTitleLabel.textAlignment = UITextAlignmentCenter;
segmentTitleLabel.font = [UIFont fontWithName:#"ftHelveticaNeueLTPro_Th" size:13.5f];
customSegmentView.frame = CGRectMake(60, 20, 180, 35);
[customSegmentView addSubview:segmentTitleLabel];
[self.segmentType setTitleView:customSegmentView];
Hope that will work for your issue. Please check and let me know if we have to go with another solution.
In InterfaceBuilder on XCode 6 there is a Content Offset control for the segments, which affects the baseline of the text. I had this problem because my Content Offset was 2 in the Y dimension instead of 0.

iOS change navigation bar title font and color

So i have this code that should change the nav bar title font, but it doenst
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont
fontWithName:_dataManager.optionsSettings.fontString size:14], NSFontAttributeName,
[UIColor whiteColor], NSForegroundColorAttributeName, nil];
[[UINavigationBar appearance] setTitleTextAttributes:attributes];
Changing the back button font with this code works just fine.
//set backbutton font
NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:_dataManager.optionsSettings.fontString size:15], NSFontAttributeName,
nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:normalAttributes
forState:UIControlStateNormal];
The correct way to change the title font (and color) is:
[self.navigationController.navigationBar setTitleTextAttributes:
#{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont fontWithName:#"mplus-1c-regular" size:21]}];
Edit: Swift 4.2
self.navigationController?.navigationBar.titleTextAttributes =
[NSAttributedString.Key.foregroundColor: UIColor.red,
NSAttributedString.Key.font: UIFont(name: "mplus-1c-regular", size: 21)!]
Edit: Swift 4
self.navigationController?.navigationBar.titleTextAttributes =
[NSAttributedStringKey.foregroundColor: UIColor.red,
NSAttributedStringKey.font: UIFont(name: "mplus-1c-regular", size: 21)!]
Swift 3:
self.navigationController?.navigationBar.titleTextAttributes =
[NSForegroundColorAttributeName: UIColor.redColor(),
NSFontAttributeName: UIFont(name: "mplus-1c-regular", size: 21)!]
Swift 5:
navigation.navigationBar.titleTextAttributes = [
.foregroundColor: UIColor.red,
.font: UIFont(name: "mplus-1c-regular", size: 21)!
]
There is nothing wrong with the other answers. I'm just sharing the storyboard version for setting the font.
1. Select Your Navigation Bar within your Navigation Controller
2. Change the Title Font in the Attributes Inspector
(You will likely need to toggle the Bar Tint for the Navigation Bar before Xcode picks up the new font)
Notes (Caveats)
Verified that this does work on Xcode 7.1.1+. (See the Samples below)
You do need to toggle the nav bar tint before the font takes effect (seems like a bug in Xcode; you can switch it back to default and font will stick)
If you choose a system font ~ Be sure to make sure the size is not
0.0 (Otherwise the new font will be ignored)
Seems like this works with no problem when only one NavBar is in the view
hierarchy. It appears that secondary NavBars in the same stack are ignored. (Note that if you show the master navigation controller's navBar all the other custom navBar settings are ignored).
Gotchas (deux)
Some of these are repeated which means they are very likely worth noting.
Sometimes the storyboard xml gets corrupt. This requires you to
review the structure in Storyboard as Source Code mode (right click
the storyboard file > Open As ...)
In some cases the navigationItem tag associated with user defined runtime attribute was set as an xml
child of the view tag instead of the view controller tag. If so
remove it from between the tags for proper operation.
Toggle the NavBar Tint to ensure the custom font is
used.
Verify the size parameter of the font unless using a dynamic font
style
View hierarchy will override the settings. It appears that one font
per stack is possible.
Result
Samples
Video Showing Multiple Fonts In Advanced Project
Simple Source Download
Advanced Project Download ~ Shows Multiple NavBar Fonts & Custom Font Workaround
Video Showing Multiple Fonts & Custom Fonts
Handling Custom Fonts
Note ~ A nice checklist can be found from the Code With Chris website and you can see the sample download project.
If you have your own font and want to use that in your storyboard, then there is a decent set of answers on the following SO Question. One answer identifies these steps.
Get you custom font file(.ttf,.ttc)
Import the font files to your Xcode project
In the app-info.plist,add a key named Fonts provided by
application.It's an array type , add all your font file names to the
array,note:including the file extension.
In the storyboard , on the NavigationBar go to the Attribute
Inspector,click the right icon button of the Font select area.In the
popup panel , choose Font to Custom, and choose the Family of you
embeded font name.
Custom Font Workaround
So Xcode naturally looks like it can handle custom fonts on UINavigationItem but that feature is just not updating properly (The font selected is ignored).
To workaround this:
One way is to fix using the storyboard and adding a line of
code: First add a UIView (UIButton, UILabel, or some other UIView
subclass) to the View Controller (Not the Navigation Item...Xcode is not currently allowing one to do that). After you add the control
you can modify the font in the storyboard and add a reference as an
outlet to your View Controller. Just assign that view to the
UINavigationItem.titleView. You could also set the text name in code
if necessary. Reported Bug (23600285).
#IBOutlet var customFontTitleView: UIButton!
//Sometime later...
self.navigationItem.titleView = customFontTitleView
Try this:
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],NSForegroundColorAttributeName,
[UIColor whiteColor],NSBackgroundColorAttributeName,nil];
self.navigationController.navigationBar.titleTextAttributes = textAttributes;
My Swift code for change Navigation Bar title:
let attributes = [NSFontAttributeName : UIFont(name: "Roboto-Medium", size: 16)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
self.navigationController.navigationBar.titleTextAttributes = attributes
And if you want to change background font too then I have this in my AppDelegate:
let attributes = [NSFontAttributeName : UIFont(name: "Roboto-Medium", size: 16)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
UIBarButtonItem.appearance().setTitleTextAttributes(attributes, forState: UIControlState.Normal)
ADD this single line code in your App Delegate - Did Finish Lauch. It will change Font, color of navigation bar throughout the application.
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: UIFont(name: "YOUR FONT NAME", size: 25.0)!]
Here is an answer for your question:
Move your code to below method because navigation bar title updated after view loaded. I tried adding above code in viewDidLoad doesn't work, it works fine in viewDidAppear method.
-(void)viewDidAppear:(BOOL)animated{}
Anyone needs a Swift 3 version. redColor() has changed to just red.
self.navigationController?.navigationBar.titleTextAttributes =
[NSForegroundColorAttributeName: UIColor.red,
NSFontAttributeName: UIFont(name: "{your-font-name}", size: 21)!]
It's a bit more readable using literals:
self.navigationController.navigationBar.titleTextAttributes = #{
NSFontAttributeName:[UIFont fontWithName:#"mplus-1c-regular" size:21],
NSForegroundColorAttributeName: [UIColor whiteColor]
};
I had one problem because when I tried to change my NavigationItem title programmatically i was not able to find my family font (tried many things but impossible to make it run correctly) so I found one workaround very nice and easy in storyboard.
Firstly you add under Navigation Item one view in middle and don't forget to set backGroundColor to clear color to have the same color of navBar:
Then you add one Label which you can edit (set color of text, font, size...) in this view
You add constraints to label (Top = 0, Bottom = 0, Trailing = 0 and Leading = 0) to View and center text of label
Finally you should have something like that in document outline:
And something like that in your ViewController:
Hope it can help.
iOS 11
Objective-C
if (#available(iOS 11.0, *)) {
self.navigationController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
self.navigationController.navigationBar.prefersLargeTitles = true;
// Change Color
self.navigationController.navigationBar.largeTitleTextAttributes = #{NSForegroundColorAttributeName: [UIColor whiteColor]};
} else {
// Fallback on earlier versions
}
Swift:-
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont(name:"Love Nature", size: 40)!]
This one for alternative to Swift 4 (already answer by #Josh):
let titleTextAttributed: [NSAttributedStringKey: Any] = [.foregroundColor: UIColor.red, .font: UIFont(name: "AvenirNext-Regular", size: 20) as Any]
navigationController?.navigationBar.titleTextAttributes = titleTextAttributed
For Objective-C to set Font and Color
- (void)_setup {
NSDictionary *barButtonTitleAttributes = #{
NSForegroundColorAttributeName : [UIColor whiteColor],
NSFontAttributeName :[UIFont fontWithName:#"Lato-Regular" size:15.0]
};
[self.navigationBar setTitleTextAttributes:barButtonTitleAttributes];
}
Here is an answer for Swift 4 😁:
let textAttributes:[String:Any]? = [NSAttributedStringKey.foregroundColor.rawValue:UIColor.blue, NSAttributedStringKey.font.rawValue:UIFont(name:"Avenir Next", size:20)!]
navigationController?.navigationBar.titleTextAttributes = textAttributes
Don't forget to add the Raw values of the keys to avoid compile errors.
let textAttributes:[NSAttributedStringKey: Any] = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue):UIColor.blue, NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue):UIFont(name:"OpenSans", size: 17)!]
navigationController?.navigationBar.titleTextAttributes = textAttributes
Swift 4.2
self.navigationController?.navigationBar.titleTextAttributes =
[NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont(name: "LemonMilklight", size: 21)!]
Add this extension
extension UINavigationBar {
func changeFont() {
self.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont(name:"Poppins-Medium", size: 17)!]
}
}
Add the following line in viewDidLoad()
self.navigationController?.navigationBar.changeFont()
Working in swift 3.0
For changing the title color you need to add titleTextAttributes like this
let textAttributes = [NSForegroundColorAttributeName:UIColor.white]
self.navigationController.navigationBar.titleTextAttributes = textAttributes
For changing navigationBar background color you can use this
self.navigationController.navigationBar.barTintColor = UIColor.white
For changing navigationBar back title and back arrow color you can use this
self.navigationController.navigationBar.tintColor = UIColor.white

How do you add multi-line text to a UIButton?

I have the following code...
UILabel *buttonLabel = [[UILabel alloc] initWithFrame:targetButton.bounds];
buttonLabel.text = #"Long text string";
[targetButton addSubview:buttonLabel];
[targetButton bringSubviewToFront:buttonLabel];
...the idea being that I can have multi-line text for the button, but the text is always obscured by the backgroundImage of the UIButton. A logging call to show the subviews of the button shows that the UILabel has been added, but the text itself cannot be seen. Is this a bug in UIButton or am I doing something wrong?
For iOS 6 and above, use the following to allow multiple lines:
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
// you probably want to center it
button.titleLabel.textAlignment = NSTextAlignmentCenter; // if you want to
[button setTitle: #"Line1\nLine2" forState: UIControlStateNormal];
For iOS 5 and below use the following to allow multiple lines:
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
// you probably want to center it
button.titleLabel.textAlignment = UITextAlignmentCenter;
[button setTitle: #"Line1\nLine2" forState: UIControlStateNormal];
2017, for iOS9 forward,
generally, just do these two things:
choose "Attributed Text"
on the "Line Break" popup select "Word Wrap"
The selected answer is correct but if you prefer to do this sort of thing in Interface Builder you can do this:
If you want to add a button with the title centered with multiple lines, set your Interface Builder's settings for the button:
[]
For IOS 6 :
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
As
UILineBreakModeWordWrap and UITextAlignmentCenter
are deprecated in IOS 6 onwards..
To restate Roger Nolan's suggestion, but with explicit code, this is the general solution:
button.titleLabel?.numberOfLines = 0
SWIFT 3
button.titleLabel?.lineBreakMode = .byWordWrapping
button.titleLabel?.textAlignment = .center
button.setTitle("Button\nTitle",for: .normal)
I had an issue with auto-layout, after enabling multi-line the result was like this:
so the titleLabel size doesn't affect the button size
I've added Constraints based on contentEdgeInsets (in this case contentEdgeInsets was (10, 10, 10, 10)
after calling makeMultiLineSupport():
hope it helps you (swift 5.0):
extension UIButton {
func makeMultiLineSupport() {
guard let titleLabel = titleLabel else {
return
}
titleLabel.numberOfLines = 0
titleLabel.setContentHuggingPriority(.required, for: .vertical)
titleLabel.setContentHuggingPriority(.required, for: .horizontal)
addConstraints([
.init(item: titleLabel,
attribute: .top,
relatedBy: .greaterThanOrEqual,
toItem: self,
attribute: .top,
multiplier: 1.0,
constant: contentEdgeInsets.top),
.init(item: titleLabel,
attribute: .bottom,
relatedBy: .greaterThanOrEqual,
toItem: self,
attribute: .bottom,
multiplier: 1.0,
constant: contentEdgeInsets.bottom),
.init(item: titleLabel,
attribute: .left,
relatedBy: .greaterThanOrEqual,
toItem: self,
attribute: .left,
multiplier: 1.0,
constant: contentEdgeInsets.left),
.init(item: titleLabel,
attribute: .right,
relatedBy: .greaterThanOrEqual,
toItem: self,
attribute: .right,
multiplier: 1.0,
constant: contentEdgeInsets.right)
])
}
}
In Xcode 9.3 you can do it by using storyboard like below,
You need to set button title textAlignment to center
button.titleLabel?.textAlignment = .center
You don't need to set title text with new line (\n) like below,
button.setTitle("Good\nAnswer",for: .normal)
Simply set title,
button.setTitle("Good Answer",for: .normal)
Here is the result,
There is a much easier way:
someButton.lineBreakMode = UILineBreakModeWordWrap;
(Edit for iOS 3 and later:)
someButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
Left align on iOS7 with autolayout:
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.textAlignment = NSTextAlignmentLeft;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
First of all, you should be aware that UIButton already has a UILabel inside it. You can set it using –setTitle:forState:.
The problem with your example is that you need to set UILabel's numberOfLines property to something other than its default value of 1. You should also review the lineBreakMode property.
Swift 5 , For multi Line text in UIButton
let button = UIButton()
button.titleLabel?.lineBreakMode = .byWordWrapping
button.titleLabel?.textAlignment = .center
button.titleLabel?.numberOfLines = 0 // for Multi line text
To fix title label's spacing to the button, set titleEdgeInsets and other properties before setTitle:
let button = UIButton()
button.titleLabel?.lineBreakMode = .byWordWrapping
button.titleLabel?.numberOfLines = 0
button.titleEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 20, right: 20)
button.setTitle("Dummy button with long long long long long long long long title", for: .normal)
P.S. I tested setting titleLabel?.textAlignment is not necessary and the title aligns in .natural.
For those who are using Xcode 4's storyboard, you can click on the button, and on the right side Utilities pane under Attributes Inspector, you'll see an option for Line Break. Choose Word Wrap, and you should be good to go.
Answers here tell you how to achieve multiline button title programmatically.
I just wanted to add that if you are using storyboards, you can type [Ctrl+Enter] to force a newline on a button title field.
HTH
Setting lineBreakMode to NSLineBreakByWordWrapping (either in IB or code) makes button label multiline, but doesn't affect button's frame.
If button has dynamic title, there is one trick: put hidden UILabel with same font and tie it's height to button's height with layout; when set text to button and label and autolayout will make all the work.
Note
Intrinsic size height of one-line button is bigger than label's, so to prevent label's height shrink it's vertical Content Hugging Priority must be greater than button's vertical Content Compression Resistance.
You have to add this code:
buttonLabel.titleLabel.numberOfLines = 0;
These days, if you really need this sort of thing to be accessible in interface builder on a case-by-case basis, you can do it with a simple extension like this:
extension UIButton {
#IBInspectable var numberOfLines: Int {
get { return titleLabel?.numberOfLines ?? 1 }
set { titleLabel?.numberOfLines = newValue }
}
}
Then you can simply set numberOfLines as an attribute on any UIButton or UIButton subclass as if it were a label. The same goes for a whole host of other usually-inaccessible values, such as the corner radius of a view's layer, or the attributes of the shadow that it casts.
As to Brent's idea of putting the title UILabel as sibling view, it doesn't seem to me like a very good idea. I keep thinking in interaction problems with the UILabel due to its touch events not getting through the UIButton's view.
On the other hand, with a UILabel as subview of the UIButton, I'm pretty confortable knowing that the touch events will always be propagated to the UILabel's superview.
I did take this approach and didn't notice any of the problems reported with backgroundImage. I added this code in the -titleRectForContentRect: of a UIButton subclass but the code can also be placed in drawing routine of the UIButton superview, which in that case you shall replace all references to self with the UIButton's variable.
#define TITLE_LABEL_TAG 1234
- (CGRect)titleRectForContentRect:(CGRect)rect
{
// define the desired title inset margins based on the whole rect and its padding
UIEdgeInsets padding = [self titleEdgeInsets];
CGRect titleRect = CGRectMake(rect.origin.x + padding.left,
rect.origin.x + padding.top,
rect.size.width - (padding.right + padding.left),
rect.size.height - (padding.bottom + padding].top));
// save the current title view appearance
NSString *title = [self currentTitle];
UIColor *titleColor = [self currentTitleColor];
UIColor *titleShadowColor = [self currentTitleShadowColor];
// we only want to add our custom label once; only 1st pass shall return nil
UILabel *titleLabel = (UILabel*)[self viewWithTag:TITLE_LABEL_TAG];
if (!titleLabel)
{
// no custom label found (1st pass), we will be creating & adding it as subview
titleLabel = [[UILabel alloc] initWithFrame:titleRect];
[titleLabel setTag:TITLE_LABEL_TAG];
// make it multi-line
[titleLabel setNumberOfLines:0];
[titleLabel setLineBreakMode:UILineBreakModeWordWrap];
// title appearance setup; be at will to modify
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setFont:[self font]];
[titleLabel setShadowOffset:CGSizeMake(0, 1)];
[titleLabel setTextAlignment:UITextAlignmentCenter];
[self addSubview:titleLabel];
[titleLabel release];
}
// finally, put our label in original title view's state
[titleLabel setText:title];
[titleLabel setTextColor:titleColor];
[titleLabel setShadowColor:titleShadowColor];
// and return empty rect so that the original title view is hidden
return CGRectZero;
}
I did take the time and wrote a bit more about this here. There, I also point a shorter solution, though it doesn't quite fit all the scenarios and involves some private views hacking. Also there, you can download an UIButton subclass ready to be used.
If you use auto-layout on iOS 6 you might also need to set the preferredMaxLayoutWidth property:
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
button.titleLabel.preferredMaxLayoutWidth = button.frame.size.width;
In Swift 5.0 and Xcode 10.2
//UIButton extension
extension UIButton {
//UIButton properties
func btnMultipleLines() {
titleLabel?.numberOfLines = 0
titleLabel?.lineBreakMode = .byWordWrapping
titleLabel?.textAlignment = .center
}
}
In your ViewController call like this
button.btnMultipleLines()//This is your button
It works perfectly.
Add to use this with config file like Plist, you need to use CDATA to write the multilined title, like this:
<string><![CDATA[Line1
Line2]]></string>
If you use auto-layout.
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.numberOfLines = 2
swift 4.0
btn.titleLabel?.lineBreakMode = .byWordWrapping
btn.titleLabel?.textAlignment = .center
btn.setTitle( "Line1\nLine2", for: .normal)
Roll your own button class. It's by far the best solution in the long run. UIButton and other UIKit classes are very restrictive in how you can customize them.
In iOS 15 in 2021, Apple for the first time officially supports multi-line UIButtons via the UIButton.Configuration API.
UIButton.Configuration
A configuration that specifies the appearance and behavior of a button and its contents.
This new API is explored in What's new in UIKit as well as the session:
Meet the UIKit button system
Every app uses Buttons. With iOS 15, you can adopt updated styles to create gorgeous buttons that fit effortlessly into your interface. We'll explore features that make it easier to create different types of buttons, learn how to provide richer interactions, and discover how you can get great buttons when using Mac Catalyst.
https://developer.apple.com/videos/play/wwdc2021/10064/
self.btnError.titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
self.btnError.titleLabel?.textAlignment = .center
self.btnError.setTitle("Title", for: .normal)
I incorporated jessecurry's answer within STAButton which is part of my STAControls open source library. I currently use it within one of the apps I am developing and it works for my needs. Feel free to open issues on how to improve it or send me pull requests.
Adding Buttons constraints and subviews. This is how i do it in my projects, lets say its much easier like this. I literally 99% of my time making everything programmatically.. Since its much easier for me. Storyboard can be really buggy sometimes
[1]: https://i.stack.imgur.com/5ZSwl.png
My experience:
Go to "Attribut" tab.
Texting in title, press "alt+Enter" while you want to jump to next line.
And check "Word Wrap" under "Attribut --> Control" field.
picture

Resources