Changing UITextField Placeholder font - ios

I'm changing the placeholder text color with the following code, but when I try to add NSFontAttribute I get the compiler error "too many arguments to method call, expect 2 have 3"
UIColor *color = [UIColor blackColor];
_nameField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Your Name" attributes:#{NSForegroundColorAttributeName: color},#{NSFontAttributeName:#"Roboto-Bold"}];
This Works Fine:
UIColor *color = [UIColor blackColor];
_nameField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Your Name" attributes:#{NSForegroundColorAttributeName: color}];

Objective-C:
UIColor *color = [UIColor blackColor];
someUITextField.attributedPlaceholder =
[[NSAttributedString alloc] initWithString:#"Placeholder Text"
attributes:#{
NSForegroundColorAttributeName: color,
NSFontAttributeName : [UIFont fontWithName:#"Roboto-Bold" size:17.0]
}
];
(There are no brackets between literal dictionary key-value pairs.)
Swift:
let attributes = [
NSForegroundColorAttributeName: UIColor.blackColor(),
NSFontAttributeName : UIFont(name: "Roboto-Bold", size: 17)! // Note the !
]
someUITextField.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes:attributes)

Update for Swift 4.x:
textField.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes: [
.foregroundColor: UIColor.lightGray,
.font: UIFont.boldSystemFont(ofSize: 14.0)
])

For the convenience of swift people:
someTextField.attributedPlaceholder = NSAttributedString(string: "someString",
attributes:[NSForegroundColorAttributeName: UIColor.lightGrayColor(), NSFontAttributeName: PlaceHolderFont])

You should subclass UITextField, and override the method of:
- (void)drawPlaceholderInRect:(CGRect)rect;
Here is the implementation below:
- (void)drawPlaceholderInRect:(CGRect)rect {
NSDictionary *attributes = #{
NSForegroundColorAttributeName : [UIColor lightGrayColor],
NSFontAttributeName : [UIFont italicSystemFontOfSize:self.font.pointSize]
};
// center vertically
CGSize textSize = [self.placeholder sizeWithAttributes:attributes];
CGFloat hdif = rect.size.height - textSize.height;
hdif = MAX(0, hdif);
rect.origin.y += ceil(hdif/2.0);
[[self placeholder] drawInRect:rect withAttributes:attributes];
}
For more you can find click here

Appreciate #ddevaz answer.
UITextField Subclass solution
Above answer works perfect for UITextField. But when i use UITextField subclass and try to execute this method in it. Then its not working.
I found other solution only when you subclass UITextField. Override below method in your UITextField subclass and it will do job for you.
- (void) drawPlaceholderInRect:(CGRect)rect {
NSDictionary *attrDictionary = #{
NSForegroundColorAttributeName: [UIColor lightGrayColor],
NSFontAttributeName : [UIFont fontWithName:#"Menlo" size:17.0]
};
[[self placeholder] drawInRect:rect withAttributes:attrDictionary];
}

Working version for Swift 4
let attributes = [NSAttributedStringKey.foregroundColor: UIColor.black,
.font : UIFont.systemFont(ofSize: 14, weight: .regular)]
someTextfield.attributedPlaceholder = NSAttributedString(string: "Placeholder text", attributes:attributes)

Update for Swift 4
let attributes = [
NSAttributedStringKey.foregroundColor: .black,
NSAttributedStringKey.font : UIFont(name: "Your font name", size: 14)!
]
someTextfield.attributedPlaceholder = NSAttributedString(string: "Placeholder text", attributes:attributes)

Update for Swift 4.2 +
let attributes = [NSAttributedString.Key.foregroundColor: UIColor.black,
.font: UIFont.systemFont(ofSize: 14)]
searchTextField.attributedPlaceholder = NSAttributedString(string: "Placeholder text",
attributes: attributes)
Reference for more attribute Keys - NSAttributedString.Key

