How to change Weight property of UIButton title in a custom UITableViewCell? - swift

I have a custom cell that has three buttons, that when one of them is pressed, its title weight changes to bold and the font size of the other two buttons gets shrunked.
As it is shown above, while the titles of the other buttons do change their font size, the weight of the title of the pressed button does not seem to change at all. What am I missing? Thanks.
The code of the custom cell:
import UIKit
class SymptomTableViewCell: UITableViewCell {
#IBOutlet weak var severeButton: UIButton!
#IBOutlet weak var moderateButton: UIButton!
#IBOutlet weak var lowButton: UIButton!
var selectedButton = UIButton()
var buttons = [UIButton]()
override func awakeFromNib() {
super.awakeFromNib()
buttons = [severeButton, moderateButton, lowButton]
}
#IBAction func rateButtonPressed(_ sender: UIButton) {
if sender != selectedButton {
selectedButton = sender
for button in buttons {
if button != selectedButton {
button.titleLabel?.font = .systemFont(ofSize: 12)
}
}
selectedButton.titleLabel?.font = .boldSystemFont(ofSize: 16)
}
}
}

Changing button style from plain to default worked.

Related

Add custom button UISearchController in swift

i want to add a custom button on UISearchController in swift.
Like this image i want to add the custom button. In this image it is a search bar. The plus button will show if there is text in the search bar and it will disappear if search bar is empty. can any one help me to implement it.
When the TextField changes, check for whitespace.
Hide or position the button depending on whether it is blank or not.
After the view is loaded, the text field is blank, so it is initialized with ishidden().
I hope my answer is helpful to you.
SearchViewController.swift
import UIKit
class SearchViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var searchTextField: UITextField!
#IBOutlet weak var plusButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
searchTextField.delegate = self
plusButton.isHidden = true
}
func textFieldDidChangeSelection(_ textField: UITextField) {
if searchTextField.text == "" {
plusButton.isHidden = true
} else {
plusButton.isHidden = false
}
}
}
Preview

If we click on the AddCommentsButton the AddCommentsTextView must enable. how to implement in swift iOS?

#IBOutlet weak var AddCommentsButton: UIButton!
#IBOutlet weak var AddCommentsTextView: UITextView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.AddCommentsButton.setTitle("Add Comments", for: UIControl.State.normal)
self.AddCommentsButton.setTitleColor(UIColor.blue, for: UIControl.State.normal)
AddCommentsTextView.rchDelegate = self as? RCHTextViewDelegate
AddCommentsTextView.delegate = self as? UITextViewDelegate
AddCommentsTextView.placeholder = "Leave a comment"
AddCommentsTextView.canShowBorder = true
AddCommentsTextView.addBorderWithCornerRadius(cornerRadius: 0, borderColor: .Gray, borderWidth: 0.5)
AddCommentsTextView.backgroundColor = UIColor.clear
self.AddCommentsTextView.font = UIFont.dynamicFont(control: .listKeyText)
self.AddCommentsTextView.textColor = UIColor.listKeyTextColor()
}
#IBAction func didTapAddCommentsButton(_ sender: Any) {
}
If you are looking for when you click on didTapAddCommentsButton button then the AddCommentsTextView Textview should show the cursor in the textview for entering the text then you can use:
#IBAction func didTapAddCommentsButton(_ sender: Any) {
self.AddCommentsTextView.becomeFirstResponder()
}
So that on button click it will enable textview cursor for entering purpose.
UPDATED:
If you want to remove focus from the text entry then use:
self.AddCommentsTextView.resignFirstResponder()

How do I change the text of a label with a segmented controller that has three segments>

For class I have a segmented controller that has three segments.
First segment: make label say Hello
Second segment make label say goodbye
Third segment make label say you rock.
I also need to add a name entered by a text field and when a button is pushed it goes into the label either starting with Hello, Goodbye, or You Rock.The picture is showing what I have so far.
second picture shows what I need to do
You can get the title of the selected segment and insert it into the name label text.
class ViewController: UIViewController {
// MARK: Outlets
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var greetingControl: UISegmentedControl!
// MARK: Properties
var name: String? {
return nameTextField.text
}
var greeting: String? {
return greetingControl.titleForSegment(
at: greetingControl.selectedSegmentIndex
)
}
// MARK: Methods
private func updateOutlets() {
nameLabel.text = "\(greeting!), \(name!)!"
}
// MARK: Actions
#IBAction func changeNameOnTap(_ sender: UIButton) {
updateOutlets()
}
#IBAction func nameController(_ sender: UISegmentedControl) {
updateOutlets()
}
}

I have two buttons, how can I change one of the button's image if the other button is tapped? - Swift

So I am making an app where the user will have to categorize their post between two options - "Lost" & "Found". Each button has the option to be clicked, but when I did the Testflight I got the comment that when they clicked the other button because they made a mistake, that the image stayed activated.
Is there a way to change the image of the other button back to the normal deselected image if the other button is clicked?
Here's my code:
var lost = Bool()
#IBOutlet weak var address: UITextField!
#IBOutlet weak var breed: UITextField!
#IBOutlet weak var phone: UITextField!
#IBOutlet weak var imagePosted: UIImageView!
#IBOutlet weak var changeImageLostButton: UIButton!
#IBOutlet weak var changeImageFoundButton: UIButton!
//BUTTONS PRESSED - LOST & FOUND
#IBAction func lostPressedButton(_ sender: UIButton) {
changeImageLostButton.setImage(UIImage(named:"LostButton-active.png"), for: .normal)
lost = true
}
#IBAction func foundPressedButton(_ sender: Any) {
changeImageFoundButton.setImage(UIImage(named:"FoundButton-active.png"), for: .normal)
lost = false
}
The problem seems to be that while you set the image on each button when it is pressed, you don’t remove the image from the other one. All you need to do is remove the image from the appropriate button in the lostPressedButton and foundPressedButton methods:
#IBAction func lostPressedButton(_ sender: UIButton) {
changeImageLostButton.setImage(UIImage(named:"LostButton-active.png"), for: .normal)
// Remove the image on the found button.
changeImageFoundButton.setImage(nil, for: .normal)
lost = true
}
#IBAction func foundPressedButton(_ sender: Any) {
changeImageFoundButton.setImage(UIImage(named:"FoundButton-active.png"), for: .normal)
// Remove the image on the lost button.
changeImageLostButton.setImage(nil, for: .normal)
lost = false
}

UITextfield value for each indexpath.row

My uitableview consists of 10 cells and each cell has 2 uitextfield's.
I need to take values from each uitextfield in each cell and add it to an array
// my custom cell
class MatchTableViewCell: UITableViewCell, UITextFieldDelegate {
#IBOutlet weak var Team2Score: UITextField!
#IBOutlet weak var Team1Score: UITextField!
#IBOutlet weak var Team2: UILabel!
#IBOutlet weak var Image2: UIImageView!
#IBOutlet weak var Image1: UIImageView!
#IBOutlet weak var Team1: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Make sure in the cell initializer for tableviewcontroller that your using your custom cell name and that you've set your identifier correctly. Then just initalize a cell and say something to the effect of
cell.Team2Score.text = "100"
you will need a global array to hold the strings. So..
var wordArray: [String] = []
you will need to add a button within the cell so when you hit the button it will add (append in the input of string (letters) to the array.
You need to add a tag to the button to know which cell it was clicked in.
button.tag = indexPath.row
#IBaction button(sender: UIButton){
var team2Words = Team2Score.text
wordArray.append(team2Words)
// do the samething for Team1Score
// you can use and if statement to check if one or the other one is empty
}
This will give you a lead