UITextField Placeholder and Text Size should be separate - Swift - ios

My requirement is such that when placeholder of textfield is visible then its color should be gray and font size 10, but when user starts typing into the textfield, its color should be black and font size 14. I have tried this:
textField.attributedPlaceholder = NSAttributedString(string: placeholderText,
attributes: [NSAttributedString.Key.foregroundColor: Color.iPhoneGrayColor, NSAttributedString.Key.font: UIFont(name: "SourceSansPro-Regular", size: 10)!])
textField.textColor = UIColor.black
textField.font = UIFont(name: "SourceSansPro-Regular", size: 14)!
But, my placeholder font size is getting overridden by textfield.font, so I am unable to get placeholder of size 10. Where am I going wrong? Tried this for couple of hours now. Any help will be appreciated.

Simply set the placeholder after setting the font since setting the font also applies to the placeholder (see https://developer.apple.com/documentation/uikit/uitextfield/1619604-font):
textField.font = ...
textField.textColor = ...
textField.attributedPlaceholder = ...

Try this code into your viewDidLoad() method:
textField.attributedPlaceholder = NSAttributedString(string: placeholderText, attributes: [NSAttributedString.Key.foregroundColor: Color.iPhoneGrayColor, NSAttributedString.Key.font: UIFont(name: "SourceSansPro-Regular", size: 10)!])
textField.textColor = UIColor.black
textField.font = UIFont(name: "SourceSansPro-Regular", size: 14)!

Just try this code, take the UITextFieldEditingChanged action outlet and use as below.
#IBAction func nameTextFieldEditingChanged(_ sender: UITextField) {
if sender.text?.isEmpty ?? true {
//placeholder text size set here
textField.font = UIFont(name: "SourceSansPro-Regular", size: 10)!
} else {
// When user starting typing
textField.textColor = UIColor.black
textField.font = UIFont(name: "SourceSansPro-Regular", size: 14)!
}
}
If any doubt please comment ].

Just try this code may help you for further:
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

Related

How to change MDCTextField placeholder text color?

I'm currently using it with MDCTextInputControllerOutlined.
I already tried
self.emailTextField.attributedPlaceholder = NSAttributedString(string: "Email", attributes: [.font: UIFont.ralewayFont(ofSize: 18.0, weight: .semibold), .foregroundColor: UIColor.white])
but not working
Found it!
set UIColor to inlinePlaceholderColor for MDCTextInputControllerOutlined
let emailTextField = MDCTextField()
let emailTextFieldController = MDCTextInputControllerOutlined(textInput: emailTextField)
emailTextFieldController.inlinePlaceholderColor = .white

NSAttributedString on UIButton

let strNo = "2222555" // size 18, this should be bold
let remainingStr = "Call to" + "\(strNo)" + "Number"
Now, in my UIButton, say button, How to set this title "Call to 2222555 number" And i need to change the size according to device, so I have to do it by coding.
Update
I need like following image.
And need to change the size of the title by coding. Above screenshot is from iPhone 7, In iPhone 5 it become bigger and in iPad it become smaller, so i have to set the size of the title according to requirement.
Any help will be appreciable.
Thanks
I found the Answer by struggling few days. And it is quite easy.
guard
let font1 = UIFont(name: "HelveticaNeue-Bold", size: 22),
let font2 = UIFont(name: "HelveticaNeue-Medium", size: 22) else { return }
let dict1:[String:Any] = [
// NSUnderlineStyleAttributeName:NSUnderlineStyle.styleSingle.rawValue,
NSFontAttributeName:font2,
NSParagraphStyleAttributeName:style,
NSForegroundColorAttributeName: UIColor.white
]
let dict2:[String:Any] = [
// NSUnderlineStyleAttributeName:NSUnderlineStyle.styleNone.rawValue,
NSFontAttributeName:font1,
NSParagraphStyleAttributeName:style,
NSForegroundColorAttributeName: UIColor.white,
// NSFontAttributeName: UIFont.boldSystemFont(ofSize: 20)
]
let dict3:[String:Any] = [
// NSUnderlineStyleAttributeName:NSUnderlineStyle.styleNone.rawValue,
NSFontAttributeName:font2,
NSParagraphStyleAttributeName:style,
NSForegroundColorAttributeName: UIColor.white
]
let dict4:[String:Any] = [
// NSUnderlineStyleAttributeName:NSUnderlineStyle.styleNone.rawValue,
NSFontAttributeName:font1,
NSParagraphStyleAttributeName:style,
NSForegroundColorAttributeName: UIColor.white
]
let attString = NSMutableAttributedString()
attString.append(NSAttributedString(string: "Call to", attributes: dict1))
attString.append(NSAttributedString(string: " 2222555 ", attributes: dict2))
attString.append(NSAttributedString(string: " To Book Your Skip ", attributes: dict3))
attString.append(NSAttributedString(string: "NOW", attributes: dict4))
btnCall.setAttributedTitle(attString, for: .normal)
And its Working perfectly as expected.