If you want to support both iOS 6 and previous versions then:
UIColor *placeholderColor = [UIColor lightTextColor];
UIFont *placeholderFont = [UIFont systemFontOfSize:[UIFont systemFontSize]];
if ([textField respondsToSelector:#selector(attributedPlaceholder)]) {
#ifdef __IPHONE_6_0
textField.attributedPlaceholder = [[NSAttributedString alloc]
initWithString:textField.placeholder attributes: // NOTE: textField.placeholder can't be nil
#{ NSForegroundColorAttributeName : placeholderColor,
NSFontAttributeName : placeholderFont }];
#endif
} else { // for pre-iOS6
[textField setValue:placeholderColor forKeyPath:#"_placeholderLabel.textColor"];
[textField setValue:placeholderFont forKeyPath:#"_placeholderLabel.font"];
}

You can use below code
First find name of system accepted font for your Custom font
for (NSString *familyName in [UIFont familyNames]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"%#", fontName);
}
}
and then use below code make your placeholder with custom font
searchTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Where are you shopping?" attributes:#{NSFontAttributeName : font }];
Else there can be chance to get nil object[1] for font not found

let attributes: [NSAttributedStringKey : Any] = [NSAttributedStringKey.foregroundColor: UIColor.black,
NSAttributedStringKey.font: UIFont(name: "Menlo", size: 17.0) ?? UIFont.systemFont(ofSize: 17.0) ]
textField.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes: attributes)
Note: when we use UIFont(name: "Menlo", size: 17.0) method, if the font name can't get it in ios , so it will be nil, so we need to provide the default font. It was explained above.
You can use below code First find name of system accepted font for your Custom font
for (NSString *familyName in [UIFont familyNames]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"%#", fontName);
}
}

