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
Related
I'm a beginner with swift and have reviewed this site to understand how importing custom fonts work. I've also reviewed this StackOverflow question which didn't seem to solve my specific issue
Upon reviewing the site, I've added the .tts file Lovelo-Black to the project and plist.
private let loginButton: UIButton = {
let button = UIButton()
button.setTitle("log in", for: .normal)
button.layer.masksToBounds = true
button.layer.cornerRadius = Constants.cornerRadius
button.backgroundColor = UIColorFromHex(rgbValue: 0x00FF9C,alpha: 1)
button.setTitleColor(.black, for: .normal)
button.titleLabel?.font = UIFont(name: "Lovelo-Black") //issue here
return button
Is it possible to make the button use the Lovelo-Black font? Ideally, I would like it to look like this
If it is possible, what should I replace the 8th line with?
Make sure you add the fonts correctly to your project..
check this link
Use the correct font name
button.titleLabel?.font = UIFont(name: "Lovelo Black", size: size)
In swiftUI you can use custom function from Font to initialise and use any custom fonts. have a look at bellow example code.
Font.custom("Lovelo_Black", size: 24)
Which generates bellow result
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.
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
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
I imported 2 *.ttf font files for a normal and bold font into xcode, set everything up in the .plist etc - and I can perfectly view the font in the storyboard now.
Unfortunately attributed textviews as well as labels or buttons will ignore my font when I set it in storyboard mode!
If I generate something programmatically it works fine, e.g. like this:
var testButton = UIButton()
testButton.setTitle("Abbrechen", forState: UIControlState.Normal)
testButton.backgroundColor = UIColor.blueColor()
testButton.titleLabel?.font = UIFont(name: "FaktConPro-Bold", size: 37)
testButton.tintColor = UIColor.redColor()
testButton.frame = CGRectMake(0 , 0, self.view.frame.width, 60)
view.addSubview(testButton)
any hints? does iOS prefer any format?
€dit:setting the labels and textfields to "plain" instead of "attributed" temporarily fixes the problem
This is a bug on Xcode. I already created a radar a couple of month ago. Here: http://openradar.appspot.com/radar?id=5117089870249984