UIButton not highlighting the whole button when ImageEdgeInsets are set - ios

I'm trying to have an UIButton with an image in the middle of it. I want this button to have edge insets of 10px. When I set these, I have the correct visual result but I can only click in a little area of this button.
How can I have the same visual but be able to click on the whole button ?
Here is my code :
self.descriptionViewMaleButton.setImage(UIImage(icon: "icon-male", backgroundColor: UIColor.whiteColor(), iconColor: UIColor.blackColor(), iconScale: 1.0, andSize: CGSize(width: 50, height: 50)), forState: UIControlState.Normal)
self.descriptionViewMaleButton.layer.borderColor = UIColor.grayColor().CGColor
self.descriptionViewMaleButton.layer.borderWidth = 1.0
self.descriptionViewMaleButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)
And the visual result :
EDIT : I am able to click on the whole button but it is highlighting only the image.
EDIT : I "fixed" that by implementing the highlighting myself. Anyway, if someones know more about this issue, I would be glad to know about it.

Related

Swift FontAwesome 5 Pro icons don't show programmatically

In my swift app, I am using the pro version fontawesome. I got the name
Font Awesome 5 Pro
== FontAwesome5Pro-Light
and define it in a button as
var button = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
button.titleLabel?.font = UIFont(name: "FontAwesome5Pro-Light", size: 20)
button.setAttributedTitle(NSMutableAttributedString(string: "\u{f13d}"), for: .normal)
button.setTitleColor(.systemBlue, for: .normal)
"\u{f13d}" is the unicode anchor icon Cheatsheet for FontAwesome
But the icon just comes out with the question mark in the square. If I create a button in the storyboard, set the font, create an outlet and then set the unicode from code behind it displays correctly.
I'm confused as to the cause. Any ideas?
Thanks ^.^
Setting the attributed title cancels the titleLabel?.font. Set the font of the attributed string.

FontAwesome icon displayed as question mark

I am trying to set the button text for the following :
The first step I followed is to include the font file in the project. I then referred it in the Info.plist.
I copied the icon from the cheatsheet :
Then I set the font for the button in IB as follows :
As a result, I get a question mark on the button (both in IB and on running).
Just for cross checking, I set some other icon instead of this. It worked fine.
Where could the mistake be ?
You are using the wrong font. The one you want is FontAwesome5FreeSolid (fa-solid-900.ttf).
This works fine on my machine:
let s = "\u{f362} Convert"
let ss = NSAttributedString(string: s, attributes:
[.font : UIFont(name:"FontAwesome5FreeSolid", size:17)!])
let b = UIButton(type: .system)
b.frame = CGRect(x: 40, y: 40, width: 200, height: 200)
b.setAttributedTitle(ss, for: .normal)
self.view.addSubview(b)
Result:
In IB set only the font name and size , in code use something like this
self.backBu.setTitle(String(format:"%C",0xf053), for: .normal);
replace f053 with unicode that you want , also make sure that target memberShip is checked for the font resource and you add it to info.plist

UIButton titleLabel make as a circle

Hello I want to make UIButton title label as a circle. So I did in this way.
btnAttachments.setTitle(strcount, for: .normal)
btnAttachments.setTitleColor(UIColor.white, for: .normal)
btnAttachments.titleLabel?.backgroundColor=com.getRedcolor()
btnAttachments.titleLabel?.font=UIFont.init(name: "Bariol", size: 15)
btnAttachments.titleLabel?.layer.cornerRadius=(btnAttachments.titleLabel?.frame.width)!/2
But it doesn't make it as a circle. It looks like this code doen't affect on the titleLabel
UPDATE
This is what I want to make
Please help me
Thanks
well first you have to verify that the uibutton has its clipsToBounds true, but incase that s already set and the problem still there, then you probably trying to achieve this in the viewDidLoad which "should" work, but for some reason in some of the xcode 8 versions in the viewDidLoad the layout are still aint final (objects doesnt have their true dimensions set yet) you can verify this by logging the uibutton frame right before you assign the corner radius if you find some incoherent that this is probably the issue , incase this is the issue then you just need to move your code to viewWillAppear
You are missing to set MasToBounds
btnAttachments.titleLabel?.layer.masksToBounds = true;
add this you code will work fine ,
You should set corner redius in viewDidLayoutSubViews or drawRect(usually in UIView class)
i hope this will help u
Use the following code to get desired result:
let btnAttachments = UIButton(type: .custom)
btnAttachments.setTitle(strcount, for: .normal).setTitleColor(UIColor.white, for: .normal)
btnAttachments.frame = CGRect(x: 160, y: 100, width: 50, height: 50)
btnAttachments.titleLabel?.backgroundColor=com.getRedcolor()
btnAttachments.titleLabel?.font=UIFont.init(name: "Bariol", size: 15)
btnAttachments.layer.cornerRadius = 0.5 * button.bounds.size.width
btnAttachments.clipsToBounds = true

How to remove right padding/margin/space from a custom bar button item which has an image?

I've been trying to find a solution to this problem for the past two days and still nothing I have tried works for me. Some solutions I have tried either stretch or completely mess up my custom image but nothing removes the right padding. Here is my code below as well as the result. Notice how the right bar button has a bigger space than the left system one.
var saveButton: UIButton = UIButton(frame: CGRectMake(0, 0, 32, 32))
saveButton.addTarget(self, action: "saveAction:", forControlEvents: .TouchUpInside)
let img = UIImage(named: "save")
saveButton.setBackgroundImage(img, forState: .Normal)
var saveBarButton: UIBarButtonItem = UIBarButtonItem(customView: saveButton)
self.navigationItem.setRightBarButtonItem(saveBarButton, animated: false)
Alright guys, I've found a solution that works pretty well for me. Instead of creating and setting the bar button item in code I added one in storyboard and set it's left margin in size inspector to -6 (or whatever value you like) and it's right margin to 6 (again whatever value you prefer note that the two values must be the same values but one is positive and the other is negative). Then I set it's image to the image I desired in the attributes inspector. If for some reason you want to change the image in code you can do so like this:
barButtonOutlet.image = UIIMage(named: "yourImage") as UIImage?
Hope this helps some of you out.
This is my solution. Try..
rightBtn.imageInsets = UIEdgeInsets(top: 0, left: -13.0, bottom: 0, right: 13.0)
self.navigationItem.rightBarButtonItem = rightBtn

UIButton sizing wrong with image and title inset

It does not seem like UIButton instrinsicSize and/or sizeToFit takes a title left edge inset into account, or something is messed up with my expectations.
To demonstrate, I have two Custom type buttons in a view, both with the title "Button". I want to add an image to the button to the left of a title.
var image = UIImage(named: "circledPlay")
image = image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
self.button1.setImage(image, forState: UIControlState.Normal)
self.button1.invalidateIntrinsicContentSize()
self.button1.sizeToFit()
self.button2.setImage(image, forState: UIControlState.Normal)
self.button2.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0)
self.button2.invalidateIntrinsicContentSize()
self.button2.sizeToFit()
The result is as follows:
Note the second button is being truncated.
So my question would be if anybody has seen this before (and hopefully has a solution) or am I confused and this is behaving as expected (and hopefully knows the right way to do this)?
As it says in the documentation, for titleEdgeInsets: "The button does not use this property to determine intrinsicContentSize and sizeThatFits:".
So, setting the titleEdgeInsets just moves the title label, but doesn't affect the size of the button. If you want the button to have more padding around the content, set the contentEdgeInsets as well. I don't think you need to call either sizeToFit, or invalidateIntrinsicContentSize (but 'm not sure about that).
You can use the contentEdgeInsets and titleEdgeInsets to implement that.
button2.setImage(image, for: .normal)
button2.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
button2.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10)
button2.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, -10)
button2.setTitle("Button", for: .normal)
button2.sizeToFit()

Resources