Swift 4: Placeholder Color change using UITextField subclass
override func drawPlaceholder(in rect: CGRect) {
let color = UIColor(red: 210/255, green: 210/255, blue: 210/255, alpha: 1.0)
if (placeholder?.responds(to: #selector(NSString.draw(in:withAttributes:))))! {
let fontS = UIFont.systemFont(ofSize: 12)
let attributes = [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: fontS] as [NSAttributedStringKey : Any]
let boundingRect: CGRect = placeholder!.boundingRect(with: rect.size, options: [], attributes: attributes, context: nil)
placeholder?.draw(at: CGPoint(x: 0, y: (rect.size.height / 2) - boundingRect.size.height / 2), withAttributes: attributes)
}
}

if you have problem same me. I solve by set Font because some time font can't change when set Font in stroryboard or set in self.emailTextField.placeholder .
self.emailTextField.font = UIFont(..custom you font....)

You can change the font size of the text and placeholder by simply doing this.
field.font = .systemFont(ofSize: 16)

Related

UIButton textLabel with different fonts

Is there a way to have a UIButton with two different font sizes in its textLabel? Natively?
I don't want to have a UILabel on top.
You can do this in interface builder. This GIF will show you how to increase the size of one section of the text and perhaps change its font.
To do this in code:
NSString *fullString = #"This bit's plain. This bit's bigger";
NSRange rangeOfPlainBit = [fullString rangeOfString:#"This bit's plain."];
NSRange rangeOfBigBit = [fullString rangeOfString:#"This bit's bigger"];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:fullString];
[attributedText setAttributes:#{NSFontAttributeName:[UIFont fontWithName:#"My-font" size:15.0],
NSForegroundColorAttributeName: [UIColor whiteColor]}
range:rangeOfPlainBit];
[attributedText setAttributes:#{NSFontAttributeName:[UIFont fontWithName:#"My-font" size:25.0],
NSForegroundColorAttributeName: [UIColor whiteColor]}
range:rangeOfBigBit];
[self.myButton setAttributedTitle:attributedText forState:UIControlStateNormal];
SWIFT 3
func customizeButtonFont(fullText: String, mainText: String, creditsText: String, button: UIButton) {
let fontBig = UIFont(name:"SFUIDisplay-Medium", size: 16.0)
let fontSmall = UIFont(name:"SFUIDisplay-Light", size: 14.0)
let attributedString = NSMutableAttributedString(string: fullText, attributes: nil)
let bigRange = (attributedString.string as NSString).range(of: mainText)
let creditsRange = (attributedString.string as NSString).range(of: creditsText)
attributedString.setAttributes([NSAttributedStringKey.font: fontBig, NSAttributedStringKey.foregroundColor: UIColor.white], range: bigRange)
attributedString.setAttributes([NSAttributedStringKey.font: fontSmall, NSAttributedStringKey.foregroundColor: UIColor.white], range: creditsRange)
button.setAttributedTitle(attributedString, for: .normal)
}
just change the font size for the title.
swift:
button.titleLabel.font = UIFont.systemFontOfSize(FONTSIZE)

AsyncDisplayKit ASButtonNode setTitle not work

I use AsyncDisplayKit write button , and use ASButtonNode , but I can't set title when I use - (void)setAttributedTitle:(NSAttributedString *)title forState:(ASButtonState)state . It can respond action, but without title. Am I worry or something else need set ?
Here is code :
NSDictionary *placeholderAttrs = #{NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue-LightItalic" size:14.0f] , NSForegroundColorAttributeName: [UIColor greenColor] };
ASButtonNode *button = [[ASButtonNode alloc] init];
[button setAttributedTitle:[[NSAttributedString alloc] initWithString:#"button" attributes:placeholderAttrs] forState:ASButtonStateNormal];
[button addTarget:self action:#selector(action:) forControlEvents:ASControlNodeEventTouchUpInside];
[button setFrame:CGRectMake(20, 20, 100, 44)];
button.backgroundColor = [UIColor redColor];
button.titleNode.displaysAsynchronously = NO;
[self.view addSubnode:button];
Thanks.
#leo, It looks like you may just need to measure your ASButtonNode before adding it to your view.
[button measure:...];
Please reference into this topic here Add custom Button with AsyncDisplayKit
Basiclly, ASTextNode will be used as button, also below func example aim to set title, color, font-weight and color (in Swift)
func configureTextNode(text: String, size: CGFloat, frame : CGRect = CGRectMake(0,0,0,0), bold: Bool = false, color: UIColor = UIColor.whiteColor(), textAlignment: NSTextAlignment = .Left) -> ASTextNode {
let textNode = ASTextNode()
let range = NSMakeRange(0, text.characters.count)
// Set String
let mutableString = NSMutableAttributedString(string: text) // Set string
// Set size and font-weight
let fontSize = bold ? UIFont.boldSystemFontOfSize(size) : UIFont.systemFontOfSize(size)
mutableString.addAttribute(NSFontAttributeName, value: fontSize, range: range)
// Set text color
mutableString.addAttribute(NSForegroundColorAttributeName, value: color, range: range)
// Set alignment
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = textAlignment
mutableString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: range)
// Add attributes into node
textNode.attributedString = mutableString
// Node Frame
textNode.frame = frame
return textNode
}
you can use this code
fileprivate var noteButtonNode : ASButtonNode = ASButtonNode()
addSubnode(noteButtonNode)
let attributedTitle : NSAttributedString = NSAttributedString(
string: "Your_Text",
attributes: [
NSFontAttributeName: UIFont.init(name: Fonts.Your_Font, size: 16)!,
NSForegroundColorAttributeName: Colors.Your_Color
])
noteButtonNode.setAttributedTitle(attributedTitle, for: ASControlState())

How to change UITextfield placeholder color and fontsize using swift 2.0?

How to change UITextfield placeholder & fontsize in SWIFT 2.0?
#1. set Placeholder textfield color Programmatically
var myMutableStringTitle = NSMutableAttributedString()
let Name = "Enter Title" // PlaceHolderText
myMutableStringTitle = NSMutableAttributedString(string:Name, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 20.0)!]) // Font
myMutableStringTitle.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range:NSRange(location:0,length:Name.characters.count)) // Color
txtTitle.attributedPlaceholder = myMutableStringTitle
OR
txtTitle.attributedPlaceholder = NSAttributedString(string:"Enter Title", attributes: [NSForegroundColorAttributeName: yellowColor])
Note : Name is your placeholder of textField.
PlaceHolder TextFiled :
-------------------------------- OR -------------------------------------
#2. set Placeholder textfield color at runtime attribute
Set textfield placeHolder text Enter Title
Click on identity inspector of textfield property.
User Define Runtime Attributes, add color attributes
Key Path : _placeholderLabel.textColor
Type : Color
value : Your Color or RGB value
PlaceHolder TextFiled :
Updated for Swift 3
If you want to change the UITextField Placeholder color for Swift 3, use the following lines of code:
let yourTextFieldName = UITextField(frame: CGRect(x: 0, y: 0, width: 180, height: 21))
yourTextFieldName.attributedPlaceholder = NSAttributedString(string: "placeholder text", attributes: [NSForegroundColorAttributeName: UIColor.white])
Updated for Swift 5
For swift 5.0 use NSAttributedString.Key.foregroundColor instead of NSForegroundColorAttributeName
So, do it like so
textField.attributedPlaceholder = NSAttributedString(string: "placeholder text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
For swift 4 instead of
NSForegroundColorAttributeName
use
NSAttributedStringKey.foregroundColor
You can try with this sample code
let textFld = UITextField();
textFld.frame = CGRectMake(0,0, 200, 30)
textFld.center = self.view.center;
textFld.attributedPlaceholder = NSAttributedString(string:"Test Data for place holder", attributes:[NSForegroundColorAttributeName: UIColor.blueColor(),NSFontAttributeName :UIFont(name: "Arial", size: 10)!])
self.view.addSubview(textFld)
Placeholder for textfield Objective C
NSString* str = #"Placeholder text...";
NSRange range1 = [str rangeOfString:#"Placeholder text..."];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:str];
[attributedText setAttributes:#{
NSFontAttributeName:[UIFont fontWithName:customFont_NotoSans_Regular size:13.0]
}
range:range1];
[attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:range1];
txtFld.font = [UIFont fontWithName:customFont_NotoSans_Regular size:13.0];
txtFld.keyboardType = UIKeyboardTypeDefault;
txtFld.attributedPlaceholder = attributedText;
It's easy to do with a subclass of UITextField.
Add placeholderColor property to easily set the color, and then observer changing of .placeholder to apply the color to it (with use of .attributedPlaceholder property)
var placeholderColor: UIColor = .lightGray
override var placeholder: String? {
didSet {
let attributes = [ NSAttributedString.Key.foregroundColor: placeholderColor ]
attributedPlaceholder = NSAttributedString(string: placeholder ?? "", attributes: attributes)
}
}
You do need to set the placeholder text programatically for the color to apply.
set Textfield placeholder
let leftBarButtonItem = UIBarButtonItem.init(image: UIImage(named:"ic_nav-bar_back.png"), landscapeImagePhone: nil, style: .plain, target: viewController, action: #selector(viewController.buttonClick(_:)))
leftBarButtonItem.imageInsets = UIEdgeInsets(top: 0, left: -15, bottom: 0, right: 0)
leftBarButtonItem.tintColor = UIColor(hex: 0xED6E19)
viewController.navigationItem.setLeftBarButton(leftBarButtonItem, animated: true)
A simple solution is override placeholder property in an UITextField extension. It will update color of placeholder whole project. You don't need to update your code in many places.
extension UITextField {
var placeholder: String? {
get {
attributedPlaceholder?.string
}
set {
guard let newValue = newValue else {
attributedPlaceholder = nil
return
}
let attributes: [NSAttributedString.Key: Any] = [.foregroundColor: Color.textFieldPlaceholder.color]
let attributedText = NSAttributedString(string: newValue, attributes: attributes)
attributedPlaceholder = attributedText
}
}
}
open your identity inspector by selecting text field and then put " placeholderLabel.textColor " in key path by pressing + button .
Give the type " Color " and in value select desired RGB color.
Swift 5
textfiled.attributedPlaceholder = NSAttributedString(string:NSLocalizedString("Input Group Name", comment: "Input Group Name"), attributes: [NSAttributedString.Key.foregroundColor: yourColor.withAlphaComponent(0.5)])

Change of UITextField placeholder color

How to dynamically change placeholder color of the UITextField?
This is always the same system color.
No option in xib editor.
From Docs
#property(nonatomic, copy) NSAttributedString *attributedPlaceholder
This property is nil by default. If set, the placeholder string is
drawn using a 70% grey color and the remaining style information
(except the text color) of the attributed string. Assigning a new
value to this property also replaces the value of the placeholder
property with the same string data, albeit without any formatting
information. Assigning a new value to this property does not affect
any other style-related properties of the text field.
Objective-C
NSAttributedString *str = [[NSAttributedString alloc] initWithString:#"Some Text" attributes:#{ NSForegroundColorAttributeName : [UIColor redColor] }];
self.myTextField.attributedPlaceholder = str;
Swift
let str = NSAttributedString(string: "Text", attributes: [NSForegroundColorAttributeName:UIColor.redColor()])
myTextField.attributedPlaceholder = str
Swift 4
let str = NSAttributedString(string: "Text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])
myTextField.attributedPlaceholder = str
_placeholderLabel.textColor
In swift
myTextField.attributedPlaceholder =
NSAttributedString(string: "placeholder", attributes:[NSForegroundColorAttributeName : UIColor.redColor()])
Objective-C
UIColor *color = [UIColor grayColor];
nameText.attributedPlaceholder =
[[NSAttributedString alloc]
initWithString:#"Full Name"
attributes:#{NSForegroundColorAttributeName:color}];
P.S Copied 3 different answers from Stackoverflow.
Use below code
[YourtextField setValue:[UIColor colorWithRed:97.0/255.0 green:1.0/255.0 blue:17.0/255.0 alpha:1.0] forKeyPath:#"_placeholderLabel.textColor"];
First add this extension
extension UITextField{
#IBInspectable var placeHolderTextColor: UIColor? {
set {
let placeholderText = self.placeholder != nil ? self.placeholder! : ""
attributedPlaceholder = NSAttributedString(string:placeholderText, attributes:[NSForegroundColorAttributeName: newValue!])
}
get{
return self.placeHolderTextColor
}
}
}
Then you can change placeholder text color via storyboard or by just setting it like this :
textfield.placeHolderTextColor = UIColor.red
I use this in SWIFT:
myTextField.attributedPlaceholder =
NSAttributedString(string: "placeholder", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
It seems that this works for others... I have no idea why it haven't worked for me before... maybe some project settings. Thanks for the comments. Currently I have no way how to test it again.
Obsolete:
But I don't know why, text is applied correctly, but placeholder color remains same (black/gray).
--iOS8
Try this:
NSAttributedString *strUser = [[NSAttributedString alloc] initWithString:#"Username" attributes:#{ NSForegroundColorAttributeName : [UIColor whiteColor] }];
NSAttributedString *strPassword = [[NSAttributedString alloc] initWithString:#"Password" attributes:#{ NSForegroundColorAttributeName : [UIColor whiteColor] }];
self.username.attributedPlaceholder = strUser;
self.password.attributedPlaceholder = strPassword;
You can use the following code
[txtUsername setValue:[UIColor darkGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
This solution works without any subclassing and without any private ivars:
#IBOutlet weak var emailTextField: UITextField! {
didSet {
if emailTextField != nil {
let placeholderText = NSLocalizedString("Tap here to enter", comment: "Tap here to enter")
let placeholderString = NSAttributedString(string: placeholderText, attributes: [NSForegroundColorAttributeName: UIColor(white: 0.66, alpha: 1.0)])
emailTextField.attributedPlaceholder = placeholderString
}
}
}
Try This.
UIColor *color = [UIColor redColor];
self.txtUsername.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Your Placeholder Text" attributes:#{NSForegroundColorAttributeName:color}];
Swift 4
let placeholderColor = UIColor.red
self.passwordTextField?.attributedPlaceholder =
NSAttributedString(string:"placeholderText", attributes:
[NSAttributedStringKey.foregroundColor : placeholderColor])
#DogCoffee's answer in Swift would be
let placeholderAttrs = [ NSForegroundColorAttributeName : UIColor.redColor()]
let placeholder = NSAttributedString(string: "Some text", attributes: placeholderAttrs)
textField.attributedPlaceholder = placeholder
This is an improved version of the extension provided by #Medin Piranej above (good idea by the way!). This version avoids an endless cycle if you try to get the placeHolderTextColor and prevents crashes if the color set is nil.
public extension UITextField {
#IBInspectable public var placeholderColor: UIColor? {
get {
if let attributedPlaceholder = attributedPlaceholder, attributedPlaceholder.length > 0 {
var attributes = attributedPlaceholder.attributes(at: 0,
longestEffectiveRange: nil,
in: NSRange(location: 0, length: attributedPlaceholder.length))
return attributes[NSForegroundColorAttributeName] as? UIColor
}
return nil
}
set {
if let placeholderColor = newValue {
attributedPlaceholder = NSAttributedString(string: placeholder ?? "",
attributes:[NSForegroundColorAttributeName: placeholderColor])
} else {
// The placeholder string is drawn using a system-defined color.
attributedPlaceholder = NSAttributedString(string: placeholder ?? "")
}
}
}
}
for swift 3 ,we can use this code for change the placeholder text color for UITextfield
let placeholderColor = UIColor.red
mytextField.attributedPlaceholder = NSAttributedString(string: mytextField.placeholder, attributes: [NSForegroundColorAttributeName : placeholderColor])

iOS NSAttributedString on UIButton

I'm using iOS 6, so attributed strings should be easy to use, right? Well... not so much.
What I want to do:
Using a custom subclass of UIButton (it doesn't do anything custom to titleLabel), I'd like to have a multi-line, attributed title that is:
All caps (I realize that's not part of the attributes) on the first line
Bolded on the first line
Underlined on the first line
"Normal" weight on the second line
Non-underlined on the second line
Centered on both lines
I've been able to get #'s 1 through 5 so far (at least, I thought I did, but current testing is yielding errors with multi-line text), but when I tried to do something (anything!) to get the text to be centered, my app keeps crashing. When I try to get all 6 items working (through various methods), I get the following crash/error:
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason:
'NSAttributedString invalid for autoresizing,
it must have a single spanning paragraph style
(or none) with a non-wrapping lineBreakMode.'
Based on what I've tried, it appears that I can have one of the following options, but not both:
A multi-line, centered label
An attributed label
I can live with one or the other if I must, but I can't believe that I can't have what seems to be a fairly straightforward concept.
Can someone please tell me what I've got wrong?
Here's the last iteration of the code I'm trying:
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSTextAlignmentCenter];
[style setLineBreakMode:NSLineBreakByWordWrapping];
UIFont *font1 = [UIFont fontWithName:#"HelveticaNeue-Medium" size:20.0f];
UIFont *font2 = [UIFont fontWithName:#"HelveticaNeue-Light" size:20.0f];
NSDictionary *dict1 = #{NSUnderlineStyleAttributeName:#(NSUnderlineStyleSingle),
NSFontAttributeName:font1};
NSDictionary *dict2 = #{NSUnderlineStyleAttributeName:#(NSUnderlineStyleNone),
NSFontAttributeName:font2};
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:#"LINE 1\n" attributes:dict1]];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:#"line 2" attributes:dict2]];
[[self buttonToStyle] setAttributedTitle:attString forState:UIControlStateNormal];
[[[self buttonToStyle] titleLabel] setNumberOfLines:0];
[[[self buttonToStyle] titleLabel] setLineBreakMode:NSLineBreakByWordWrapping];
It looks to me like you forgot in your code to use the "style" object that you set up.. you just instantiated it. You should modify your code to look like this:
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSTextAlignmentCenter];
[style setLineBreakMode:NSLineBreakByWordWrapping];
UIFont *font1 = [UIFont fontWithName:#"HelveticaNeue-Medium" size:20.0f];
UIFont *font2 = [UIFont fontWithName:#"HelveticaNeue-Light" size:20.0f];
NSDictionary *dict1 = #{NSUnderlineStyleAttributeName:#(NSUnderlineStyleSingle),
NSFontAttributeName:font1,
NSParagraphStyleAttributeName:style}; // Added line
NSDictionary *dict2 = #{NSUnderlineStyleAttributeName:#(NSUnderlineStyleNone),
NSFontAttributeName:font2,
NSParagraphStyleAttributeName:style}; // Added line
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:#"LINE 1\n" attributes:dict1]];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:#"line 2" attributes:dict2]];
[self.resolveButton setAttributedTitle:attString forState:UIControlStateNormal];
[[self.resolveButton titleLabel] setNumberOfLines:0];
[[self.resolveButton titleLabel] setLineBreakMode:NSLineBreakByWordWrapping];
Note that I only added the lines that define the NSParagraphStyleAttributeName.. everything else is the same.. and this is what I get for the button:
And here it is in Swift 3.0
let style = NSMutableParagraphStyle()
style.alignment = .center
style.lineBreakMode = .byWordWrapping
guard
let font1 = UIFont(name: "HelveticaNeue-Medium", size: 20),
let font2 = UIFont(name: "HelveticaNeue-Light", size: 20) else { return }
let dict1:[String:Any] = [
NSUnderlineStyleAttributeName:NSUnderlineStyle.styleSingle.rawValue,
NSFontAttributeName:font1,
NSParagraphStyleAttributeName:style
]
let dict2:[String:Any] = [
NSUnderlineStyleAttributeName:NSUnderlineStyle.styleNone.rawValue,
NSFontAttributeName:font2,
NSParagraphStyleAttributeName:style
]
let attString = NSMutableAttributedString()
attString.append(NSAttributedString(string: "LINE 1", attributes: dict1))
attString.append(NSAttributedString(string: "line 2", attributes: dict2))
button.setAttributedTitle(attString, for: .normal)
button.titleLabel?.numberOfLines = 0
button.titleLabel?.lineBreakMode = .byWordWrapping
With Swift 5.1 and iOS 13.1, you can use the UIButton subclass implementation below in order to solve your problem:
import UIKit
class CustomButton: UIButton {
required init(title: String, subtitle: String) {
super.init(frame: CGRect.zero)
let style = NSMutableParagraphStyle()
style.alignment = NSTextAlignment.center
style.lineBreakMode = NSLineBreakMode.byWordWrapping
let titleAttributes: [NSAttributedString.Key : Any] = [
NSAttributedString.Key.foregroundColor: UIColor.label,
NSAttributedString.Key.underlineStyle : NSUnderlineStyle.single.rawValue,
NSAttributedString.Key.font : UIFont.preferredFont(forTextStyle: UIFont.TextStyle.largeTitle),
NSAttributedString.Key.paragraphStyle : style
]
let subtitleAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.label,
NSAttributedString.Key.font : UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body),
NSAttributedString.Key.paragraphStyle : style
]
let attributedString = NSMutableAttributedString(string: title, attributes: titleAttributes)
attributedString.append(NSAttributedString(string: "\n"))
attributedString.append(NSAttributedString(string: subtitle, attributes: subtitleAttributes))
setAttributedTitle(attributedString, for: UIControl.State.normal)
titleLabel?.numberOfLines = 0
titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Usage:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = CustomButton(title: "Title", subtitle: "Subtitle")
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(button)
let horizontalConstraint = button.centerXAnchor.constraint(equalTo: view.centerXAnchor)
let verticalConstraint = button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint])
}
}
As an alternative if you really need a button of type system, you may use the following code:
import UIKit
extension UIButton {
static func customSystemButton(title: String, subtitle: String) -> UIButton {
let style = NSMutableParagraphStyle()
style.alignment = NSTextAlignment.center
style.lineBreakMode = NSLineBreakMode.byWordWrapping
let titleAttributes: [NSAttributedString.Key : Any] = [
NSAttributedString.Key.underlineStyle : NSUnderlineStyle.single.rawValue,
NSAttributedString.Key.font : UIFont.preferredFont(forTextStyle: UIFont.TextStyle.largeTitle),
NSAttributedString.Key.paragraphStyle : style
]
let subtitleAttributes = [
NSAttributedString.Key.font : UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body),
NSAttributedString.Key.paragraphStyle : style
]
let attributedString = NSMutableAttributedString(string: title, attributes: titleAttributes)
attributedString.append(NSAttributedString(string: "\n"))
attributedString.append(NSAttributedString(string: subtitle, attributes: subtitleAttributes))
let button = UIButton(type: UIButton.ButtonType.system)
button.setAttributedTitle(attributedString, for: UIControl.State.normal)
button.titleLabel?.numberOfLines = 0
button.titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
return button
}
}
Usage:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton.customSystemButton(title: "Title", subtitle: "Subtitle")
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(button)
let horizontalConstraint = button.centerXAnchor.constraint(equalTo: view.centerXAnchor)
let verticalConstraint = button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint])
}
}
The two screen shots below show the result display for the UIButton subclass (on the left) and for the system type button (on the right):
Two line UIButton with NSAttributedString title in Swift 5.1:
func customizeSubscribeButton() {
subscribeButton.titleLabel?.numberOfLines = 2
let title = NSMutableAttributedString()
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let part1 = NSAttributedString(string: "SUBSCRIBE FOR 12 MONTH\n",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 24, weight: .semibold),
NSAttributedString.Key.paragraphStyle: paragraphStyle])
let part2 = NSAttributedString(string: "999.00 RUB ECONOMY 85%",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12, weight: .light),
NSAttributedString.Key.paragraphStyle: paragraphStyle])
title.append(part1)
title.append(part2)
subscribeButton.setAttributedTitle(title, for: .normal)
}

Resources