I wrote an extension to display an empty state if my view controller is empty. I also wanted to add a UIButton() with a target. How can I implement the addTarget action for my button within the extension? (F.e. a simple "Hello World" or what ever I need)
extension UITableView{
func setEmptyView(title: String, message: String) {
// handling states
button.setTitle("+ Add New Documentation", for: .normal)
button.titleLabel!.font = UIFont(name: "Roboto-Regular", size: 24)
button.setTitleColor(UIColor(hex: "BDC86F"), for: .normal)
button.backgroundColor = UIColor(hex: "506182")
button.addTarget...
//more code
emptyView.addSubview(button)
}
}
I have achieved the same thing for UIView extension. Hope, It will help you and I assume that you're able to convert it for UITableView
Code:
extension UIView{
func setEmptyView(title: String, message: String) {
let emptyView = UIView(frame: self.bounds)
// handling states
let button = UIButton(frame: emptyView.bounds)
button.setTitle("+ Add New Documentation", for: .normal)
button.titleLabel!.font = UIFont(name: "Roboto-Regular", size: 24)
button.setTitleColor(UIColor.red, for: .normal)
button.backgroundColor = UIColor.black
button.addTarget(self, action: #selector(btnTapEvent(sender:)), for: .touchUpInside)
emptyView.addSubview(button)
//more code
self.addSubview(emptyView)
}
#objc func btnTapEvent(sender:UIButton){
print("btnTapEvent")
}
}
Reference image:
Related
I am coding in Swift 5.0. I want to create an app where users can download custom fonts and use them in any app. I am using a keyboard extension to do that.
Here is my code
class KeyboardViewController: UIInputViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = createButton(title: "A")
self.view.addSubview(button)
}
func createButton(title: String) -> UIButton {
let button = UIButton(type: .system)
button.frame = CGRect(x: 0,y: 0,width: 20,height: 20)
button.setTitle(title, for: .normal)
button.sizeToFit()
button.translatesAutoresizingMaskIntoConstraints = false
button.titleLabel?.font = UIFont(name: "Montague.ttf", size: 15)
button.backgroundColor = UIColor(white: 1.0, alpha: 1.0)
button.setTitleColor(UIColor.darkGray, for: .normal)
button.addTarget(self, action: #selector(didTapButton(sender:)), for: .touchUpInside)
return button
}
#objc func didTapButton(sender: AnyObject) {
let button = sender as! UIButton
button.titleLabel?.font = UIFont(name: "Montague.ttf", size: 15)
let title = button.title(for: .normal)
textDocumentProxy.insertText(title!)
}
Here is the output.result
So my question is how to make a keyboard button appearance and output in the font I want?
You can customize your button's title font using the titleLabel attribute of button.
button.titleLabel?.font = UIFont(name: "YourCustomFont", size: 10.0)
You can't change font on the output as app developers decide what fonts they want to use in their app AFAIK.
Edit: Well I didn't know what you meant by setting the output, for more info about the app you linked to please read this SO post:
How to Insert NSAttributedString using custom keyboard extension?
tl;dr: You can't set a custom font, those apps use unicodes and if you choose to, you should too.
I'm using a function to create multiple buttons for my game.
func createButton() {
let button = UIButton()
button.setTitle("", for: .normal)
button.frame = CGRect(x:15, y: 50, width: 200, height:100)
button.backgroundColor = UIColor.red
self.view.addSubview(button)
button.addTarget(self, action: Selector(("buttonPressed:")), for:
.touchUpInside)
}
I call this function once for testing in viewDidLoad function, but I don't know what code I should put into my buttonPressed() function for the color of my button to change? I tried doing
self.backgroundColor = UIColor.blue
but that didn't work. I also tried using UIButton and button instead of self, but both of those didn't work either. What should I do?
Your code isn't clean Swift 4 code. Here's how to do this:
Create your button like you are, but change Selector to #selector:
func createButton() {
let button = UIButton()
button.setTitle("", for: .normal)
button.frame = CGRect(x:15, y: 50, width: 200, height:100)
button.backgroundColor = UIColor.red
self.view.addSubview(button)
button.addTarget(self, action: #selector((buttonPressed)), for: .touchUpInside)
}
Use the sender that is automatically added:
#objc func buttonPressed(sender: UIButton) {
sender.backgroundColor = UIColor.blue
}
Additionally may I offer a few suggestions?
Check the background color before changing it. No sense in needlessly changing a button that is already blue.
Since you aren't setting the title to your button, set the tag property (you can even add this as a parameter to createButton). This way you can know which button was tapped.
Just make the button an instance property.
let changingButton = UIButton()
func createButton() {
changingButton.backgroundColor = UIColor.red
changingButton.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
}
#objc func buttonPressed() {
changingButton.backgroundColor = UIColor.blue
}
I Have written a function in Swift 4 which creates a button that receives the UIButton, a Color and a String for the title. I would like to add to this function a highlight or tap style but I am not sure how.
The function:
func homeBtn(button: UIButton, color: UIColor, title: String){
button.setTitle(title, for: .normal)
button.setTitleColor(color, for: .normal)
button.titleLabel?.font = UIFont(name: "Raleway-Medium", size: 24)
button.titleLabel?.lineBreakMode = .byWordWrapping
button.titleLabel?.textAlignment = .center
button.titleEdgeInsets = UIEdgeInsetsMake(20,20,20,20)
button.layer.borderColor = color.cgColor
button.layer.borderWidth = 2
button.layer.cornerRadius = button.frame.width/2
button.layer.backgroundColor = whiteColor.cgColor
}
The button is a circle and has a String centered in the middle with the UIColor being used as an outline. Can someone show me how to add a tap style to this function that uses the same UIColor to fill in the button and turn the text white please?
For the title color you can easily do this without adding events.
button.setTitleColor(.white, for: .highlighted)
button.setTitleColor(.black, for: .normal)
For the background color you need to define the following events:
button.addTarget(self, action: #selector(touchDown(button:)), for: .touchDown)
button.addTarget(self, action: #selector(touchUpInside(button:)), for: .touchUpInside)
Define functions - You can refer additional touch types as well - see UIControlEvents
#objc func touchDown(button: UIButton) {
guard let borderColor = button.layer.borderColor else {
return
}
button.backgroundColor = UIColor(cgColor: borderColor)
}
#objc func touchUpInside(button: UIButton) {
// Set backgroundColor for normal state...
}
I am trying to set a listener function for my button but I keep getting error . Here is how've done it :
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//Create the BackGround
self.view.backgroundColor = UIColor.black
let startButton = UIButton()
startButton.setTitle( " start ", for: UIControlState.normal)
startButton.setTitleColor(UIColor.blue, for: UIControlState.normal)
startButton.frame = CGRect(x: 10, y:40 , width: 250, height: 25)
self.view.addSubview(startButton)
startButton.addTarget(self, action: "buttonPressed:" , for: .touchUpInside)
}
func buttonPressed(sender: UIButton!) {
print("hello")
}
}
Does any one knows where am I making the mistake ? :)
You were close. Just replace
startButton.addTarget(self, action: "buttonPressed:" , for: .touchUpInside)
with this:
startButton.addTarget(self, action: #selector(ViewController.buttonPressed(sender:)) , for: .touchUpInside)
I dynamically created several buttons in my UITableviewCell class like so:
for (clientObjectId, _) in connectedObjectIds {
self.clientNameButton = UIButton(type: UIButtonType.System) as UIButton
self.clientNameButton.titleLabel?.lineBreakMode = NSLineBreakMode.ByTruncatingTail
self.clientNameButton.frame = CGRectMake(self.leftSideSpaceForUsersAndUserLabel, clientNameButtonFrameHeight, self.nameButtonWidth, self.nameButtonHeight)
self.clientNameButton.setTitle(self.userObjectIdsAndNames[clientObjectId], forState: UIControlState.Normal)
self.clientNameButton.titleLabel!.font = UIFont(name: "Helvetica", size: 12.0)
self.clientNameButton.addTarget(self, action: "asdf:", forControlEvents: UIControlEvents.TouchUpInside)
self.nameButtons.append(self.clientNameButton)
self.addSubview(self.clientNameButton)
}
However, I can't seem to find a way to call the following function in my table view cell class:
func asdf(sender:UIButton) {
print("Button tapped")
}
I don't really care so much about being able to call it in my UITableViewCell class as much as my calling the function in my table view class.
Anybody have a solution to this problem?
for (clientObjectId, _) in connectedObjectIds {
self.clientNameButton = UIButton(type: UIButtonType.System) as UIButton
self.clientNameButton.titleLabel?.lineBreakMode = NSLineBreakMode.ByTruncatingTail
self.clientNameButton.frame = CGRectMake(self.leftSideSpaceForUsersAndUserLabel, clientNameButtonFrameHeight, self.nameButtonWidth, self.nameButtonHeight)
self.clientNameButton.setTitle(self.userObjectIdsAndNames[clientObjectId], forState: UIControlState.Normal)
self.clientNameButton.titleLabel!.font = UIFont(name: "Helvetica", size: 12.0)
self.clientNameButton.addTarget(self, action: "asdf:", forControlEvents: UIControlEvents.TouchUpInside)
self.nameButtons.append(self.clientNameButton)
self.addSubview(self.clientNameButton)
}