Add label in center of imageView - swift

I have the two images like in the picture at the end of the question (the image of a list and a red dot). I want to add a label in the center of the red dot. This is my code that doesn't work:
image = UIImageView(image: UIImage(named: "pallino"))
image.frame = CGRect(x: 55, y: self.view.frame.height-60, width: 22, height: 22)
self.view.addSubview(image)
image.layer.cornerRadius = image.frame.width/2
label = UILabel(frame: CGRect(x: self.image.center.x, y: self.image.center.y, width: image.frame.size.width, height: image.frame.size.height))
label.text = "4"
label.font = UIFont(name:"HelveticaNeue-Bold", size: 15.0)
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor.black
image.addSubview(label)
Can someone tell me were am I wrong?

Problem in this line:
label = UILabel(frame: CGRect(x: self.image.center.x, y: self.image.center.y, width: image.frame.size.width, height: image.frame.size.height))
self.image.center.x - The center point is specified in points in the coordinate system of its superview, it is mean that self.image.center is not center of image
You need frame for label, something like this:
let imageSize = 22
let frame = CGRect(x: 0, y: 0, width: imageSize, height: imageSize)
let label = UILabel(frame: frame)
label.aligment = .center

You can set constraint in your label(centered Horizontally and Vertically). Try with the following code.
let label = UILabel()
label.text = "4"
label.font = UIFont(name:"HelveticaNeue-Bold", size: 15.0)
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor.black
image.addSubview(label)
label.centerXAnchor.constraint(equalTo: self.image.centerXAnchor).isActive = true
label.centerYAnchor.constraint(equalTo: self.image.centerYAnchor).isActive = true

To center the label in UIImageView you can refer to this example which is tested and working solution.
extension UIImageView {
/// Create label programmatically
/// - Returns: UILabel
private func ageSensitiveLabel() -> UILabel {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height))
label.text = "Content"
label.font = .systemFont(ofSize: 5.0)
label.textAlignment = .center
label.numberOfLines = 0
label.minimumScaleFactor = 0.5
label.adjustsFontSizeToFitWidth = true
label.baselineAdjustment = .alignCenters
label.textColor = .white
return label
}
/// Add label as subview to UIImageView
func addAgeSensitiveLabel() {
self.subviews.forEach { view in
DispatchQueue.main.async {
view.removeFromSuperview()
}
}
self.addSubview(ageSensitiveLabel())
}
}
Use it with UIImageView
imageView.addAgeSensitiveLabel()

Related

How to change UITextField RightView size?

