Splitting text into cards

Let me explain the idea. Let's say the text and title comes from the Internet. The text takes up more space than one page shown in the gif. How to make automatic generation of cards to fully fit the text, also taking into account the size of the device (that is, smaller phones will have more cards, because there will be fewer cards). Also, the title must appear once on the first card. The last card may be half empty, if that happens. How can such an algorithm be implemented? Please, help me.
SwiftUI or UIKit is welcome.

Splitting text into cards

Usually, it is better UI to have text in a scrollView than in different cards.

the title must appear once on the first card

That will be harder for user. You'd better keep a title, adding "Cont'd" at the end.

Now if you still want to do this.
  • you can have several cards (VCs) or only one in which you will change the content of textView and title.

What you have is to evaluate the size needed for some text. Use
Code Block
func boundingRect(with size: CGSize,
options: NSStringDrawingOptions = [],
context: NSStringDrawingContext?) -> CGRect

See here how to use:
https://stackoverflow.com/questions/19128797/calculating-uilabel-text-size

You will have to iterate on the original text:
  • startPoint = first char

  • endPoint : last char of next word

  • compute the rect

  • it it fits, test with next word for end point

  • if does not fit, keep the previous endPoint: that is where you have to split

For next card, start at previous endPoint and repeat the algorithm above.
Actually, I don't understand the answers well.
What don't you understand. Is it because of english (what is your preferred language) or the content itself ?
To begin with, excuse me for my other question with someone else's code, I did not understand it and did not see your answer, but it seems simpler, but still not clear. Why? I started learning to program for iOS with SwiftUI and have little understanding of how UIKit works. The details will not be superfluous.
Did I explain myself incomprehensibly?
First of all, please clarify what you want to do.

You should better show some example inputs: the text and title comes from the Internet.
And output examples for each input, what sort of cards will be shown?
(Frankly, I do not understand why device size would affect.)

Frankly, Claude31's first reply is too difficult even for me, so it is quite reasonable you do not understand it well.
But if it would be the right direction to achieve what you want, you may need to learn it, even if it may take very long time.
(Simply saying, writing an app would not be so simple.)

So, I recommend you to clarify what you want to achieve. That will help both you and us to judge whether the proposed solution would be the right way to go, or not.
I'll try to clarify so.

There is a full text.
I do think it would be better to let user scroll through the text than create different cards, but…

If the design is for several cards, we need:
  • To know what part of text will fit in the rect of the textField, Label, whatever it is.

  • For the first card, one need to find what is the last word that can fit into the rect.

  • Loop from first word to following words, compute what would be the needed rect to display (with func boundingRect), and continue as long we don't hit the end of text or exceed the size of available rect.

  • When the loop ends, we get the subtext from first word to the last that fits.

  • And put this text in the display zone

 
  • when user asks for next card, redo the same, but the new first word is the one just after last word of the previous card.

Hope that's more clear…

Did you get the answer there ?

If so, don't forget to close this thread.
I did not receive an answer, and if I did, I could not understand and implement it.

I did not receive an answer, and if I did, I could not understand and implement it.

Unless you clarify what you want to do, your question will not be answered.

https://stackoverflow.com/questions/66537950/how-can-i-split-text-into-cards

Anyway, do everything for me-posts are not preferred both in stackoverflow and in the dev forums.
Please clarify what you want to do and show what you have done till now.
This is my partner's post. This is her strategy ahah. We are making an application where the key element on the main screen will be the text placed in the cards. We will update the text on the Internet once a day, they can be of different sizes and must be broken into cards that can be flipped while reading the text.
Seems you want to ignore my suggestion:

You should better show some example inputs:


No, no, what are you. I just don't really understand what the input is. When it comes to text and heading, it is literally text and heading. Text in the region of 2-4 thousand characters. And an example of how it will look is in the gif here, for example:
https://stackoverflow.com/questions/66537950/how-can-i-split-text-into-cards?noredirect=1#

 I just don't really understand what the input is. 

Seems there are nothing I can help. Good luck.
Splitting text into cards
 
 
Q