How to use the .monospaced system font design - ios

I am attempting to use a system font and apply the monospaced design, without luck. I can successfully make the text monospaced using the custom font function and passing in Courier and a size, but this is not idea because then the font size is fixed.
VStack {
Text("lmlmlmlm 12345678")
Text("lmlmlmlm 12345678")
.font(Font.system(.body, design: .monospaced))
Text("lmlmlmlm 12345678")
.font(Font.custom("Courier", size: 18))
}
How do I get the system font to work with the .monospaced design? I think it might be a bug with .monospaced, because the .serif option does modify the text as expected.

It seems .monospaced font only applies when given a fixed size:
Text("monospaced")
.font(.system(size: 14, design: .monospaced))
This won't work given a dynamic text style such as body. But as you've also mentioned it works fine for other fonts so this is probably a bug in Xcode 11.0 beta and hopefully will be fixed in next releases.
Update:
This issue was fixed with Xcode 11 beta 3. The following code works now:
Text("monospaced")
.font(.system(.body, design: .monospaced))

In case you want to make only digits monospaced, you might try using something like this:
Text("0123456789")
.font(Font.system(.body, design: .monospaced).monospacedDigit())
This does not circumvent the obvious bug of Xcode 11.0 beta, however. Letters are still not rendered monospaced.

Related

System font (SF Pro?), .rounded when using .custom — for iOS 15

until now I have been using fixed font sizes, like this:
Text("Example")
.font(.system(size: 24 weight: .medium, design: .rounded))
I am now trying to implement dynamic font sizes, so it scales with different accessibility modes. I found this, which says "If you’re using iOS 14 or later you’ll find your custom fonts scale automatically with no further work from you". This does work for me, but now I can't find a way to make the font .rounded (as .fontDesign is iOS 16+, and I am targeting iOS 15).
Also, what should I use as the first parameter (font name) for the system font? I tried UIFont.systemFont, etc, which didn't work. I'm not even 100% sure SF Pro is the correct name here:
Text("Example")
.font(.custom("SF Pro", size: 24))
Perhaps I'm doing this completely wrong in some other way that is not clear to me. Is this the correct approach to dynamic font sizing for accessibility — to use .custom, specify a default size (for the default type size), and let iOS scale this accordingly?
I realised that if I remove the .weight parameter and use .largeTitle, etc, it works — and I can set the weight separately:
.font(.system(.largeTitle, design: .rounded))
.fontWeight(.bold)
How do I set the default sizes of .largeTitle, .body, etc?

Apps complied with xCode 11.7 but running on iOS 14+ issues

So I made the "mistake" of upgrading one of my devices to iOS 14 (currently iOS 14.0.1), which I found out that now I can NO longer deploy to that device via xCode, since I am still developing with xCode 11.7 and not the newest version of xCode 12.
I am still able to deploy to the device with iOS 14, because of utilizing testflight, regardless of it being on the newest iOS, but I am getting some really weird results!!
This is what the screen SHOULD look like, which is running on a device with iOS 13.7:
versus the result when the device is on the newer iOS 14.0.1 version:
Has anyone else witnessed this!?
Couple of questions:
How does the text go from black to blue!?
The main image is all blue vs rendering the true image!?
In the case of the picker for "Origin" the image of the country flag is also all blue vs rendering correctly?
Is this the expected behavior of any app that is uploaded for sale in the iTunes Store which is compiled under an older version of xCode, my case xCode 11.7, vs the newest version?
I mean in the instance any person downloads the current version of the app created in an older version of xCode, but that user has the newest version of iOS on their phone, will the app become all messed up like that!? If so, what was apple thinking!?
Can anyone else provide ways they got around it, or what they experienced?
*** UPDATE ***
Providing the code for the text fields and pickers:
Here is the "default" Textfield:
TextField("Cigar Name", text: $model.cigarName)
Here is the code for the custom picker:
NavigationLink(destination: CountryPicker(cCode: $countryCode, cName: $countryName)) {
Text("Origin")
if countryCode != "" {
Spacer()
HStack() {
Image(uiImage: UIImage(named: "CountryPicker.bundle/Images/\(countryCode)")!)
.clipShape(Circle())
Text(countryName)
}
}
}
Code for "default" picker:
Picker(selection: $selectedCigarStrength, label: Text("Strenght")) {
ForEach(0 ..< cigarStrength.count) {
Text(self.cigarStrength[$0])
}
}
*** Latest Update ***
So I got my new MacBook and d/l xCode 12 for deployment to iphone with iOS 14.0.1 and this is what the screen shot looks like in comparison:
As you can see it looks way different!!! Good news is that the blue images went back to rendering the correct images in the sheet.. bad news is that the text is still blue!? I guess developers will have to strictly specify the text color going forward!? Which is weird... if no color is declared I would imagine it should stay .black by default... as it did in all other versions of xCode!?
Notice too that the form does not extend to the edge of the screen anymore and the date picker has really changed (which I actually like, finally something they actually improved in the new OS).
Me2 but I do not have a solution. I switch back from xcode 12 to xcode 11.7
Based on the issue perhaps it should be the defaults textColor property changed by the system, if possible, please show the code about the textColor for UILabel.
It would seem that iOS 14, for me 14.0.1, has serious issues with colors!?
As noted above, now it would seem that you need to be explicit with the foregroundColor for Textfields and text alike and "force" them to a default color of black, as that used to be default... not sure why they changed that!
The same thing goes for Navigation titles (navigationBarTitle), if you move from one tab that utilizes the title to another tab which does not require it, it will not remove the navigation bar title in the new view for some reason, UNLESS you "trick" the system with navigationBarTitle("") other wise force the blank title.
Not sure why apple decided that everything needs to be explicit vs implicit as it was in the previous xcode and ios versions!?
Another, which I think is a biggest issue, I have uncovered is the evaluation of the Nil-Coalescing Operator.
I have the following condition:
Button(action: {
if (self.viewRouter.currentView != "menu") {
self.viewRouter.currentView = "menu"
}
}) {
Image("menu")
.renderingMode(.template) //**** REQUIRED!!!!
}
.foregroundColor(self.viewRouter.currentView == "menu" ? .blue : .gray)
This simple condition states that if the view currently clicked on is the "menu" then the color of the button should be blue vs if its not and a different view is being displayed it should be gray... the end result is that the button is black and no matter which I select in the tab bar they all remain black and not not change color as they do on my device that i am running iOS 13.6!?
*** UPDATE ***
Again ios 14 became a lot more explicit!! you need to add the .renderingMode(.template) in order to achieve the same result in the pre-ios 14 era.

