NSImageView being clipped off - swift

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.

Related

insertSubview() covers the whole Mainview even though inserted at 0

I insert a UIView into a UIButton with following code:
let backgroundView = UIView()
backgroundView.backgroundColor = .red
backgroundView.translatesAutoresizingMaskIntoConstraints = false
blueButton.insertSubview(backgroundView, at: 0)
NSLayoutConstraint.activate([
backgroundView.leadingAnchor.constraint(equalTo: blueButton.leadingAnchor),
backgroundView.trailingAnchor.constraint(equalTo: blueButton.trailingAnchor),
backgroundView.topAnchor.constraint(equalTo: blueButton.topAnchor),
backgroundView.bottomAnchor.constraint(equalTo: blueButton.bottomAnchor)
])
The promlem is that the backgroundView covers the whole button and I only see the red field instead of the button (its an image) with a red background (my subView).
This is the storyboard part of my image. I don´t do something else in the code with this buttons with the exception of trying to put a subView into:
My goal is to have a circle background like in the picture below. For example the red circle button has a rounded red (but brighter than the buttons red) backgroundView.The standard background of the button should stay the same.
I also tried to send the subView into the background but it didn´t work for me:
blueButton.sendSubviewToBack(backgroundView)
I reduced the alpha value of the subview to see if the button image is still there.
for view in blueButton.subviews {
view.alpha = 0.5
}
And it is:
How can I solve this problem?
Do not inject extra subviews into a button. If the goal is to make the background color of the button red, set its backgroundColor to .red. If the goal is to put something behind the button image, that is what the backgroundImage is for. Or just make the button image look like the image you really want.
I made this button:
It looks a lot like your picture. Here's the code:
let im = UIGraphicsImageRenderer(size: CGSize(width: 30, height: 30)).image {
ctx in
UIColor.red.withAlphaComponent(0.2).setFill()
ctx.cgContext.fillEllipse(in: CGRect(x: 0, y: 0, width: 30, height: 30))
UIColor.red.setFill()
ctx.cgContext.fillEllipse(in: CGRect(x: 5, y: 5, width: 20, height: 20))
}
b.setImage(im.withRenderingMode(.alwaysOriginal), for: .normal)
I think that's a lot better than messing with unwanted illegal subviews.

How to access TextField label frame?

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

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

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.

Adding text to image not in the right position Swift

I use this code to add text from textview to Image
func textToImage(drawText: NSString, inImage: UIImage, atPoint: CGPoint) -> UIImage{
// Setup the font specific variables
var textColor = UIColor.whiteColor()
var textFont = UIFont(name: "Helvetica Bold", size: 12)!
// Setup the image context using the passed image
let scale = UIScreen.mainScreen().scale
UIGraphicsBeginImageContextWithOptions(inImage.size, false, scale)
// Setup the font attributes that will be later used to dictate how the text should be drawn
let textFontAttributes = [
NSFontAttributeName: textFont,
NSForegroundColorAttributeName: textColor,
]
// Put the image into a rectangle as large as the original image
inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height))
// Create a point within the space that is as bit as the image
var rect = CGRectMake(atPoint.x, atPoint.y, inImage.size.width, inImage.size.height)
// Draw the text into an image
drawText.drawInRect(rect, withAttributes: textFontAttributes)
// Create a new image out of the images we have created
var newImage = UIGraphicsGetImageFromCurrentImageContext()
// End the context now that we have the image we need
UIGraphicsEndImageContext()
//Pass the image back up to the caller
return newImage
}
I download image from server, and add text for this. I already resize the image before I add text for that to fit the device i am using, in this case is Iphone 6s, 375*667
When I add text from textView which is red in the picture to my image, it isn't in the right position. It is always pushed to the right without reason.
Does anyone know about this problem ?
Thank you very much !!!
Imagine you have a square. If you set both the X and Y to 0, it won't place the middle of the square to the x: 0 and y: 0. It will place the top left of that square to that coordinate. Your issue is that when you call your function, it's placing the top left of the text to the point of where you tapped. You have to minus half of the texts width for the X and half of the texts height for the Y. It should look something like this:
func textToImage(drawText: "Hello, inImage: ***YOUR_IMAGE***, atPoint: CGPoint(x: point.x - (text.frame.width / 2), y: point.y - (text.frame.height / 2))