tabBarController navigationItem.title color - ios

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

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)
}

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

Font of barButtonItem changes when highlighted swift 4.2

I have a barbuttonItem here:
let doneBarButtonItem: UIBarButtonItem? = {
let barButtonItem = UIBarButtonItem()
barButtonItem.setTitleTextAttributes([
NSAttributedString.Key.font : UIFont(name: "Avenir-Heavy", size: 12)!,
NSAttributedString.Key.foregroundColor : UIColor.black,
], for: .normal)
barButtonItem.title = "DONE"
return barButtonItem
}()
Yet when i press on it, it changes to a different font. What is the property to change the font when highlighted? Thank you.
barButtonItem.setTitleTextAttributes([
NSAttributedString.Key.font : UIFont(name: "Avenir-Heavy", size: 12)!,
NSAttributedString.Key.foregroundColor : UIColor.black,
], for: .highlighted) // This line

Button title font

btn.titleLabel?.font = UIFont(name:"Times New Roman", size: 20)
I know this is the way to change font size of button. But it does not work for me.
Anybody know why it's not working for me ?
Using setAttributedTitle on button instance fixes the problem
let title = "Some String"
let attributedTitle = NSAttributedString.init(string: title, attributes: [.font: UIFont(name:"Times New Roman", size: 20)!])
UIButton().setAttributedTitle(attributedTitle, for: .normal)

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.

Resources