Text swiftui

Text swiftui DEFAULT

Text can be used to represent TextView or UILabel in UIKit. Text has capabilities of both TextView & UILabel. Isn’t is awesome that you won’t be switching either to UILabel or TextView because one lacks some capabilities. This is definitely a good news!

A view that displays one or more lines of read-only text.

Apple Documentation

In this tutorial, you’ll learn what it takes to use Text in SwiftUI. You’ll learn:

  • How to limit text line
  • Different text styles
  • Different font weight
  • Different font design
  • Different text formatting
  • Text alignment
  • Truncation mode
  • Combining modified texts


To follow along this tutorial, you’ll need some basic knowledge in:

  • A basic familiarity with Swift.
  • At least Xcode 11

Text Line Limit

By default the line limit is set to unlimited but in case you would like to limit the line, you could use the and insert the number of line you would like to have. Below is an example of line limit.

The first text does not implement and the line can go on and on. While the second text implements a of 1, it only allows one line regardless of how long the text is.

Text Styles

You can modify the text styles easily the type of style you would like to use.

Font Weight

You can also adjust the font weight depending on your need.

Font Design

There are about 4 Font Design available for use and they are , , and .

Text Formatting

You also can format the text to several other factor such as bold, italic and more. What you will about to see also will give you some insights of the new capabilities that you may easily achieve compare to UIKit.

Line Spacing

Traditionally this could be achieved by TextView and not UILabel. With the introduction of Text, it definitely has make things a lot easier.

Text Alignment

This is a basic one by aligning the text to certain angle using .

You can even try it with or which would align it right and left respectively.

Truncation Mode

By truncating the text, you see a three dots (…) either in between text, beginning of the text or at the end of the text. You can use and select either , or . The below is an example of middle.

Combining Modified Texts

This is a widely used features in most of my projects with a combination of certain design on certain texts. This certainly is a lot easier to achieve and I really do look forward to SwiftUI.

Custom Fonts

You can even configure the text to use certain fonts that are already provided by Apple in the system.

What if you would like to add your own kind of font. That’s definitely do-able. I downloaded a font from this site. It’s called Pacifico. Then head to Info.plist

Make sure that the font file target your project or else it will not work.

Where to go From Here

If you feel like you are ready to take on more challenges, feel free to check out some other tutorials that we have created:

Published inSwiftUI

Sours: https://daddycoding.com/2019/12/26/swiftui-text/

How can a SwiftUI Text with mixed styling be created?

When building views using SwiftUI, the type can be used to define various strings that are going to be displayed within our UI. However, SwiftUI does not include a built-in equivalent to , which is commonly used to style text in various ways when using frameworks like UIKit and AppKit.

So while a value can be styled as a whole — using modifiers like and — at first it might seem like we need to place multiple instances into something like an in order to create a text that has multiple mixed styles:

However, while the above approach might work initially, it does have a few downsides. Not only do we have to manually manage line breaks, but we also have to decide what exact spacing to use between each text component — which gets quite complicated when we start considering things like dynamic text sizes and accessibility.

Thankfully, there is a much better way to accomplish something like the above, and that’s to simply concatenate multiple values using the operator — just like when dealing with normal values:

Now, we no longer have to manually manage the layout of our text, and we get complete support for all sorts of dynamic text features for free, all while still being able to render text with mixed styling within our UI.

Essential Developer

Essential Developer: If you want to become a complete senior iOS developer, join this free online crash course starting on October 18th. Learn how to apply truly scalable iOS app architecture patterns through a series of lectures and practical coding sessions. Click to learn more.

More on similar topics

Browse all content by tag

Sours: https://www.swiftbysundell.com/questions/swiftui-text-mixed-styles
  1. Goldwing ebay
  2. Enbrighten lights remote
  3. Moab decals

SwiftUI Text View Tutorial

SwiftUI Text View

SwiftUI Text is a view that is used to display one or more lines of text in Application UI. The text displayed is read only.

Example Text View

The following code snippet is a simple example to create a SwiftUI Text view.


import SwiftUI struct ContentView: View { var body: some View { Text("Hello, world!") } }

SwiftUI Text View Modifiers

