How to change title of UILabel when they create themselve in Swift? - ios

I override draw func in UIButton class and i need to set unique label for this button when he create themselve.
When i use super.titleLabel?.text = "But Me" - this is working, but before first click on button. After click, title change to standart "Button". Please help.
P.S.: title label must be set up into UIButton class, not into ViewController.

You need to set button title as below :
button.setTitle("Button Title",for: .normal)

Related

How to access a right item button on navigation bar in XCUITest?

I am writing UITest cases for my iOS Swift app. In the app I have created a custom right item button on the navigation bar in this way:
let barButtonItem = UIBarButtonItem(customView: customView)
navigationItem.rightBarButtonItem = barButtonItem
Now I don't know how to access this custom right item button in XCUITest and really need some help. Thanks in advance.
You cannot access a UIBarButtonItem, because it is not a real UIElement (it is not a UIView subclass), but you probably want to access the UIButton inside your right bar button item anyway.
There are several ways how you could access the button, here are two ideas:
1. Query the first button in the navigation bar
let rightNavBarButton = XCUIApplication().navigationBars.children(matching: .button).firstMatch
XCTAssert(rightNavBarButton.exists)
That way you access the first UIButton inside a UINavigationBar.
This only works if there is only one button in your navigation bar. So it will break when you add another button.
2. Use an accessibility identifier
You can define a accessibility identifier for the button inside your right bar button item and use that to access it during the test:
In your app:
let barButtonItem = UIBarButtonItem(customView: customView)
barButtonItem.accessibilityIdentifier = "navbarRightItem"
navigationItem.rightBarButtonItem = barButtonItem
In your test:
let rightNavBarButton = XCUIApplication().navigationBars.buttons["navbarRightItem"]
XCTAssert(rightNavBarButton.exists)
Just make sure you are using accessibilityIdentifier and not accessibilityLabel. Because accessibilityLabel will be read by VoiceOver for handicapped users and should contain useful text.
You have to assign an accessibilityIdentifier to the button
barButtonItem.accessibilityIdentifier = “barButtonItemID”
If didn’t work set IsAccessibilityElement to YES/true

Is it possible for a uibutton to set the title of another uibutton seeing that both were created in the storyboard?

I have just started coding not too long ago, so apologies in advance for any glaringly obvious mistakes.
I have created both buttons using the storyboard. I understand that you can change the sender button's title like how this solution stated it, but I was still not sure how to change the other button title when the first one is clicked. I have tried referencing tags, but nothing happened when I clicked the first button. Is it possible to do this? Please give an example if it is.
Goal: Change the title of one button by clicking another button, both created with Storyboard.
There are two solution to achieve this:
By using IBOutlets
By using Storyboard tags
Using IBOutlets, you can create an outlet of second button e.g.
#IBOutlet weak var btnSecond: UIButton!
Now change the title of this button in action of first button like this:
#IBAction clickFirstBtn(sender: UIButton){
btnSecond.set("New Title", for: .normal)
}
Using storyboard tags, you can give any tag to second button except 0. For example let the tag of second button = 10.
Now you change the title like this:
#IBAction clickFirstBtn(sender: UIButton){
if let btnSecond = view.viewWithTag(10) as? UIButton{
btnSecond.set("New Title", for: .normal)
}
}
(I'll answer to this question even if there already are correct answers because they both are showing swift solutions even if the question is expicitly about Objective C)
The simplest way, as stated in this other answer, is to assign outlets to the buttons. Ctrl-drag from the button in Interface Builder to the #interface of your ViewController and give the button an outlet name:
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIButton *button2;
#end
Then, in the first button's IBAction, call the setTitle:forControlState: method of the second button outlet
- (IBAction)button1Tapped:(id)sender {
[self.button2 setTitle:#"new title" forState:UIControlStateNormal];
}
As an alternative, you can also use storyboard tags (the fact that i used a UIView and then checked if isKindOfClass is to avoid crashes in case you mistakenly assign the same tag to some non-UIButton object):
- (IBAction)button1Tapped:(id)sender {
UIView *buttonView = [self.view viewWithTag:1002];
if ([buttonView isKindOfClass:[UIButton class] ]) {
[(UIButton *)buttonView setTitle:#"new title" forState:UIControlStateNormal];
}
}
Lets say you have Button1 and Button2 and you want to set the title of Button2 onclick of Button1.
You can do it by creating an action on the click of Button1 and setting the title of Button2 on this action..
Swift 3
override func viewDidLoad() {
super.viewDidLoad()
Button1.addTarget(self, action: #selector(setTitle), for: .touchUpInside)
}
#objc func setTitle()
{
Button2.setTitle("Button Title",for: UIControlState.Normal)
}

Cann't I set title and Image to the right bar item on navigation bar at the same time in storyboard?

I want to set the right button item to be this:
It has the title and the down image.
But in storyboard, I can not do that:
I only can set one of the title and image, if I set image, I can not set the title, if I set the title, I can not set the image.
I have tried use the UIButton and the Button Bar Item to do it, get the same result.
Some friend know how to do with that?
You can simply add drag UIButton to rightBarItem in storyBoard . and customise that button as per your needs ..
Check View hierarchy after adding UIButton .
you can try this add the custom view to your bar button item
var RightView = UIView(frame: CGRectMake(0, 0, 100, 44))
// you can add in RightView, your title in uilable and your image in imageview
var RightBarButton = UIBarButtonItem(customView: RightView)
self.navigationItem.rightBarButtonItem = RightBarButton

Make selected button titlelabel to be uppercase

I have button with title "Button". After some actions this button gets "selected" status. While it is selected it have different text color that is easy to specify in interface builder.
The problem is that I also want set it's title to be uppercase. Which I don't know how.
You can set Title for Selected State of UIButton from Attribute Inspector.
Please find below image :
You can also set it programatically.
Objective-C
[btn setTitle:btn.titleLabel.text.uppercaseString forState:UIControlStateSelected];
Swift
btn.setTitle(btn.titleLabel!.text.uppercaseString, forState: .Selected)
There is a built in function for that
Button.capitalizedString
So you can use it in this way in swift
Let me assume that abtn is your IBOutlet of UIButton and Button is titlelabel of UIButton.
abtn.setTitle(abtn.titleLabel!.text.uppercaseString, forState: .Selected)
as said by kirsteins
To capitalize only the first letter you can use:
nameOfString.replaceRange(nameOfString.startIndex...nameOfString.startIndex, with: String(nameOfString[nameOfString.startIndex]).capitalizedString)
If you are setting it from code use
var myString = "string"
myString = myString.uppercaseString
button.setTitle(myString, forState: .Selected)

UIButton Multiple Labels Using UIControlStates

I know with a UIButton, I can add additional UILabels as subviews:
[myButton addSubview: myLabel];
And (at least, with the default title label) I can set its text color when tapped by using:
[myButton setTitleColor:someColor forState:UIControlStateHighlighted]
My question is, how can I implement this functionality for additional UILabels added to the UIButton (if this is possible)?
Subclass UIButton and add your additional labels in there as instance variables. Then override -setHighlighted and -setSelected to adjust the additional labels as desired.
FYI - you call [myButton setTitleColor...], not [myButton.titleLabel setTitleColor...]
It seems my way of going about it isn't easy, but I realized I can just add an action to the UIButton for the event UITouchDown, and change the labels accordingly in the action.
You would have to set myLabels text color, before you added it as a subView.
Otherwise, you'll have to enumerate through the button's subviews and change each of your added label's text colors.
Update:
You can change the button title's font as follows:
myButton.titleLabel!.font = UIFont(name: "...", 10)
You can change the button's title color as follows:
colorsBtn.setTitleColor(UIColor.brownColor(), forState: UIControlState.Highlighted)

Resources