Button text shrinks when the text is changed - swift

Ones I change the text in button it's getting stink to font size of ~18 even though initial font size is 55. Button is not custom. It has System font.
button.setTitle("Some button text", for: .normal)
I tried manually change font size in code but it doesn't help
button.titleLabel?.font = UIFont.systemFont(ofSize: 55)
I also tried to change the name through configuration function, but there is no font size and it shrinks anyway.
`
button.configuration = paperButtonConfiguration()
func buttonConfiguration() -> UIButton.Configuration {
var config: UIButton.Configuration = .plain()
config.title = "Some button text"
//Should probably be something like this but it doesn't work
//config.font = UIFont.systemFont(ofSize: 55)
return config
}
`
While I was writing this question I kind of solve this, but not completely. If I change the button style from "plain" to "default" it works, but some weird animation of fading and appearance occurs.
How could I do this with "plain"?

UIButton has four type are Plain,Grain,Tinted,Filled .When you create a button in storyboard , button type automatically set with Plain that means new UIButton configurations is on. If you want to old behaviour , you must set style plain to default.
Or , If you want one of the style above . You need to set font like
button.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
var outgoing = incoming
outgoing.font = UIFont.systemFont(ofSize: 50)
return outgoing
}

Related

Forcing UIButton to have just one title line

I'm trying to force the UIButton to accept just one line of text, and if the title is too long I would like to have "..." at the end or in the middle of the title. but when I try out the code below, unfortunately, it doesn't work, it still gives multiline title text.
titleButton.titleLabel!.lineBreakMode = .byWordWrapping
titleButton.titleLabel!.numberOfLines = 1
titleButton.titleLabel!.textAlignment = .center
The main problem was the button style which was on the "plain", the magic happened after changing it to default!
It sounds like you want lineBreakMode.byTruncatingTail or .byTruncatingMiddle.

Localize button Swift

For example I've got an app which has a textLabel and a button which are firstly set as:
mainLabel.attributedText = "labelNewText".uppercased()
mainButton.titleLabel?.attributedText = "buttonNewText".uppercased()
Then I've created a Localization file, where I set a some values for a German language:
"labelNewText" = "Etikette";
"buttonNewText" = "Taste";
And rewrote set ups for label and button like:
mainLabel.attributedText = "\(NSLocalizedString("labelNewText", comment: ""))".uppercased()
mainButton.titleLabel?.attributedText = "\(NSLocalizedString("buttonNewText", comment: ""))".uppercased()
Though, right after after I change my phone language setting to German, the translation only works for a Label but is not working for button. What am I doing wrong and how to localise button titleLabel?
You have to use UIButton's setAttributedTitle:forState: instead of trying to manipulate the text label itself. So for example:
mainButton.setAttributedTitle(myAttributeString, forState: .normal)

Cocoa/Swift: NSStatusBarItem's text gets trimmed when using a picture

When creating an NSStatusBar.systemStatusBar related application for MacOS, I stumbled upon a problem that seemed strange to begin with. The problem is: when using an image and a text for the NSStatusBar, the text was being clipped unless you manually specified a sufficient length, which would be hardcoded and causes problems with alternating lengths. How can this be solved?
// The -1 is supposed to mean "variable length"
let myStatusItem = NSStatusBar.systemStatusBar().statusItemWithLength(CGFloat(-1))
myStatusItem.image = NSImage(named: "customImage")
myStatusItem.title = "Some special information"
This would be the normal case where this problem will occur.
After playing around with some ridiculous variations, I realized that the problem can be fixed when using BOTH .title and .attributedTitle parameters of the NSStatusBar item.
Also make sure that you declare the .image BEFORE you declare the titles. If you want to use the .attributedTitle, define it after .title - if you want to use the plain .title, just define it after the .attributedTitle.
// The -1 is supposed to mean "variable length"
let myStatusItem = NSStatusBar.systemStatusBar().statusItemWithLength(CGFloat(-1))
myStatusItem.image = NSImage(named: "customImage")
myStatusItem.title = "Some special information"
myStatusItem.attributedTitle = NSAttributedString(string: "Some special information")

Add Placeholder to UITextField, how to set the placeholder text programmatically in swift?

I'm pulling out a phone number from a database, and when the user begins editing in the text field to change that phone number I'd like to use the number I currently have in the database as the placeholder. Since this information changes with each user, how can I set it programmatically in swift?
You need to get the phone number from your database first (convert them to String), then you set placeholder of your textField to that String, like so
textField.placeholder = phoneNumberString
Swift 3
If your textField has text, you need to first set text property to nil, then set placeholder text:
textField.text = nil
textField.placeholder = "My Placeholder Text"
Important to note for anyone else reading this, setting placeholder text in the main.storyboard seems to nullify this solution, so I had to first clear out my placeholders in the storyboard before implementing this. Once that was done #Khuong and #Himanshu's answer worked perfectly.
Apply this line of code in to View Did Load
new_Password.attributedPlaceholder =
NSAttributedString(string: " New Password", attributes: [NSForegroundColorAttributeName : UIColor.white]) // new_Password : our text feild name
Fetch your desired data from your database (Core data) and after converting it into string format... say phoneString
use this line to set this string as a placeholder text
phoneTextField.placeholder = phoneString
Objective-C code:
[usernameText setPlaceholder:#"My Placeholder Text"];
Just a note to say if you have changed your textfield's text and background colors programmatically, you can't do that with the placeholder text's colors and must set up an Attributed Placeholder instead. This is a problem if your device is in put in "dark mode" and are trying to make a non-dark mode screen by hand - you might not be able to see the placeholder!

Changing colours for button which have got a focus

I use DotNetBar controls. I can change colours for buttons with:
Office2007ColorTable table = ((Office2007Renderer)GlobalManager.Renderer).ColorTable;
Office2007ButtonItemColorTable bt = table.ButtonItemColors[6];
bt.Default.Background = new LinearGradientColorTable(Color.White);
bt.MouseOver.Background = new LinearGradientColorTable(Color.Green);
Unfortunate I cannot find how I can change colour for a focused button.
Is it possible?