We can use view modifiers to change the styling of the text in Text view.

Bold Text

bold() modifier makes the text in Text view to bold.


import SwiftUI struct ContentView: View { var body: some View { Text("Hello, world!") .bold() } }

Italic Text

italic() modifier makes the text in Text view to italic.


import SwiftUI struct ContentView: View { var body: some View { Text("Hello, world!") .italic() } }

Text as Title

To display the text as standard title in Mac/iOS, use font(_:) modifier.


import SwiftUI struct ContentView: View { var body: some View { Text("Hello, world!") .font(.title) } }

Underline Text

To underline the text in Text view, use underline() modifier.


import SwiftUI struct ContentView: View { var body: some View { Text("Hello, world!") .underline() } }

Text Color

To change the color of text in Text view, use foregroundColor() modifier.


import SwiftUI struct ContentView: View { var body: some View { Text("Hello, world!") .foregroundColor(.red) } }

Text Size

To change the font size of text in Text view, use font() modifier.


import SwiftUI struct ContentView: View { var body: some View { Text("Hello, world!") .font(.system(size: 100)) } }

Text Uppercase

To change the casing of text in Text view to uppercase, use textCase() modifier.


import SwiftUI struct ContentView: View { var body: some View { Text("Hello, world!") .textCase(.uppercase) } }

Text Lowercase

To change the casing of text in Text view to lowercase, use textCase() modifier.


import SwiftUI struct ContentView: View { var body: some View { Text("Hello, world!") .textCase(.uppercase) } }


Concluding this SwiftUI Tutorial for Text view, we have learned how to create a Text view in SwiftUI, and how to modify the styling of the text in Text view using different modifiers. We have covered only some of the modifiers for Text view. For other examples on modifiers for Text view, visit following tutorials.

❮ PreviousNext ❯

Sours: https://www.tutorialkart.com/swiftui/swiftui-text/
(OLD) Reading text from the user with TextField – WeSplit SwiftUI Tutorial 7/10

Text View

Text View

Text in SwiftUI is a view that lets you display one or more lines of text. This is suitable for read-only information that's not editable. To display a line of text, you initialize Text and set a String value.

Changing the Font

The font modifier allows you to customize the text style. By default, it's going to use the SF font specific to its platform: SF Pro for iOS and Mac and SF Compact for Apple Watch. System fonts come with dynamic type, which automatically adjust its size for accessibility.

System Fonts

You can use all system fonts: SF, SF Rounded, SF Mono or New York.

Font Weight


Change Font Color

Use the foregroundColor modifier with a Color value to set the text color.

Dimension and alignment

You can add the frame modifier with size and alignment. This is especially useful for multi-line texts.

Multiline Text Alignment

Use the multilineTextAlignment modifier to align multiple lines of text with leading, center or trailing.

Line Limit

Limit the number of lines for your text with the lineLimit modifier. Overflowing characters will have the ellipsis at the end.

Line Spacing

Add more space between the lines with the lineSpacing modifier.

Sours: https://designcode.io/swiftui-handbook-text-view/

Swiftui text

Chapter 2
Getting Started with SwiftUI and Working with Text

If you've worked with UIKit before, the control in SwiftUI is very similar to in UIKit. It's a view for you to display one or multiple lines of text. This control is non-editable but is useful for presenting read-only information on screen. For example, you want to present an on-screen message, you can use to implement it.

In this chapter, I'll show you how to work with to present information. You'll also learn how to customize the text with different colors, fonts, backgrounds and apply rotation effects.

Creating a New Project for Playing with SwiftUI

First, fire up Xcode and create a new project using the Single View App template. Type the name of the project. I set it to SwiftUIText but you're free to use any other name. For the organization name, you can set it to your company or organization. The organization identifier is a unique identifier of your app. Here I use com.appcoda but you should set to your own value. If you have a website, set it to your domain in reverse domain name notation. To use SwiftUI, you have to choose SwiftUI in the User Interface option. Click Next and choose a folder to create the project.

Figure 1. Creating a new project

Once you save the project, Xcode should load the file and display a design/preview canvas. If you can't see the design canvas, you can go up to the Xcode menu and choose Editor > Canvas to enable it.

By default, Xcode generates some SwiftUI code for . However, the preview canvas doesn't render the app preview. You have to click the Resume button in order to see the preview. After you hit the button, Xcode renders the preview in a simulator that you choose in the simulator selection (e.g. iPhone XR). Optionally, you can hide the project navigator and the utility panes to free up more space for both the code editor and canvas.

Figure 2. The code editor and the canvas

Displaying a Simple Text

The sample code generated in already shows you how to display a single line of text. You initialize a and pass it with the text (e.g. Hello World) to display like this:

The preview canvas should display Hello World on screen. This is the basic syntax for creating a text view. You're free to change the text to whatever value you want and the canvas should show you the change instantaneously.

Figure 3. Changing the text

Changing the Font Type and Color

In SwiftUI, you can change the properties (e.g. color) of a control by calling methods that are known as Modifiers. Let's say, you want to bold the text. You can use the modifier named and specify your preferred font weight (e.g. ) like this:

You access the modifier by using the dot syntax. Whenever you type a dot, Xcode will show you the possible modifiers or values you can use. For example, you will see various font weight options when you type a dot in the modifier. You can choose to bold the text. If you want to make it even bolder, use or .

Figure 4. Choosing your preferred font weight

By calling with the value , it actually returns you with a new view that has the bolded text. What's interesting in SwiftUI is that you can further chain this new view with other modifiers. Say, you want to make the bolded text a little bit bigger, you can write the code like this:

Since we may chain multiple modifiers together, we usually write the code above in the following format:

The functionality is the same but I believe you'll find the code above more easy to read. We will continue to use this coding convention for the rest of this book.

The modifier lets you change the font properties. In the code above, we specify to use the title font type in order to enlarge the text. SwiftUI comes with several built-in text styles including title, largeTitle, body, etc. If you want to further increase the font size, replace with .

