How to change MDCTextField placeholder text color? - ios

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

Related

UIButton Change Title Color Partially

enter image description here
I want to make button like this picture.
In order to implement it like a picture, the color of the button title needs to be partially changed, but I don't know what to do.
Here is my code.
private let signUpButton = UIButton().then {
$0.setTitleColor(.black, for: .normal)
$0.titleLabel?.font = .boldSystemFont(ofSize: 12)
}
Use NSMutableAttributedString for this purpose
private let signUpButton = UIButton().then {
// Creating gray text part
let grayButtonText = NSAttributedString(string: "FirstPart", attributes: [.font: UIFont.systemFont(ofSize: 12), .foregroundColor: UIColor.gray ])
// Creating black text part
let blackButtonText = NSAttributedString(string: "SecondPart", attributes: [.font: UIFont.systemFont(ofSize: 12), .foregroundColor: UIColor.black ])
// Merging two parts together
let buttonTitle = NSMutableAttributedString(attributedString: grayButtonText)
buttonTitle.append(blackButtonText)
// Set it as a title for ".normal" state
$0.setAttributedTitle(buttonTitle, for: .normal)
}

UITextField Placeholder and Text Size should be separate - Swift

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

tabBarController navigationItem.title color

I am setting the title as follows, but I want to change the color of the text as well, but there is no textattributes, I wonder how it can be done ?
self.tabBarController?.navigationItem.title = "Settings"
I tried the following as well, but it did not even show the label.
let navLabel = UILabel()
let navTitle = NSMutableAttributedString(string: "Settings", attributes:[
NSAttributedString.Key.foregroundColor: UIColor.blue,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17.0, weight: UIFont.Weight.light)])
navLabel.attributedText = navTitle
self.tabBarController?.navigationItem.titleView = navTitle
try this code, I hope it helps you.
Build Settings\Swift Language Version: 4.1
General\Deployment Target: 10.3
let attrsNormal = [NSAttributedStringKey.foregroundColor : UIColor.black,
NSAttributedStringKey.font : UIFont(name: "Arial", size: 14)!]
UITabBarItem.appearance().setTitleTextAttributes(attrsNormal,
for: UIControlState.normal)
The size of label is right ? Call fitThatSize

How to change UIButton attributed text colour programatically?

I have a subclass (KeyButton) of UIButton where I am applying certain styles for the button. The following code adds the attributed text for buttons in the ViewController.
func superScriptText(text: String, button: KeyButton, fontSize: Int) {
let font:UIFont? = UIFont.systemFont(ofSize: 22, weight: UIFont.Weight.light)
let fontSuper:UIFont? = UIFont(name: "Helvetica", size:CGFloat(fontSize))
let attString:NSMutableAttributedString = NSMutableAttributedString(string: text, attributes: [.font:font!])
attString.setAttributes([.font:fontSuper!,.baselineOffset:15], range: NSRange(location:1,length:1))
button.setAttributedTitle(attString, for: .normal)
}
How can I change the color of attributed text for the button in the class?
Just change:
let attString:NSMutableAttributedString =
NSMutableAttributedString(string: text, attributes: [.font:font!])
to:
let attString:NSMutableAttributedString =
NSMutableAttributedString(string: text, attributes: [.font:font!, .foregroundColor: UIColor.red])
NSAttributedStringKey.foregroundColor is used for the text color, see more options in docs.
You have to add the .foregroundColor key with a UIColor object as the value to the NSAttributedStrings attributes dictionary.
Example (assuming you have added a custom button in storyboard):
class CustomButton: UIButton {
override func awakeFromNib() {
super.awakeFromNib()
let text = "CustomButton"
let font = UIFont.systemFont(ofSize: UIFont.buttonFontSize)
let textColor = UIColor.orange
let attributes: [NSAttributedStringKey: Any] = [
.font: font,
.foregroundColor: textColor
]
let attributedText = NSAttributedString(string: text, attributes: attributes)
self.setAttributedTitle(attributedText, for: .normal)
}
}
I couldn't do this through UIButton subclass. I created the subclass of NSAttributtedText and add the following method:
var textColor: UIColor?
func setSuperScript(text: String, button: KeyButton, fontSize: Int) {
let font:UIFont? = UIFont.systemFont(ofSize: 22, weight: UIFont.Weight.light)
let fontSuper:UIFont? = UIFont(name: "Helvetica", size:CGFloat(fontSize))
let attString:NSMutableAttributedString = NSMutableAttributedString(string: text, attributes: [.font:font!, .foregroundColor: textColor!])
attString.setAttributes([.font:fontSuper!,.baselineOffset:15, .foregroundColor: textColor!,], range: NSRange(location:1,length:1))
button.setAttributedTitle(attString, for: .normal)
}
I am setting the color based on the logic I have and then set the attributed string color accordingly.

foregroundColor for Textfield in swift

Dears,
I'm learning swift and I'm trying to color a textfield text, the issue now is that the color is not applied to the text. please find the code next:
let textStyle:[String:Any] = [
NSAttributedStringKey.strokeColor.rawValue: UIColor.black,
NSAttributedStringKey.foregroundColor.rawValue: UIColor.white,
NSAttributedStringKey.font.rawValue: UIFont (name: "Impact", size: 50)!,
NSAttributedStringKey.strokeWidth.rawValue: 3.5
]
Thanks
The key appears to be the .strokeWidth... If you want stroke and fill, the stroke width needs to be a negative value.
Give this a try:
let textStyle = [
NSAttributedStringKey.strokeColor: UIColor.black,
NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.font: UIFont(name: "Impact", size: 50)!,
NSAttributedStringKey.strokeWidth: -3.5
]
can you try this...
myTextField.attributedText = NSAttributedString(string: "text",
attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])

Resources