Trouble setting Navigation Bar Font and Size - ios

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

Related

Unable to change UI of MDCTextField i

I am trying to use the MDCTextField with MDCTextInputControllerOutlined of MaterialComponents in my swift project but I facing a major challenge with design adjustments in it. It's not allowing me to change even the height of the textfield as well to centre the text input part. Not even the fonts of placeholder is changing.
Tried following all possible options provided in sdk.
tptf.placeholder = "Name"
tptf.placeholderLabel.textColor = UIColor.green
tptf.placeholderLabel.font = UIFont(name: "TimesNewRoman", size: 14)
tptf.textColor = UIColor.green
tptf.font = UIFont(name: "TimesNewRoman", size: 14)
tptf.clearButtonMode = .unlessEditing
tptf.delegate = self
self.textController = MDCTextInputControllerOutlined(textInput: tptf)
self.textController.textInsets(UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
self.textController.activeColor = UIColor.green
self.textController.floatingPlaceholderActiveColor = UIColor.green
PFA image

Label not displaying full text swift

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

Underline covers text in NSAttributedString

I'm trying to create an attributed string but the underline covers my text instead of appearing behind it:
Is there a way to fix this? I'm using the following code:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 10.0
let attributes = [NSForegroundColorAttributeName: UIColor.white,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleThick.rawValue,
NSUnderlineColorAttributeName: UIColor.red,
NSParagraphStyleAttributeName: paragraphStyle]
let attributedString = NSAttributedString(string: "Story", attributes: attributes)
Thanks!
EDIT:
To give more context:
I'm displaying the attributed string on a UILabel placed in a .xib file:
view.textLabel.attributedText = attributedString
The label has the following font:
System Bold 32.0
I'm running the code on iPhone 6 - iOS 10.3 simulator.
EDIT 2:
I should have mentioned that the label may, at some point, contain more than one line of text. That's why the numberOfLines is set to 0.
EDIT 3:
If anybody encounters this problem -- it seems that there is a lot of difference in how underline is drawn on iOS 9 vs 10 as well as UILabel vs UITextView. I've ended up having to draw the underline myself by subclassing NSLayoutManager.
Yes, there is such problem as you have described. It shows up when you use multiline UILabel, so not only setting numberOfLines to 0, but type more than 1 line in it.
Example
let selectedStringAttributes: [String: Any]
= [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 28),
NSForegroundColorAttributeName: UIColor.green,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue,
NSUnderlineColorAttributeName: UIColor.green]
let label = UILabel(frame: CGRect(x: 100, y: 100, width: 500, height: 100))
label.numberOfLines = 0
label.attributedText = NSAttributedString(string: "String to test underline", attributes: selectedStringAttributes)
And everything will look pretty good.
But if you want to use such text:
label.attributedText = NSAttributedString(string: "String to\ntest underline", attributes: selectedStringAttributes)
or label's width is too short, than:
So the reason for such behaviour is of course bug in NSAttributedString. As it mentioned in radar there is a workaround
You should add this attribute to your NSAttributedString
NSBaselineOffsetAttributeName: 0
And magic will happen.
Instead of using NSAttributedString you can draw border below the label with x space using this.
let space:CGFloat = 10
let border = CALayer()
border.backgroundColor = UIColor.red.cgColor
border.frame = CGRect(x: 0, y: (label?.frame.size.height)! + space, width: (label?.frame.size.width)!, height: 1)
label?.layer.addSublayer(border)
On my machine, showing your attributed string in a black-backgrounded UILabel, it makes a quite nice-looking display:
The red thick underline is nicely separated from the text, and is interrupted to allow the descender of the "y" to pass through it.
NOTE You cannot combine the font of the UILabel (set in Interface Builder) with its attributedText. You must set the entire label's text formatting in the attributedText. So, my code looks like this:
let attributes : [String:Any] = [NSForegroundColorAttributeName: UIColor.white,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleThick.rawValue,
NSUnderlineColorAttributeName: UIColor.red,
NSFontAttributeName: UIFont.boldSystemFont(ofSize: 32)]
let attributedString = NSAttributedString(string: "Story", attributes: attributes)
lab.backgroundColor = .black
lab.attributedText = attributedString
(You will notice that I removed your stipulation of the paragraph line spacing; there is only one line, so this stipulation adds nothing. However, I get the same result even if I restore it.)
So this is my solution to this issue.
I think it is "cleaner" and easier.
Post me if you dont understand :)
class BottomLineTextField: UITextField {
var bottomBorder = UIView()
override func awakeFromNib() {
super.awakeFromNib()
setBottomBorder()
}
func setBottomBorder() {
self.translatesAutoresizingMaskIntoConstraints = false
bottomBorder = UIView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
hasError = false
bottomBorder.translatesAutoresizingMaskIntoConstraints = false
addSubview(bottomBorder)
bottomBorder.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
bottomBorder.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
bottomBorder.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
bottomBorder.heightAnchor.constraint(equalToConstant: 1).isActive = true // Set underline height
}
}

UINavigationItem barButtonItem customView detecting other items then setting width

Here is my NavBar
Now I know how to add a customView on the leftBarButtonItem by using this code
navigationItem.leftBarButtonItem!.customView = someCustomView
As seen on my screenshot here
But the question is, Is there a way that I could detect if there are any navigationItems next to my leftBarButtonItem so I could set the width to be dynamic? instead of overlapping like the screenshots?
Here's more screenshots of my test case
Note: Yes I know how to set the frame by using the code someCustomView.frame = CGRect(.....) also it seems that the UINavigationBar/Item inherits from NSObject rather than UIView
Edit: So apparently after doing much coding, recoding, and research, the only part of the UINavigationBar/UINavigationItem that automatically resizes itself according to the left and right barButtons is the titleView which kind of sucks in a way.
So according to my knowledge, the only way to achieve this:
is by using the titleView property of the UINavigationItem which doesn't answer my question.
If -hopefully- these are the outputs that are looking for:
Then you should set the titleView of the navigationItem:
A custom view displayed in the center of the navigation bar when the
receiver is the top item.
It dynamically handles the width to not overlap.
For example, in the follwoing code snippet, I'll set the value of navigationItem.titleView as a UILabel (and of course you can customize it):
override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 40))
label.text = "Some very long text that should be in the middle of the navigation bar"
label.textAlignment = .center
label.textColor = UIColor.blue
navigationItem.titleView = label
}
The output should be similar to the screenshots above.
Note that "Item 01", "Item 02" and "Item 03" that appear in the screenshots are added directly in the storyboard, there is no code related to adding them.
If you want to let the label to at next of "Item 01" button, simply let it:
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 40))
label.text = "my label"
label.textAlignment = .left
label.textColor = UIColor.blue
navigationItem.titleView = label
Output should be like:
Hope this helped.

TableViewController navigation bar title not bold

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)

Resources