Goal: use a custom Font on SwiftUI, targeting MacOS.
Problem: On iOS, custom Font works fine in SwiftUI:
But on MacOS, it doesn't:
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
.font(Font.custom("SourceCodePro-ExtraLight", size: 40))
Text("Hello, world!")
.font(Font.custom("LobsterTwo", size: 40))
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Tried: I made sure that both fonts are added to Info tab on corresponding iOS and MacOs targets:
Seems to be a problem with SwiftUI using UIFont under the hood, and a special NSFont would be needed...
Any help is much appreciated!
Solved by adding this line to the plist file, and a "." as the value
Related
When having a view inside a GeomtryReader and setting the background to a color. It ignores the vertical safe area and stretches beyond it. Only when the view is at the vertical edges.
It doesn't only happen on Preview, it happens on actual device as well.
I am not sure if this is a bug or a normal behaviour of SwiftUI if so what am I doing wrong?
I have a very simple view that you can test with as follows:
import SwiftUI
struct TestView: View {
var body: some View {
GeometryReader { reader in
Text("TEST")
.background(Color.red)
}
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
TestView()
}
}
You can add it in a VStack and add a Spacer and you will see the same behaviour when it is at the bottom.
import SwiftUI
struct TestView: View {
var body: some View {
GeometryReader { reader in
VStack {
Spacer()
Text("TEST")
.background(Color.red)
}
}
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
TestView()
}
}
I can clip it by adding .clipped() to the Text and it will be removed but this sounds like a work around to me.
I stepped away from SwiftUI for a while so I do not remember if this is a normal behaviour or not. I did some research but couldn't find anything related to this.
I am using XCode 14.2 (14C18) and Swift swift-driver version: 1.62.15 Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51)
I explicitely specified device name in preview provider but it is not changing the device in preview editor.
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.previewDevice(PreviewDevice(rawValue: "iPhone 11"))
}
}
Have you downloaded the iPhone 11 simulator into your project? If it's not available in the device selection, it won't display by just setting the device in code. You need to have the simulator device installed as well.
I read tons of questions about how to make the app ignore the safe area, but when I create a new app then the status bar space is ignored anyway without
.ignoresSafeArea()
Why is that? I don't want it to be ignored!
This is all I have:
import SwiftUI
struct ContentView: View {
var body: some View {
VStack{
Text("Hello, world!")
.padding()
Spacer()
}
.frame(width: 300)
.background(Color.teal)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
the result screenshot
You can do it like so:
VStack {
Color.teal
.overlay {
Text("Hello World!")
}
}
Ok I got it, inside a ZStack:
GeometryReader { reader in
Color.white
.frame(height: reader.safeAreaInsets.top, alignment: .top)
.ignoresSafeArea()
}
Use this modifier:
.edgesIgnoringSafeArea(.all)
if you don't want everything you can choose : .top, .bottom, .vertical, .horizontal,...
The Problem:
The NavigationLinks in the macOS version of the SwiftUI app don't work properly.
In my case is a MasterDetailNavigationView.
What I've tried
I have the problem running the app with the macOS simulator, and also running the archived application.
I still have the problem with a brand new project with the sample code below:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
VStack(alignment: .center, spacing: 10) {
NavigationLink(destination: Text("View One")) {
Text("ONE")
}
NavigationLink(destination: Text("View Two")) {
Text("TWO")
}
Spacer()
}
Text("Hello, World!")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
How to reproduce the problem:
Copy and paste the above code in a new SwiftUI project. Enable the macOS tick the the target settings. And the run on the macOS emulator.
Versions:
macOS: 10.15.4 (19E266)
Xcode: 11.4 (11E146)
There is no built-in font-weight modifier for textfield in SwiftUI, as of Xcode 11.2.1.
How can we introduce font-weight without extending UITextField as UIViewRepresentable?
A general approach for using standard font size options and weights that work with SwiftUI TextField. For example:
TextField("Name", text: $name)
.font(Font.headline.weight(.light))
Available standard size options (smallest to largest):
.caption
.footnote
.subheadline
.callout
.body
.headline
.title3
.title2
.title
.largeTitle
Available standard font weights (lightest to heaviest):
.ultralight
.thin
.light
.regular
.medium
.semibold
.bold
.heavy
.black
import SwiftUI
struct ContentView: View {
#State var TextValue: String = "Hello"
var body: some View {
VStack {
TextField("placeholder", text: $TextValue)
.padding(.horizontal, 50)
.font(.system(size: 30, weight: .heavy, design: .default))
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
TextField("Name", text: $name)
.font(Font.body.bold())
iOS 15+
SwiftUI supports markdown.
Add double asterisks (**) arroud the text/characters to make it bold.
Text("**This text is bold**")
To emphasize text, use underscore
Text("_This text is italic_")
The updated approach for iOS 13.0+, macOS 10.15+, tvOS 13.0+, watchOS 6.0+ is:
.fontWeight(.bold)
Expanding on shawnynicole's answer, you can create an extension:
extension View {
func bold() -> some View {
font(Font.body.bold())
}
}
and apply it to any View (including the TextField):
TextField("Text", text: $text)
.bold()