How to center vertically attributed text (custom placeholder font) in UITextField?

I am using attributed text property to set custom placeholder font in UITExtfield. But my placeholder text is not vertically centered.Here is preview
let font = UIFont(name: Font.myFont.name, size: 15)!
let attributes = [
NSForegroundColorAttributeName: UIColor.cadetGrey,
NSFontAttributeName: font
]
exampleTextField.attributedPlaceholder = NSAttributedString(string: "Example Placeholder",
attributes: attributes)
I tried to add
let centeredParagraphStyle = NSMutableParagraphStyle()
centeredParagraphStyle.alignment = .center
to attribute still no luck. I also tried to use SizeToFit() on my UITextBox but no luck at all
This worked for me in Swift 5:
let unitText = NSAttributedString(string: "\(unit)", attributes: [NSAttributedString.Key.foregroundColor: MyColors.doveGray, NSAttributedString.Key.baselineOffset:2])
Try like this :
let centeredParagraphStyle = NSMutableParagraphStyle()
centeredParagraphStyle.alignment = .center
let attributes: [String : Any] = [NSParagraphStyleAttributeName: centeredParagraphStyle]
This is will set both placeholder and text input by user in center.
exampleTextField.textAlignment = .center
To center both vertically and horizontally.
exampleTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
exampleTextField.textAlignment = .center
OR
This is will only set placeholder in center whereas the text input by user will remain at it's default position i.e left align
let font = UIFont(name: Font.myFont.name, size: 15)!
let centeredParagraphStyle = NSMutableParagraphStyle()
centeredParagraphStyle.alignment = .center
// add attribute of NSMutableParagraphStyle
let attributes = [
NSForegroundColorAttributeName: UIColor.gray,
NSFontAttributeName: font,
NSParagraphStyleAttributeName: centeredParagraphStyle
]
exampleTextField.attributedPlaceholder = NSAttributedString(string: "Example Placeholder",
attributes: attributes)

How can I set the attributed text while having no placeholder text?

Code that worked in Xcode 6.2 no longer works and I'm having trouble figuring out how to fix it. String attributes are now ignored unless I set default text. Here's what I've got right now:
let font = UIFont(name: "Arial-BoldMT", size: 42.0)!
let textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle
textStyle.alignment = NSTextAlignment.Center
let textColor = UIColor.whiteColor()
var shadow = NSShadow()
shadow.shadowColor = UIColor.blackColor()
shadow.shadowOffset = CGSizeMake(2.0,2.0)
var attr = [String:NSObject]()
attr = [
NSFontAttributeName: font,
NSForegroundColorAttributeName: textColor,
NSParagraphStyleAttributeName: textStyle,
NSShadowAttributeName: shadow
]
let placeholderText = NSAttributedString(string: "", attributes: attr)
textView.attributedText = placeholderText
If you change the placeHolderText to NSAttributedString(string: " ", attributes: attr) you can then change the UITextView's text by changing its text property and the new text will have all the same attributes.
I hope that answers your question.

Could not find an overload for “init” that accepts the supplied arguments SWIFT

I used this code
self.navigationController?.navigationBar.titleTextAttributes =
[NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 20),
NSForegroundColorAttributeName: UIColor.whiteColor()]
and I'm getting error "Could not find an overload for “init” that accepts the supplied arguments"
UIFont(name:size:) is now a failable initializer -- it will return nil if it can't find that font and crash your app if you unwrap the return value. Use this code to safely get the font and use it:
if let font = UIFont(name: "HelveticaNeue-Light", size: 20) {
self.navigationController?.navigationBar.titleTextAttributes =
[NSFontAttributeName: font,
NSForegroundColorAttributeName: UIColor.whiteColor()]
}
Use this
self.navigationController?.navigationBar.titleTextAttributes =
[NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 20)!,
NSForegroundColorAttributeName: UIColor.whiteColor()!]
or this one
if let font = UIFont(name:"HelveticaNeue-Light", size: 20.0) {
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: font]
}
Another approach is to build up a dictionary before setting titleTextAttributes. This just avoids you the else(s), which would be more beneficial in cases where you wanted to set further parameters also using failable initialisers. Eg:
var attributes : [NSObject : AnyObject] = [NSForegroundColorAttributeName : UIColor.whiteColor()]
if let font = UIFont(name: "Helvetica", size: 20) {
attributes[NSFontAttributeName] = font
}
if let someData = NSData(contentsOfFile: "dataPath") {
attributes["imageData"] = someData
}
self.myObject.attributes = attributes

Resources