Skip to content

Instantly share code, notes, and snippets.

@don1138
Last active April 1, 2023 16:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save don1138/8cd69b4959d25753164691b3b3697219 to your computer and use it in GitHub Desktop.
Save don1138/8cd69b4959d25753164691b3b3697219 to your computer and use it in GitHub Desktop.
Style settings for SwiftUI Text object
// Xcode - Text Formatting
// https://daddycoding.com/2019/12/26/swiftui-text/
Text("Sample Text")
.font(.system(
size: 15,
weight: .semibold,
design: .default // .default, .serif, .rounded, .monospaced
))
// Text Style
.font(.body) // Regular 17/22
.font(.callout) // Regular 16/21
.font(.caption) // Regular 12/16
.font(.footnote) // Regular 13/18
.font(.headline) // Semibold 17/22
.font(.largeTitle) // Regular 34/41
.font(.subheadline) // Regular 15/20
.font(.title) // Regular 28/34
// Weight
.fontWeight(.ultraLight)
.fontWeight(.thin)
.fontWeight(.light)
.fontWeight(.regular)
.fontWeight(.medium)
.fontWeight(.semibold)
.fontWeight(.bold)
.fontWeight(.heavy)
.fontWeight(.black)
// Design
.font(Font.system(size: 36, design: Font.Design.default))
.font(Font.system(size: 36, design: .monospaced))
.font(Font.system(size: 36, design: .rounded))
.font(Font.system(size: 36, design: .serif))
.font(Font.custom("Helvetica Neue", size: 30))
// What custom fonts are preinstalled on iOS?
// https://developer.apple.com/fonts/system-fonts/
// http://www.iosfonts.com
// Use other custom font
// Add *.ttf or *.otf file to project
// Add "Fonts provided by application" to info.plist
// https://developer.apple.com/documentation/uikit/text_display_and_fonts/adding_a_custom_font_to_your_app
// Formatting
.foregroundColor(
.black // System Color
Color("AssetName") // Assets.xcassets
UIColor(red:(16/255), green:(16/255), blue:(16/255), alpha: 1) // UI Color Value
)
.bold()
.italic()
.strikethrough(true, color: .red)
.underline(true, color: .red)
.kerning(1.0)
// - Parameter kerning: How many points the following character should be
// shifted from its default offset as defined by the current character's
// font in points; a positive kerning indicates a shift farther along
// and a negative kern indicates a shift closer to the current character.
.tracking(1.0)
// - Parameter tracking: The tracking attribute indicates how much
// additional space, in points, should be added to each character cluster
// after layout. The effect of this attribute is similar to `kerning()`
// but differs in that the added tracking is treated as trailing
// whitespace and a non-zero amount disables non-essential ligatures.
// If both `kerning()` and `tracking()` are present, `kerning()` will be
// ignored; `tracking()` will still be honored.
.baselineOffset(1.0)
// View Extensions
.multilineTextAlignment(.leading) // leading, center, trailing
.truncationMode(.tail) // head, middle, tail
.lineSpacing(1.0)
// - Parameter lineSpacing: The amount of space between the bottom of one
// line and the top of the next line.
.allowsTightening(true)
// - Parameter flag: A Boolean value that indicates whether the space
// between characters compresses when necessary.
.lineLimit(3)
// Sets the maximum number of lines that text can occupy in this view.
.minimumScaleFactor(1.0)
// - Parameter factor: A fraction between 0 and 1 (inclusive) you use to
// specify the minimum amount of text scaling that this view permits.
// Mixed Formatting
Text("Mix") +
Text(" formats ").bold() +
Text("with").foregroundColor(.yellow) +
Text(" plus ") +
Text("sign.").font(.title).fontWeight(.medium)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment