How to access TextField label frame? - swift

I am trying to display a lock icon on the left hand side of a search bar's text field, like Safari displays when you visit a secure site, but I don't know how to set the frame.
How can you get the frame of the label within a text field?
Here's what I have:
let lockImageView = UIImageView()
lockImageView.image = UIImage(named: "lock")
lockImageView.contentMode = .scaleAspectFit
lockImageView.frame = CGRect(x: searchBar.bounds.midX, y: 23, width: 11, height: 11)
searchBar.addSubview(lockImageView)
Here's what it looks like because the frame for the icon is not correct:

Add this two to your searchBar before adding the image to the subview
searchBar.leftView = lockImageView
searchBar.leftViewMode = UITextFieldViewMode.Always

Related

NSImageView being clipped off

I'm trying to display an NSImageView with a symbol icon inside it, however the top and right sides are being clipped off. I tried changing the imageScaling, imageAlignment, bounds, and image.size, but it says the same.
Here's what I'm doing right now:
let iconImage = NSImage(named: .myImage)!
let icon = NSImageView(image: topBarButtonIcon)
icon.frame = CGRect(x: 7.5, y: 7.5, width: 15, height: 15)
icon.animates = true
icon.isEditable = false
icon.imageAlignment = .alignCenter
icon.imageScaling = .scaleAxesIndependently
Here's how it shows up:
This might matter: I'm sub-viewing the icon into a custom NSButton class I made, it's given a 30x30 frame, but even if I change it to something like 100x100 the icon says clipped.

Button with multiple accessible labels and images in Swift

I need to create custom button class, reuse it 4 times and I also need to override its text and image name. My next problem is how to set its frame somehow dynamically (now it is static), because I need this 4 buttons in grid 2x2.
I'm trying to create button exactly like this: https://imgur.com/a/dNhUGhc.
I have coded this but it is static and in ViewController I can't edit (override) these labels and image name. And if I tried to reuse this class I would have them in the same spot, because frame settings is exactly the same.
I'm subclassing UIButton. If something more suitable exists just let me know.
Code for adding label
// city label
let cityRect = CGRect(x: 0, y: 20, width: buttonWidth, height: 25)
let cityLabel = UILabel(frame: cityRect)
cityLabel.text = "Label"
cityLabel.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
cityLabel.textAlignment = .center
addSubview(cityLabel)
Code for adding image
// image
let imageView = UIImageView(image: UIImage(named: "something"))
imageView.frame = CGRect(x: 0, y: 60, width: 40, height: 40)
imageView.center.x = self.center.x - 20
addSubview(imageView)
Can you guys help me? Thanks
It looks like what you need to do is use an IBOutlet. Basically, an IBOutlet will give you a reference within your code (custom UIView or UIViewController subclass) to the button that you've setup in xib or storyboard. Then you can make any changes or adjustments that you want to it at runtime.
Check this out to learn more about IBOutlets and how to set them up in your project.

swift create a dot on UIimage

I need your help with solving this problem.
My UIImage is allowed to be moved around the screen using PanGesture and it also has TapGesture to rotate with trasform
.transform.rotated(by: CGFloat(M_PI_4))
After user is done with positioning and rotating, he clicks the UIbutton to show a four dots in corners of the Image
//dotcode
let dotSize = 20
let firstDotView = UIView()
firstDotView.frame = CGRect(x: 0, y: 0, width: dotSize, height: dotSize)
firstDotView.backgroundColor = UIColor.black
firstDotView.layer.cornerRadius = 10
view.addSubview(firstDotView)
1: dots like that, don't have any connection with green space
Dots always show in the corner

Set UIBarButtonItem Text to Size According to Length of Text

I'm programmatically changing the text of a UIBarButtonItem. In most cases, it works out fine but in some cases, it is extremely long and overtakes the title bar.
I'm looking for a way to set the font-size to change to fit within a certain boundary so it doesn't overtake the title.
I'm away from a compiler to test this but you could try something like:
var size = 18 - (btn.titleLabel?.text.characters.count / 3)
btn.titleLabel?.font = UIFont(name: "Helvetica" , size: size )
The best way to change the font size of a UIBarButtomItem is by using a custom view:
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 44))
label.text = "Hello world!"
label.font = UIFont.systemFont(ofSize: UIFont.smallSystemFontSize)
let barButton1 = UIBarButtonItem()
barButton1.customView = label
...
That way, you can control the font size on the UILabel and make it smaller when you need to.

Positioning a NSImage within a NSButton

I have an NSButton, created programatically. within said button, I have an NSImage that serves as an icon:
let button = NSButton(frame: NSRect(x: 10, y: (200 + (50 * id)), width: 100, height: 50))
button.action = #selector(self.switchToView)
let img = NSImage(named: "wob_icon")
button.image = img
What I'm trying to do is get the image 10pt. from the left side of the button, while centred vertically. So far, the image shows up centered horizontally as well as vertically, but since it doesn't seem like I'm able to define a frame or something like that, I can't really move it.
Is there a way to move the image within it's parent (the button)?
Thanks.
Maybe you could use the imagePosition property on a NSButton? (documented here).
It uses an enum of type NSCellImagePosition (documented here) and allows you to set the image to the left of the text, to the right of the text, above the text, below the text and so on.
You still won't have the opportunity to align things pixel perfect but if you can live with that, then imagePosition seems like the way to go.
Here is how to use it:
let button = NSButton(frame: NSRect(x: 10, y: 10, width: 100, height: 50))
button.action = #selector(self.switchToView)
let img = NSImage(named: "wob_icon")
button.image = img
button.imagePosition = .imageLeft
button.title = "Look left :)"
And that gives me this stunning UI:
Hope that helps you.