iOS 14 has Changed (or broken?) SwiftUI GeometryReader

As of today's release of iOS 14.0 my iOS code, which depends heavily on GeometryReader for layout, is no longer working well. I.e, layout has been kind of randomized.
In the Xcode debugger I have compared the same code running on iOS 13.6 vs iOS 14.0 and see that the problem is that the GeometryProxy structure is not being initialized in iOS 14.0.
In the example code below the variable g, a GeometryProxy, has valid width and height values in iOS 13 but is all zero's in iOS 14. (This is just one of the simpler uses I make of GeometryProxy values -- I would appreciate if nobody points out the obvious fact that this snippet could be accomplished much more easily in another way.)
Is this a change to the GeometryReader or a bug? Anyone have a workaround? Did I make a huge mistake depending on GeometryReader for dynamic layout?
struct TextView: View {
#EnvironmentObject var data: RecorderData
let m = MusicalNote()
var body: some View {
GeometryReader { g in
Stack(alignment: .leading) {
Text(self.data.curNote.getString())
.font(.system(size: g.size.height / 2.4))
.padding(.bottom)
Text(self.data.fdata.fingerings[self.data.curRecorder.id] ![self.data.curNote.seq] ![self.data.curFingType] ![self.data.currentFingeringChoice].comment)
.font(.headline)
}
Spacer()
}
}
}
It's definitely changed - this post sums it up: https://swiftui-lab.com/geometryreader-bug/
GeometryReader now lays out its content different to how it used to. It used to centre it horizontally and vertically in the parent but now it aligns it to the top-left.
My app looks awful - what's frustrating is it's not clear if this is new behaviour and is as-designed, or whether it's a bug and will be fixed. I'm trying to go through my app and manually apply offsets but the dynamic nature of layouts using GeometryReader means it is not always easy.
Editing my previous answer as its now obviously bad advice
From https://developer.apple.com/documentation/xcode-release-notes/xcode-12-release-notes
Rebuilding against the iOS 14, macOS 11, watchOS 7, and tvOS 14 SDKs changes uses of GeometryReader to reliably top-leading align the views inside the GeometryReader. This was the previous behavior, except when it wasn’t possible to detect a single static view inside the GeometryReader. (59722992) (FB7597816)
Which says, to me at least, that the previous behaviour was buggy by their standards - despite it matching the conventions for other layout containers like VStack. And that this behaviour is the new normal.

Xcode 8 custom font doesn't show up in interface builder

