In my storyboard, the title of the navigation bar in the TableViewController is in bold by default (there are no options to change it). However, when I run the app, both in the simulator and on an iPhone 6S, the title is not in bold. The two screenshots below will show the difference. I did not find any settings to change the font in the interface builder. I am not sure that it was incorrect before updating the environment to iOS 10. No code is changing anything visual, everything is done in the interface builder
try
self.navigationController?.navigationBar.titleTextAttributes =
[NSFontAttributeName: UIFont(name: "your-bold-font", size: 21)!]
Just create a label and add it to your navigationItem:
let title = UILabel(frame: CGRect(x: 0, y: 7, width: 200, height: 30))
title.textAlignment = NSTextAlignment.center
title.textColor = UIColor.white
title.text = "Test heading"
title.font = UIFont.boldSystemFont(ofSize: 20)
let titleView = UIView(frame: CGRect(x: deviceHelper.screenWidth / 2, y: 0, width: 200, height: 44))
titleView.backgroundColor = UIColor.clear
self.navigationItem.titleView = titleView
titleView.addSubview(title)
Related
I set up label programmatically and want to change the text of the label once a button is clicked. However, the label just displays part of the text. For example, I want to change the text from "press to start a trip" to "searching for GPS signal..", only "searching for" displayed even without "..." to indicate that there are more texts.
let startTravelBtn = UIButton(frame: CGRect(x: 40, y: 50, width: 50,
height: 30))
let startTipLabel = UILabel(frame: CGRect(x: 100, y: 50, width: 200, height: 30))
Solution
yourLabel.text = "Lorem Ipsum is simply dummy text of the printing"
yourLabel.sizeToFit()
OR
Set Lines on label to 0
public let descriptionLabel: UILabel = {
let label = PaddingLabel(withInsets: 10, 10, 10, 10)
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont(name: "Helvetica Neue Light", size: 17)
label.adjustsFontSizeToFitWidth = true
label.numberOfLines = 0
label.sizeToFit()
(label.lineBreakMode = .byWordWrapping) - doesn't also work
I tried many methods As a result, only this method
(label.adjustsFontSizeToFitWidth = true) helps to avoid hiding the text.
You can do this by setting numberOfLines to 0
startTipLabel.text = "longer text what you want"
startTipLabel.numberOfLines = 0
startTipLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
I'm trying to dynamically set my navigation bar's text so that the header text always fits. I'm acccomplishing that like this:
// Pet's Day text "Joy's Day"
if let range = currentPet.range(of: "_") {
let petsName = currentPet[range.upperBound..<currentPet.endIndex]
let deviceWidth = UIScreen.main.bounds.size.width
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: deviceWidth, height: 40))
titleLabel.text = "\(petsName)'s Day"
titleLabel.font = UIFont.systemFont(ofSize: 30)
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = UIColor.white
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.minimumScaleFactor = 0.5
titleLabel.textAlignment = .center
self.navBar.topItem?.titleView = titleLabel
}
However, as seen by this picture, this lowers the header text below its natural height:
The navigation bar on the left is from one of my app's other views, while the one on the right is the one I'm setting.
Both of these navigation bars are navigation bars that I've dragged in and made the prompt equal to an empty string to increase their height:
Can anybody please help me implement my code above so that it doesn't drop down the header text?
**Edit: Here are screenshots from Xcode's debug hierarchy:
This is the normal navigation bar:
This is the one I'm setting:
You need to set the baseline, by default is Align Baseline, you need to change to Align Centers
Code example:
class TestViewController: UIViewController {
#IBOutlet weak var navBar: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
// Change the height of the navbar
self.navBar.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 68)
let deviceWidth = UIScreen.main.bounds.size.width
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: deviceWidth, height: 40))
titleLabel.text = "Log Events" // Change to Joy's Day and check the result
titleLabel.font = UIFont.systemFont(ofSize: 30)
titleLabel.baselineAdjustment = .alignBaselines
titleLabel.textColor = UIColor.white
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.minimumScaleFactor = 0.5
titleLabel.textAlignment = .center
self.navBar.topItem?.titleView = titleLabel
}
}
I set a button to right side of navigation bar:
my view controller:
I want to set a burger icon,red cycle and a label to this button. like this:
my code:
self.navigationController?.navigationBar.barTintColor = self.utilities.hexStringToUIColor(hex: "#00b8de")
var imageview2 = UIImage(named: "menulogo")
imageview2 = imageview2?.imageResize(sizeChange: CGSize(width: 25, height: 25))
btnMenu.setImage(imageview2,for:UIControlState.normal)
btnMenu.setTitle("", for: .normal)
// setup the red circle UIView
let redCircleView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
redCircleView.backgroundColor = UIColor.red
redCircleView.layer.cornerRadius = view.frame.size.width / 2
// setup the number UILabel
let label = UILabel(frame: CGRect(x: 30, y: 0, width: 20, height: 20))
label.textColor = UIColor.white
label.font = UIFont.systemFont(ofSize: 10)
label.text = "16"
// adding the label into the red circle
redCircleView.addSubview(label)
// adding the red circle into the menu button
btnMenu.addSubview(redCircleView)
with above codes I have three problems:
my burger image isn't in most right side of navigation.
my cycle view doesn't show
my icon is white, but it shows blue!
btnMenu is my button in navigation.
I have use this library for set badge on navigation button.
MIBadgeButton is badge button written in Swift with high
UITableView/UICollectionView performance.
https://github.com/mustafaibrahim989/MIBadgeButton-Swift
I know this is very simple but I have looked everywhere and cannot find the answer to this. What I have found tells me to write this code, or similar code. And I have tried everything. I am using a Navigation Controller and I want to change the font and size of the text. I have not been able to change the font or size of text at all. Any help is very much appreciated!
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Apple SD Gothic NEO", size: 20)!]
self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
A possible solution is replace the title label with a new one.
Example:
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 20, height: 50))
titleLabel.text = "Title"
titleLabel.font = UIFont.boldSystemFontOfSize(17)
self.navigationItem.titleView = titleLabel
I have searched a lot on stackoverflow and I havent been able to find solution for my problem which is that I need to put in navigationBar in middle section image and text next to each other but apparently I was able only to place just image or just text
Here is my code
let nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Default
nav?.tintColor = UIColor.blackColor()
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imageView.contentMode = .ScaleAspectFit
let image = UIImage(named: "icon_heart")
imageView.image = image
self.navigationItem.title = "App name"
self.navigationItem.titleView = imageView
I am aware that it on this way it accept only image which is titleView but I cant think of some normal solution and dont know is it even possible. Also if that is not possible how can I make then blank image and draw that icon image and text next to each other and append then that new image.
Edit:
Based on awph answer I have added code like this and it works fine for my case where I have always same characters in title
let screenSize: CGRect = UIScreen.mainScreen().bounds
let nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Default
nav?.tintColor = UIColor.blackColor()
let imageView = UIImageView(frame: CGRect(x: screenSize.width / 2 - 40, y: 15, width: 20, height: 20))
imageView.contentMode = .ScaleAspectFit
let image = UIImage(named: "icon_heart")
imageView.image = image
nav!.addSubview(imageView)
// label on top of the application that contain word
var label = UILabel(frame: CGRectMake(0, 0, 0, 0))
label.center = CGPointMake(screenSize.width / 2 - 15, 17)
label.textColor = UIColor.blackColor()
label.font = UIFont.systemFontOfSize(14)
label.textAlignment = NSTextAlignment.Center
label.text = "App Name"
label.sizeToFit()
nav!.addSubview(label)
You right, as said in documentation:
// Custom view to use in lieu of a title. May be sized horizontally. Only used when item is topmost on the stack.
What you need to do is to create an UIView, add an UImageView (that contains your image) as subview (view.addSubview(imageView)) and then add a UILabel on it view.addSubview(label).