Custom font title for UIButton - iphone

Is there a way to have custom fonts to the title of a button.

The UIButton class exposes a titleLabel property, upon which you can change things like the font, the text color, etc.

Set the font of the button's titleLabel:
myButton.titleLabel.font = [UIFont fontWithName:#"Courier" size:22.0];

You can always render text in a UIImage and then configure the button to use that with setImage:forState:. This allows you to do custom fonts, labels with icons, images, etc.

In case you want to list all available fonts and then select your font, run this Swift code:
UIFont.familyNames().forEach {
print($0)
UIFont.fontNamesForFamilyName($0).forEach {
print(" \($0)")
}
}
It prints all font names for their font families. Then assign the same font name as printed.
button.titleLabel.font = UIFont(name: "OpenSans", size: 20.0)

Related

How can I change the font and color of the Navigation Bar back button

I am trying to change the font and color of the Navigation Bar button.
Ideally, I'd like to change it to black and use a custom font that I already have set up. Is there any way to modify the existing button?
I tried this in my view's init for the font
UINavigationBar.appearance().titleTextAttributes = [.font : UIFont(name: "Georgia-Bold", size: 20)!]
But I think this only changes the title of an inline Nav bar
It's a UIBarButtonItem so set this property.
init() {
UIBarButtonItem.appearance().tintColor = .red
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10)], for: .normal)
}
Or you can use this also,
init() {
let standard = UINavigationBarAppearance()
let button = UIBarButtonItemAppearance(style: .plain)
button.normal.titleTextAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10), NSAttributedString.Key.foregroundColor: UIColor.red]
standard.buttonAppearance = button
}
You can do the method above or:
Go to your Assets -> Set a colour for Accent, although this will affect other components in your app i.e. DisclosureGroup button etc.

Cocoa NSTextField text get cut off in view after changed to monospaced digital font

The text inside NSTextField gets cut off when I have the font changed to monospacedDigitSystemFont.
I have the following code in the viewDidLoad method. I do not understand why it has been cut off since sizeToFit has been called and the intrinsic size should be used.
The label at the bottom looks fine and it is using the default system font.
labelTimeNow.sizeToFit()
labelTimeNow.font = NSFont.monospacedDigitSystemFont(
ofSize: labelTimeNow.font!.pointSize,
weight: .medium
)
labelTimeNow.stringValue = DCClock.getCurrentTimeInFormat("HH:mm")
Reference
How can I set NSTableView column to use monospaced numbers?
Stupid me, I should call sizeToFit after changing the font spacing.
labelTimeNow.font = NSFont.monospacedDigitSystemFont(
ofSize: labelTimeNow.font!.pointSize,
weight: .medium
)
labelTimeNow.stringValue = DCClock.getCurrentTimeInFormat("HH:mm")
labelTimeNow.sizeToFit()

Can't change UINavigationBar Custom Font Color

I am using Storyboard and I want to change the UINavigationBar title color to UIColor.whiteColor() on one of my ViewControllers. Xcode is recognizing the font, but I can't change the color.
I Tried:
changing it with both Storyboard and with code below, but it does not seem to work. I also tried using different fonts and both .otf and .ttf thinking it might be caused by an error with the fonts. So far I can only get a black color with any custom font.
var nav = self.navigationController!.navigationBar
self.navigationItem.title = "HOME"
nav.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
nav.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Impact", size: 30)!]
I found that you need to set the attributes at the same time like this:
nav.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont(name: "Impact", size: 30)!]
You can also set the navigation bar's tintColor property to your desired color.

Hide UIButton title