I am trying to use custom font (Bebas Neue) in my iOS application. The steps I took are:
Copy the .otf files to the project.
Confirm the .otf files have set the project as target.
Added the .otf files in 'Fonts provided by application' in plist.
In Build Phases, the .otf files are in 'Copy Bundle Resources'.
Install the font on my Mac.
Try to print out all fonts available but I can't see my custom font.
The code I used:
for name in UIFont.familyNames() {
println(name)
if let nameString = name as? String
{
println(UIFont.fontNamesForFamilyName(nameString))
}
}
Trying to set the font in code and it worked.
The code I used:
textLabel?.font = UIFont(name: "BebasNeueRegular", size: 14)
But I can't set it in interface builder. Any idea?
Screenshots:
Try Below Steps: Code tested in Swift 3.
Step 1: Add Your custom font into your project( Make sure Add to Target
ticked).I am using "PermanentMarker.ttf","Pecita.otf" and "AROLY.ttf" font as a test font.
Note: Supporting font Type ttf and otf (Both font types should work)
Step 2: Modify the application-info.plist file.
Add the key "Fonts provided by application" in a new row and add "PermanentMarker.ttf" as new item in the Array "Fonts provided by application".
Your plist should looks like this
Now the font will be available in Interface Builder. To use the custom font in code we need to refer to it by name, but the name often isn’t the same as the font’s filename
Now, You can access the Custom Font from your viewController. I am testing the font by placing a UIlabel to the Storyboard like below.
Update 2: Working Solution
After, imported your custom font and updated your plist.selectlabel from your storyBoard,goto Attributes Inspectorunder Label>Text type> select to Attributed and choose your custom font from the list.
Output:
Update 1
If your custom font still not listed in Xcode font list.check the related link to your issue
http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
custom font not displaying on some simulator
Note: Still,You can assign BebasNeue or custom font programatically to your label or button etc. even its not showing in your interface Builder.If you having trouble setting font to your object programatically.try below method.
Assign font to UILabel:
label?.font = UIFont(name: "BebasNeue", size: 35) // Set to any size
Assign font to UIButton:
button.titleLabel?.font = UIFont(name: "BebasNeue", size: 35)
Assign font to UITextField:
textField.font = UIFont(name: "BebasNeue", size: 25)
Assign font to UINavigationBar:
navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "BebasNeue", size: 25)!, NSForegroundColorAttributeName: UIColor.red]
Assign font to UIToolBar:
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "BebasNeue", size: 25.0)!], for: UIControlState.normal)
Output:
Its Easy and simple now- Tested in Xcode 10 and swift 5
Steps to add font to your Xcode
Select your UILabel, UITextField or whatever then under fonts section and follow
Step 1
Select settings menu from left corner of font selection screen. And choose font manager option.
Step 2
Click on the add button as marked below.
Step 3
Select the folder that contains your font. It will be loaded into XCode fonts list.
Steps to add fonts to your project
Don't forget to add description to your plist with the key Fonts provided by application and put font files inside copy bundle resources under project target settings.
Yes! thats it.. njoy..
When using the same font (Bebas Neue), I experience the exact same problem: the font does not show up in the font list for Plain controls.
It does for Attributed controls, as described in Update 2 in Joes post, but then you're actually changing the attributedText instead of the regular text, which may lead to unwanted behavior. For example, you'll have to fiddle with NSMutableAttributedString to change the text or text color at runtime.
I did some investigation on this issue. I used FontLab Studio to modify the font and do some tests. I used Bebas Neue version 1.400 (dated September 15, 2014) from dafont.com.
Maybe the font file was somehow corrupted. Re-saved as .otf and .ttf: Didn't work.
Maybe the meta data of the font were corrupted. Copied all the glyphs to a new font and named that Bebas Neue: Didn't work.
I renamed the font from Bebas Neue to BN: It works! But why?
Maybe you can't use "Neue" in a font name (since Helvetica Neue was the iOS system font up to iOS 8). Renamed the font to Test Neue: Still works.
Maybe you can't use "Bebas" in a font name then? Renamed the font to Bebas: Still works.
What... Just to be sure, I changed the font name back to Bebas Neue: Doesn't work again.
I also tried BebasNeue: Also didn't work.
Then I changed the name to Bebas Neue Whatever: It works.
I really do not understand why this is happening.
Doesn't Apple or Xcode want you to use "Bebas Neue" or "BebasNeue" in the Interface Builder?
I had the normal Bebas Neue installed. Maybe the reference got corrupted?
I really can't tell. Maybe someone on a clean system can try if Bebas Neue works in their Xcode.
Anyway, I achieved my goal: being able to use Bebas Neue (I named the final version "Bebas Neue MyAppName") for Plain styled labels and buttons, so I can design my app as-is and don't have to fiddle around with attributed strings.
Finally, I found the root cause of issues with Bebas Neue fonts.
I don't remember where I have downloaded the fonts, but Bebas Neue fonts are inconsistent.
Download i.e. app called Typelight
Open not Regular, but i.e. Bold font with Typelight.
You need correct most of the Bebas Neue fonts.
maybe there is a difference between folder name and font name the solution would be:
Print all default fonts names.(without custom fonts)
Add custom fonts to project.
Print all fonts names.
and after that compare fonts names.
func printFontsNames() {
let fontFamilyNames = UIFont.familyNames
for familyName in fontFamilyNames {
print("------------------------------")
print("Font Family Name = [\(familyName)]")
let names = UIFont.fontNames(forFamilyName: familyName )
print("Font Names = [\(names)]")
}
}
You can actually just go into attributed fonts, click the custom font window. There click the gear in the upper left and click on the option manage fonts. A window should pop up and on the top and there should be a plus sign to allow you to add fonts. Just choose the custom font you downloaded like xyz.tff and then it should be added.
Important to note is that ttf file should be referenced in the p-list as well as in your project directory!
TLDR
Create a new user account in Mac OS and see if you are able to have success in the new account BEFORE going through all the steps that I went through below. In hindsight, I probably could have just performed steps 7 and 11 and saved hours of hassle.
Problem
After begrudgingly upgrading to Catalina from Mojave so that I could run the latest and not greatest version of Xcode (11.5), I was no longer able to select a custom font from the font picker for PLAIN text controls within interface builder.
As others have mentioned, attributed text works fine, because it uses the systems font picker instead of the "black box / where did these fonts come from???" picker that comes up when using plain text.
Failed Steps
I tried the following steps with no success:
Re-add the font files to the project in hopes that Xcode would fix
the link to the font table that the picker is loading from (still
have no idea why there are certain fonts in the picker and where it
is getting them from).
Reset the Mac OS font caches using several terminal commands in hopes that the picker is loading the list from a corrupt font cache in the OS
Reset the system fonts by choosing File > Restore Standard Fonts in the Font Book app
Screamed at the computer and cursed Apple for what they have become
Tried installing the fonts in the OS to see if that resolved anything, because I remembered it worked a couple years ago when I ran into something similar.
Searched the internet for any clues and tried virtually everything that seemed reasonable.
Created a brand new simple project ... still could not select a custom font ... ruh oh
Bring in the Big Guns
Cursed myself for not performing a CLEAN install of Catalina and decided to reinstall Catalina from scratch.
Realized that I made a mistake using Time Machine to backup my data before re-installing, because it would not let me bring in just my data files. The Migration Assistant requires the Library data to be imported (option was grayed out when restoring), which I did not want to do ... cursed Apple and myself again because I knew this was gonna bite me in the ... as I suspected there was garbage in the profile that was causing this (I was right ... see below)
Tried to select the font in Xcode while running under the IMPORTED account from Migration Assistant ... SAME PROBLEM ... could not select a custom font
SUCCESS!!!
Created a new user and created a new simple project under that account. BINGO!!! I could select a font now!!! Somehow my user account was corrupted during the upgrade to Catalina
It was an old bug since Xcode 3.x:
there is a bug/known issue with IB on XCode 3.x with non-standard fonts and Interface Builder. Custom fonts generally need to be set programatically (via [UIFont fontWithName:])
There are some work around like this with User Defined Runtime Attribute and categories .
I had similar problem
added custom font and also assign it to UILabel all working fine but after a few times later it's wont work as expected.
Trying all solution but nothing work for me. So here is my solution, again going to previous UILabel (already have newly added custom font) select->label->select newly added font. Check other UI controls Font.
now it's working fine.
Hope this help newDevs...
This is still a recurring issue for me with Swift 4.2, even without making any changes that would cause it.
Removing the font files from the Xcode project and adding them again fixes the problem (until next time).
In my case, a font family was missing in Interface builder, because one of the fonts from that family was not added to the project, but was indicated in the info.plist file.
Generally removing all my custom fonts from the project and adding them again - fixed the issue.

SF UI Display doesn't work on Xcode?

I am using the SF UI Display for iOS on Xcode and I chose the Ultralight option, and this is what shows up on the storyboard (the correct version)
This is what shows on the simulator when I run it, it looks much bolder and doesn't seem right...
Since San Francisco is the System Font, no need to choose custom and use San Francisco, just use system.
If it's a button, try this
[button.titleLabel setFont:[UIFont systemFontOfSize:size weight: UIFontWeightUltraLight]];
If it's a label, try this:
[label setFont:[UIFont systemFontOfSize:size weight: UIFontWeightUltraLight]];
In each of those cases, replace "size" with your desired font size.
EDIT: For Swift (labels), refer to this StackOverflow post
EDIT 2: For Swift (buttons), refer to this StackOverflow post

Resources