Forcing UIButton to have just one title line - swift

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.

Related

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!

How to set text position to bottom in textField

I created a textField through code using the following code
UITextField *txtObj=[UITextField alloc]initWithFrame:CGRectMake(88,100,80,33)];
I am getting the textfield as i planned, but the text we type in that text field seems to appear in the top area of text but not in bottom.I tried to align it but i got tired. Can anyone tell me what's the solution
it should be:
txtObj.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
This property is inherited from the UIControl class. The default is: UIControlContentVerticalAlignmentTop , that's why you get the text on top.
txtObj.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
If you want the text to be in the center you can use ,
txtObj.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
Swift 4,5 solution:
txtObj.contentVerticalAlignment = .bottom

Gtk Button inner-border

I add a button to HBox, with expand equal to False, but I want the button to have more spacing between its label and border. I assume it is "inner-border" property, but it is read-only. How can I set it to e.g. 4px?
gtk.Label is a subclass of gtk.Misc which has the method set_padding. If you get the label out of the gtk.Button then you can just call set_padding on it.
You could do something like:
label = gtk.Label("Hello World")
button = gtk.Button()
/* Add 10 pixels border around the label */
label.set_padding(10, 10)
/* Add the label to the button */
button.add(label)
/* Show the label as the button will assume it is already shown */
label.show()
Wrong answer:
What you're looking for is called "padding". When you add your button to the container, for example by calling gtk.Box.pack_start, just set the padding parameter to a positive integer.
Update:
Seems I misread the question. In that case, my guess is that you're supposed to use gtk_widget_modify_style, as inner-border is a style property. You'll first get the style modifier you need by calling gtk_widget_get_modifier_style. You'll then be able to modify the style only for that button using the ressource styles matching rules.
you can use "inner-border" style property of gtk button.
here, small code snippets
In gtkrc file:
style "button_style"
{
GtkButton::inner-border = {10,10,10,10}
}
class "GtkButton" style "button_style"
In .py file:
gtk.rc_parse(rc_file_path + rc_file)
[Edit]
In gtkrc file:
style "button_style"
{
GtkButton::inner-border = {10,10,10,10}
}
widget "*.StyleButton" style "button_style" # apply style for specific name of widget
In .py file:
gtk.rc_parse(rc_file_path + rc_file)
#set name of button
self.style_button.set_name('StyleButton')
hope, it would be helpful.
I sometimes just add spaces in the label !
gtk.Button(" Label ")
to get some spacing.
Hope this could help you.