I have several UIButtons in a scrollview which I use in order to pass certain information. The information is saved in the title of each uibutton and when the button is clicked, it passes its title into the function.
All I want to do is hide the title of the button so you can not see the button. I have them overlaid over images which I use to show buttons. I have the text set to transparent but it still turns white when it is being clicked.
If you include code in your explanation, please explain where it should go.
After IOS7, If you want to just hide the title on titleLabel of a button you can do as follow. This way the title is still there it just makes it invisible. if you do NSLog("%#",button.currentTitle) you will see the title in terminal. Hope this helps.
[button setTitle:#"Button Title" forState:UIControlStateNormal];
button.titleLabel.layer.opacity = 0.0f;
I found only one correct working way:
//hide
yourButton.setTitleColor(UIColor.clearColor(), forState: .Normal)
//show (put your color)
yourButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
using button.titleLabel.hidden = YES will not work (at least on on iOS 7).
I ended up using:
// remove the button since hiding it doesn't work
[button.titleLabel removeFromSuperview];
// put back when you're done
[button addSubview:button.titleLabel];
You can hide the label inside the button:
button.titleLabel.hidden=YES;
or set the button's title to #"" and save the value somewhere else when you want to retrieve it.
I create a subclass of UIButton and override layoutSubviews method. Hiding titleLabel in layoutSubviews method works.
public class LoadingButton: UIButton {
public var isTitleHidden: Bool = false {
didSet {
titleLabel?.isHidden = isTitleHidden
}
}
public override func layoutSubviews() {
super.layoutSubviews()
titleLabel?.isHidden = isTitleHidden
}
}
if wanna hide titleLabel, just set isTitleHidden = true
I could not remove the title from the titleLabel nor the whole view as I needed it for constraints.
I ended up using
isEnabled = false
titleLabel?.layer.opacity = 0
setTitleColor(.clear, for: .disabled)
to hide the title and
isEnabled = true
titleLabel?.layer.opacity = 1
setTitleColor(titleColor(for: .normal), for: .disabled)
to show it again
To hide a title temporary just setTitle to empty string
setTitle("", for: .normal)
the button title label will be hidden but the title will still in the titleLabel, you can return it back using
setTitle(titleLabel?.text, for: .normal)
If you want to temporary hide the title, while you disabling the button, use:
setTitle("Title", for: .normal)
setTitle("", for: .disabled)
Then, button.isEnabled = false, when you want to hide the title.
I got a problem with title, because used an attributed title and nothing above helped. Then i found a workaround:
button.titleEdgeInsets = .init(top: 0, left: shouldHide ? 1000 : 0, bottom: 0, right: 0)
However it has some disadvantages, but fit my needs.
I've come up with this solution, which allows you to set title label text and use it with button image without showing it and not moving button image to the left.
- (void)hideButtonLabel:(UIButton*)buttonInp {
buttonInp.titleLabel.layer.opacity = 0.0f;
uttonInp.titleLabel.font = [UIFont fontWithName:#"Helvetica-Light" size:0.0];
}
You cannot hide UIButton titleLabel using .hidden property. Instead you can do this.
To Hide:
[self.yourButton setTitle:nil forState:UIControlStateNormal];
To Show:
[self.yourButton setTitle:#"Your Text" forState:UIControlStateNormal];
In Swift-
You don't need to hide nor need to make the opacity to 0.0. Swift gave you a simpler way.
Just set the title as nil. In fact, I got the idea from the documentation.
Command click on the setTitle(_:for:) method and you will see-
open func setTitle(_ title: String?, for state: UIControl.State) // default is nil. title is assumed to be single line
So, I just set it to nil.
setTitle(nil, for: .normal)
Swift 5 to hide button label:
myButton.titleLabel?.isHidden = true
Here myButton is a #IBOutlet of the button.

How to add an image/button to a UITextView? With auto line break?

I want to add an image/button to my UITextView and the text should automatically make a line break that the image/button fits.
Second question. I want to make a button called "move image/button" and then the user can move the image/button through the UITextView and the text should adjust.
PS: like in "Pages" for mac or "Word" for Windows
This is decidedly non-trivial. For starters, you can't simply embed a UIImage inside a UITextView and even if you could text wouldn't magically flow around it. What you'll need to do is create your own object based on a UITextView that substantially extends its capabilities to provide such editing tools.
For a heads-up as to what you're getting into you might want to take a look at the example Text Editor source code from the Omni Group.
Use the attributedText property of UITextView with an NSAttributedString with NSTextAttachment with image. Note that the UITextView must be Selectable (in Storyboard).
Swift demonstration:
let attributedImage = NSAttributedString(with: #imageLiteral(resourceName: "myImage"))
let attributedText = NSMutableAttributedString(string: "Hello ")
attributedText.append(attributedImage)
textView.attributedText = attributedText
Where the attributedImage is built from those convenience initializers:
extension NSAttributedString {
convenience init(with image: UIImage) {
self.init(attachment: NSTextAttachment(with: image))
}
}
extension NSTextAttachment {
convenience init(with image: UIImage) {
self.init()
self.image = image
// adjust origin if needed
self.bounds = CGRect(origin: .zero, size: image.size)
}
}
This solution works for: iOS 7.0, macOS 10.11, tvOS 9.0, *.