Note: You can always to refer the documentation (https://developer.apple.com/documentation/swiftui/font) to find out all the supported values of the modifier.

Figure 5. Changing the font type

You can also use the modifier to specify the font design. Let's say, you want the font to be rounded. You can write the modifier like this:

Here you specify to use the system font with text style and design. The preview canvas should immediately respond to the change and show you the rounded text.

Figure 6. Using the rounded font design

Dynamic Type is a feature of iOS that automatically adjusts the font size in reference to the user's setting (Settings > Display & Brightness > Text Size). In other words, when you use text styles (e.g. ), the font size will be varied and your app will scale the text automatically, depending on the user's preference.

In case you want to use a fixed-size font, you can write the line of code like this:

This tells the system to use a fixed font size of 20 points.

As said, you can continue to chain other modifiers to customize the text. Now let's change the font color. To do that, you can use the modifier like this:

The modifier accepts a value of . Here we specify to use , which is a built-in color. You may use other built-in values like , , etc.

Figure 7. Changing the font color

While I prefer to customize the properties of a control using code, you can also use the design canvas to edit them. Hold the command key and click the text to bring up a pop-over menu. Choose Inspect... and then you can edit the text/font properties.

Figure 8. Using the Inspect feature to edit the properties of the text

Using Custom Fonts

By default, all text are displayed using the system font. If you want to use other fonts, you can replace the following line of code:


Instead of using , the code above use and specify the preferred font name. The name of the font can be found in Font Book. You can open Finder > Application and click Font Book to launch the app.

Figure 9. Font Book

Working with Multiline Text

supports multiple lines by default, so it can display a paragraph of text without using any additional modifiers. Replace the code with the following:

You're free to replace the paragraph of text with your own value. Just make sure it's long enough. Once you made the change, the design canvas should render a multiline text label.

Figure 10. Display multiline text

To align the text in centre, insert the modifier and set the value to like this:

In some cases, you may want to limit the number of lines to a certain number. You can use the modifier to control it. Here is an example:

By default, the system is set to use tail truncation. To modify the truncation mode of the text, you can use the modifier and set its value to or like this:

After the change, your text should look like the figure below.

Figure 11. Using the .head truncation mode

Earlier, I mentioned that the control displays multiple lines by default. The reason is that the SwiftUI framework has set a default value of for the modifier. You can change the value of to and see the result:

Setting the Padding and Line Spacing

Normally the default line spacing is good enough for most situations. In case you want to alter the default setting, you can adjust the line spacing by using the modifier.

As you see, the text is too close to the left and right side of the edges. To give it some more space, you can use the modifier, which adds some extra space for each side of the text. Insert the following line of code after the modifier:

Your design canvas should now show the result like this:

Figure 12. Setting the padding and line spacing of the text

Rotating the Text

The SwiftUI framework provides API to let you easily rotate the text. You can use the modifier and pass the rotation degree like this:

If you insert the above line of code after , you will see the text is rotated by 45 degrees.

Figure 13. Rotate the text

By default, the rotation happens around the center of the text view. If you want to rotate the text around a specific point (say, the top-left corner), you can write the code like this:

We pass an extra parameter to specify the point of the rotation.

Figure 14. Rotate the text around the top-left of the text view

Not only can you rotate thee text in 2D, SwiftUI provides a modifier called that allows you to create some amazing 3D effect. The modifier takes in two parameters: rotation angle and the axis of the rotation. Say, you want to create a perspective text effect, you can write the code like this:

With just a line of code, you already re-create the Star Wars perspective text.

Figure 15. Create amazing text effect by using 3D rotation

You can further insert the following line of code to create a drop shadow effect for the perspective text:

The modifier will apply the shadow effect to the text. All you need to do is specify the color and radius of the shadow. Optionally, you can tell the system the position of the shadow by specifying the and values.

Figure 16. Applying the drop shadow effect


Do you enjoy creating user interface with SwiftUI? I hope so. The declarative syntax of SwiftUI made the code more readable and easier to understand. As you have experienced, it only takes a few lines of code in SwiftUI to create fancy text in 3D style.

To access the full content and the complete source code, please get your copy at https://www.appcoda.com/swiftui.

Sours: https://www.appcoda.com/learnswiftui/swiftui-text.html
(OLD) Reading text from the user with TextField – WeSplit SwiftUI Tutorial 7/10

How to let users select text

Paul Hudson    @twostraws   

Updated for Xcode 13.0

New in iOS 15

Swift’s views represent static, unselectable text by default, but you can change that using the modifier with the value.

For example, this makes some text the user can select, and some the user cannot:

Download this as an Xcode project

When text is selected, the user automatically gains access to the regular text actions such as Copy and Share. However, right now at least the whole text area is copied – you don’t get a text selection loupe, so you can’t select just a few words.

Setting on any kind of group of views will automatically make all text inside that group selectable. For example, we could make both text views in our previous example selectable by moving the modifier up to the stack:

Download this as an Xcode project

You can even apply to a list, in which case the text rows in the list become selectable:

Download this as an Xcode project

Tip: You need to press and hold directly on the text in a row rather than anywhere else.

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

Sours: https://www.hackingwithswift.com/quick-start/swiftui/how-to-let-users-select-text

You will also like:

How to style text views with fonts, colors, line spacing, and more

Paul Hudson    @twostraws   

Updated for Xcode 13.0

Not only do text views give us a predictably wide range of control in terms of how they look, they are also designed to work seamlessly alongside core Apple technologies such as Dynamic Type.

By default a Text view has a “Body” Dynamic Type style, but you can select from other sizes and weights by calling on it like this:

Download this as an Xcode project

We can control the color of text using the modifier, like this:

Download this as an Xcode project

You can also set the background color, but that uses because it’s possible to use more advanced backgrounds than just a flat color. Anyway, to give our layout a yellow background color we would add this:

Download this as an Xcode project

Tip: You might wonder why has color in the name, whereas does not. This is because SwiftUI lets us recolor text however we want, but it must always be a simple color, whereas you can place any kind of view in the background – a color, yes, but also a shape, some other text, and more.

There are even more options. For example, we can adjust the line spacing in our text. The default value is 0, which means there is no extra line spacing applied, but you can also specify position values to add extra spacing between lines:

Download this as an Xcode project

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

Sours: https://www.hackingwithswift.com/quick-start/swiftui/how-to-style-text-views-with-fonts-colors-line-spacing-and-more

514 515 516 517 518