I trying put to 2 items in rightView component. for example;`
let label = UILabel(frame: CGRect(x: -80, y: 0, width: 100, height: 50))
let rightView = UIView(frame: CGRect(x: 100, y: 100, width: 200, height: 100))
label.text = String(self.availableBalance) + " ₺"
label.textColor = Colors.INVEST_RED
label.textAlignment = .center
let imageView = UIImageView(frame: CGRect(x: -100, y: 15, width: 20, height: 20))
imageView.image = UIImage(named: "full_balance_button")
rightView.addSubview(imageView)
rightView.addSubview(label)
let tap = UITapGestureRecognizer(target: self, action:#selector(tapGesture))
label.addGestureRecognizer(tap)
label.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tap)
imageView.isUserInteractionEnabled = true
rightView.addGestureRecognizer(tap)
rightView.isUserInteractionEnabled = true
rightView.backgroundColor = .white
textField.rightView = rightView
textField.rightViewMode = .always
But I can't change size rightView.

Edit- how can I get UIlabel to get in the middle of the container?

how can i get UIlabel to get in the middle of the container? and that text and view disappear equally and come to the next view.
EDIT: This code is working fine for me now
class TestViewController: UIViewController {
let container = UIView()
let redSquare = UIView()
let blueSquare = UIView()
let label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
self.label.textColor = UIColor.orange
self.label.font = label.font.withSize(25)
self.label.numberOfLines = 0
self.label.center = self.blueSquare.center
self.label.textAlignment = NSTextAlignment.center
self.label.text =
"sosdihfiosdfhsdhfdisfhsdfhdsoifhsdofhsdifhdsofhdsofhsdohdsfdosdohdfh"
self.blueSquare.addSubview(label)
self.label.frame = CGRect(x: 45, y: 120, width: 300, height: 100)
self.container.frame = CGRect(x: 7, y: 200, width: 400, height: 500)
self.view.addSubview(container)
self.redSquare.frame = CGRect(x: 0, y: 0, width: 400, height: 500)
self.blueSquare.frame = redSquare.frame
self.redSquare.backgroundColor = UIColor.darkGray
self.blueSquare.backgroundColor = UIColor.darkGray
self.container.addSubview(self.redSquare)
}
Define two labels like:
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
let label2 = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
then inside viewDidLoad() before adding redSquare to container fix some labels's property and add label's to blue and red square:
label.center = self.redSquare.center
label.textAlignment = NSTextAlignment.center
label.text = "same text"
label2.textAlignment = NSTextAlignment.center
label2.center = self.blueSquare.center
label2.text = "same text"
self.blueSquare.addSubview(label2)
self.redSquare.addSubview(label)

How do I add a background to a scroll view and have it repeat to the end?

I am making a roster on a scroll view with every slide being someone different. I was wondering if there is a way to set a background and have it repeat to the end as it cuts off at a certain person
Currently, I am copy and pasting the background and dragging it to match
for i in 0..
let label = UILabel(frame: CGRect(x:xPosition+110, y: 350, width: 200, height: 200))
label.text = names[i]
label.font = UIFont.systemFont(ofSize: 35, weight: UIFont.Weight.bold)
label.adjustsFontSizeToFitWidth = true
label.textColor = UIColor.black
let imageView = UIImageView()
imageView.image = UIImage(named: names[i])
imageView.contentMode = .scaleAspectFit
imageView.frame = CGRect(x: xPosition-110, y: 150, width: 650, height: 250)
let descriptionM = UITextView(frame: CGRect(x: xPosition+65, y: 525, width: 300, height: 250))
descriptionM.text = desc[i]
descriptionM.textColor = UIColor.black
descriptionM.backgroundColor = UIColor.clear
descriptionM.contentMode = .scaleAspectFill
descriptionM.font = UIFont.systemFont(ofSize: 20)
scrollView.contentSize.width = scrollView.frame.width * CGFloat(i + 1)
scrollView.addSubview(label)
scrollView.addSubview(imageView)
scrollView.addSubview(descriptionM)
}
Results would be the photo to repeat to the end and past.
One way is to turn the background image into a resizable image with zero edge insets and the default (tiling) resizing mode. Set your image view's contentMode to .scaleToFill.
let image = UIImage(named: "background")!.resizableImage(withCapInsets: .zero)
imageView.contentMode = .scaleToFill
imageView.image = image
Then make sure your imageView is at least as big as your scroll view's contentSize.

Why disappear navigationBar backButtonItem? Swift4

I use largeTitleBar.
When navigationBar.title has character over 20 bytes (in Case iPhone SE), backButtonItem.title disappears. (In case iPhoneX, 22 bytes)
navigationBar.title becomes truncate affects this problem.
But I don't have any idea to solve this problem.
navigationItem.title = someText
// when navigationItem.title is truncated, backButtonItem.title disappears.
self.title = "Your TiTle Text"
var lbl = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
lbl.text = navigationItem?.title
lbl.textColor = UIColor.white
if let fontSize = UIFont(name: "Helvetica-Bold", size: 30.0) {
lbl.font = fontSize
}
lbl.backgroundColor = UIColor.clear
lbl.adjustsFontSizeToFitWidth = true
lbl.textAlignment = .center
navigationItem?.titleView = lbl
Try above code may help you.

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
}