Force UIButton titleLabel text to show Swift - swift

I have a button that is made in the following manner. The label of my button is truncated as I have shown at the bottom
let width = self.view.frame.size.width
let label = UIButton()
// label setup
label.setTitle("Going Out", for: .normal)
label.titleLabel?.font = UIFont(name: "avenir-next-regular", size: 8)
label.setTitleColor(.black, for: .normal)
label.layer.zPosition = 8
label.titleLabel?.textAlignment = .center
label.frame = CGRect(x: 3 * width / 32, y: width / 6, width: width / 6, height: width / 8)
view.addSubview(label)
This produes a button with the text: Go..Out
How can I force this to show: Going Out
While NOT sizeToFit() and shrinking my UIButton

Try below, should work:
label.titleLabel?.adjustsFontSizeToFitWidth = true

Related

How to create overlapped bar button item programatically

I know how to create overlap shopping cart and quantity label with xib using uiimage like this
Now i'm trying to create overlap bar button items programatically but cannot figure out how to position the elements. My attempt:
My current code for bar button items:
let button: UIButton = UIButton(frame: CGRect(x: 0.0,
y: 0.0,
width: 24.0,
height: 24.0))
button.setImage(UIImage(named: "my cart", in: nil, compatibleWith: nil), for: .normal)
button.addTarget(self, action: #selector(cartButtonDidTapped), for: .touchUpInside)
let shoppingCartButton: UIBarButtonItem = UIBarButtonItem(customView: button)
shoppingCartQuantityLabel.layer.cornerRadius = 10
shoppingCartQuantityLabel.layer.masksToBounds = true
shoppingCartQuantityLabel.textColor = .white
shoppingCartQuantityLabel.backgroundColor = .red
shoppingCartQuantityLabel.textAlignment = .center
let shoppingCartQuantityLabelItem: UIBarButtonItem = UIBarButtonItem(customView: shoppingCartQuantityLabel)
navigationItem.rightBarButtonItems = [shoppingCartQuantityLabelItem, shoppingCartButton]
Idea here is to add the label as subview inside the button. You can adjust the label as per your needs from the below example,
let button: UIButton = UIButton(frame: CGRect(x: 0.0, y: 0.0, width: 24.0, height: 24.0))
button.setImage(#imageLiteral(resourceName: "my cart"), for: .normal)
let label = UILabel(frame: .init(origin: .init(x: 20, y: -8), size: .init(width: 20, height: 20)))
label.text = "12"
label.textAlignment = .center
label.textColor = .white
label.font = .systemFont(ofSize: 10)
label.backgroundColor = .red
label.layer.cornerRadius = 10
label.clipsToBounds = true
button.addSubview(label)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
You can use this code snippet also from Github: https://gist.github.com/freedom27/c709923b163e26405f62b799437243f4#gistcomment-2236010
then set badge to your last right bar button item as below
navigationItem.rightBarButtonItems = [shoppingCartButton]
let lastBarButton = navigationItem.rightBarButtonItems?.last
lastBarButton?.setBadge(text: "10", withOffsetFromTopRight: CGPoint(x: 38, y: -3), andColor: UIColor.red, andFilled: true, andFontSize: 12)

How do I place the stepper value between the - and + signs?

I am programatically trying to customize a UIStepper. I'd like to place the stepper.value between the - and + buttons and I want to change the background of the stepper to white.
How would I do that?
Here is what I have that does not work:
let stepper = UIStepper(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
stepper.minimumValue = 0
stepper.center = self.contentView.center
stepper.backgroundColor = .white
stepper.layer.backgroundColor = UIColor.white.cgColor
stepper.stepValue = 1
stepper.tintColor = UIColor(hex: Constants.Colors.primary)
stepper.setIncrementImage(UIImage(named: "icon_cart_plus"), for: .normal)
stepper.setDecrementImage(UIImage(named: "icon_cart_minus"), for: .normal)

Creating a button image with padding bottom in Swift

I'm creating a system of Filters.
I'm trying to do that:
Currently I'm using the setBackgroundImage func, and I don't have this little rect below the picture:
filterButton.setBackgroundImage(imageForButton, for: .normal)
I tried functions like this one:
filterButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
But it's not effective if I keep the setBackgroundImage func.
If I use setImage(), the results is good but the click is not effective any more.
filterButton.setImage(imageForButton, for: UIControlState.normal)
Do you have an idea to do what I would like ?
The full snippet:
for i in 0 ..< CIFilterNames.count {
itemCount = i
// Button properties
let filterButton = UIButton(type: .custom)
// filterButton.frame = CGRectMake(xCoord, yCoord, buttonWidth, buttonHeight)
filterButton.frame = CGRect(x: xCoord, y: yCoord, width: buttonWidth, height: buttonHeight)
filterButton.tag = itemCount
filterButton.addTarget(self, action: #selector(self.filterButtonTapped(_:)), for: .touchUpInside)
// filterButton.layer.cornerRadius = 6
// filterButton.clipsToBounds = true
let ciContext = CIContext(options: nil)
let coreImage = CIImage(image: originalImage.image!)
let filter = CIFilter(name: "\(CIFilterNames[i])" )
filter!.setDefaults()
filter!.setValue(coreImage, forKey: kCIInputImageKey)
let filteredImageData = filter!.value(forKey: kCIOutputImageKey) as! CIImage
let filteredImageRef = ciContext.createCGImage(filteredImageData, from: filteredImageData.extent)
let imageForButton = UIImage(cgImage: filteredImageRef!)
filterButton.backgroundColor = UIColor.red
filterButton.setImage(imageForButton, for: UIControlState.normal)
//filterButton.imageEdgeInsets = UIEdgeInsets(top: 0,left: 0,bottom: 15,right: 0)
//filterButton.setBackgroundImage(imageForButton, for: .normal)
filterButton.setTitle("A\(i)", for: .normal)
// filterButton.backgroundImage.contentMode = .scaleAspectFill
filterButton.setTitleColor(UIColor(red: 22 / 255, green: 21 / 255, blue: 22 / 255, alpha: 1), for: .normal)
filterButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
filterButton.titleEdgeInsets.top = 140
// Add Buttons in the Scroll View
xCoord += buttonWidth + gapBetweenButtons
filtersScrollView.addSubview(filterButton)
}
It looks like you are trying to do everything in a single UIButton. I would suggest creating a custom element to handle this.
There are a few ways you could setup a custom UI element with the functionality that you want:
Use a UILabel to display the "A1" label with the colored background and display the image itself in a separate UIImageView. These could be overlaid with a invisible UIButton to receive the touchUpInside event and pass to your controller.
Alternatively, you could create a custom UIControl add add a UILabel and UIImageView as subviews. Disabling userInteraction on the subviews should allow the UIControl to receive the touch events. This could be contained in a single reusable class as if it's one UI element.

How to set button width based on text for dynamically created button in swift 3?

I created the dynamic buttons. I need to change the width of button based on tittle label text. Here is my code.
for i in 0..<holdingSize {
let button = UIButton(type: .custom)
if i == 0 {
frame = CGRect(x: 10, y: 5, width: 100, height: 30)
}else{
buttonY = buttonY + 110
frame = CGRect(x: buttonY, y: 5, width: 100, height: 30)
}
button.setTitle("\(arrayOfHoldings[i])", for: UIControlState.normal) // We are going to use the item name as the Button Title here.
button.titleLabel?.text = "\(arrayOfHoldings[i])"
button.titleLabel?.font = UIFont(name: (button.titleLabel?.font.fontName)!, size: 15)
button.setTitleColor(Colors.green, for: .normal)
button.sizeToFit()
}
Try this
func labelSizeWithString(text: String,fontSize: CGFloat, maxWidth : CGFloat,numberOfLines: Int) -> CGRect{
let font = UIFont.systemFontOfSize(fontSize)//(name: "HelveticaNeue", size: fontSize)!
let label = UILabel(frame: CGRectMake(0, 0, maxWidth, CGFloat.max))
label.numberOfLines = numberOfLines
label.font = font
label.text = text
label.sizeToFit()
return label.frame
}
This will give you the frame of the label, you can set the height of your button from that.
FOR SWIFT 3.0
func labelSize(for text: String,fontSize: CGFloat, maxWidth : CGFloat,numberOfLines: Int) -> CGRect{
let font = UIFont.systemFont(ofSize: fontSize)//(name: "HelveticaNeue", size: fontSize)!
let label = UILabel(frame: CGRect(x: 0, y: 0, width: maxWidth, height: CGFloat.leastNonzeroMagnitude))
label.numberOfLines = numberOfLines
label.font = font
label.text = text
label.sizeToFit()
return label.frame
}

Can i have 2 colours in my navigation bar Tittle?

I want to set 2 colours in my navigation bar title.
If i cant to do that, how can i center perfectly an image as tittle of my navigation bar?
I tried:
let logo = UIImage(named: "LogoMV_BAR#2x");
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
imageView.image = logo
imageView.contentMode = UIViewContentMode.ScaleAspectFit
self.navigationItem.titleView = imageView
And it looks so small and not centered.
Thanks guys!
Thanks user Larme, your answer helped me a lot. Thats how i do it:
let A = NSMutableAttributedString(string: "Label Test", attributes: [NSFontAttributeName: UIFont(name: "Georgia", size: 18)!])
A.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(), range: NSRange(location: 6, length: 5))
let LabelTexto: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
LabelText.attributedText = A
self.navigationItem.titleView = LabelText