SwiftUI ScrollView how to scroll on tvOS with long Text - swiftui

I need to render a long text, how to wrap it with ScrollView and then slide it vertically? (tvOS)
I tried the following code, i can focus on it by adding .focusable() to Text, but it cannot slide smoothly, it will go straight to the end.
struct ContentView: View {
let longlongText = Array(repeating: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.", count: 20).joined()
var body: some View {
ScrollView {
VStack {
Button("Hello, world!") {}
Text(longlongText)
.focusable()
}
.padding()
}
}
}

Related

Truncate selected value in SwiftUI Picker with limited width

I'm implementing a Picker in SwiftUI. The picker values are strings that vary in size, but the picker itself has a maxWidth due to UI limitations. Some of the strings won't fit, and that's fine with me. However, at the moment, the string is wrapped, increasing the height of the picker, like so:
I'd like to limit the string to just one line, truncating the string, and keeping the picker height the same.
I've tried
.frame(maxHeight: 20)
.clipped()
.contentShape(Rectangle())
on the picker, but that gives me this:
I'd like to truncate the string. How can I do this? NOTE: it should only truncate here, not in the dropdown list.
I've played around with .lineLimit(1), .multilineTextAlignment(.leading), .truncationMode(.tail), on both the text in the ForEach block, and on the picker itself, but no luck so far.
Here is the full code that I have now:
Picker("Theme", selection: $selectedThemeRawValue) {
ForEach(Theme.allCases.map({$0.rawValue}), id: \.self) { value in
Text(value)
.lineLimit(1)
.multilineTextAlignment(.leading)
.truncationMode(.tail)
}
}
.accentColor(.primary)
.frame(maxHeight: 20)
.clipped()
.contentShape(Rectangle())
.lineLimit(1)
.multilineTextAlignment(.leading)
.truncationMode(.tail)
Does anyone have an idea on how I can do this?

SwiftUI - How to Preallocating space for multiple lines of text

In my view I have some text that changes. So ...
Text("Text that changes and may wrap")
Because there are multiple of these text on screen as well as other SwiftUI components I want the text components to occupy the space required for two lines of text regards of how much text they contain. This is so when the text changes the UI doesn't jink around due to the text resizing.
So I'd like these two to occupy the same space:
+-------------------------------+
| A short piece of text |
| |
+-------------------------------+
+-------------------------------+
| A longer piece of text that |
| wraps around. |
+-------------------------------+
However I'm having trouble figuring out how to do this as there doesn't appear to be any property of a Text component that specifies how many lines to display.
anyone know how to specify the height of a text component based on the lines? Some sort of fixed size based on the font and line spacing perhaps?
For anyone whose interested, here's what I ended up doing:
ZStack {
// This creates the vertical space of two lines so the
// actual text doesn't change size when it wraps to two lines.
Text("\n")
.foregroundColor(.clear)
Text(variableText)
.frame(maxWidth: .infinity)
.multilineTextAlignment(.trailing)
.lineLimit(2)
}

Combining and Breaking a Vertical scrollView Horizontally in SwiftUi?

does anyone have an idea how can i break Vertical ScrollView into small horizontal ScrollView using SwiftUi:
I have the code bellow which displays the youtubeResults vertically, since each item of the ForEach is smaller, so i want to group them by 3 itens Horizontally , then the following 3 itens goes bellow them vertically until the forEach ends.
I would like the results to be displayed like that(with images, this is just an example):
(Justin beiber) (Drake) (Omarion)
(Mandela) (Dj Khaled) (Nirvana)
(Justin beiber) (Prince) (Adele)
My code: Displaying the results one by one Vertically
ScrollView (.vertical, showsIndicators: false, content:{
LazyVStack(spacing : 25){
ForEach(getData.youtubeResults){ result in
SideItemView(youtubeResults: result, selectedTheme: self.$selectedTheme)
}
}
.padding()
.padding(.top)
})
As stated by Asperi, LazyVGrid or LazyHGrid was the soulution, introduced by Apple in IOS 14+.

How can i pass object from class thread to post?

Hello on line 88 I get an error that says no matching function for call to Post::Post(int&, std::string* [4], std::string* [4], std::string, const char [10]) and also candidate expects 1 argument, 5 provided! I am not so good with C++ so I cannot understand what I am doing wrong! I want to pass objects from class Thread to class Post! Thank you very much.
#include<iostream>
#include<string.h>
#include<cstdlib>
using namespace std;
class Forum{
private:
string ForumName="Antikeimenostrafis programmatismos";
public:
Forum(){
}
};
class Thread{
public:
string *postTitles[4];
string *postCreatorNames[4];
string _name;
string _creator;
string _date;
public:
Thread(string name, string creator, string date, int number){
_name=name;
_creator=creator;
_date=date;
}
void NumberOfPost(){
int noPosts=rand()%6;
for(int i=0;i<noPosts;i++){
}
}
public:
string GenerateText(){
string finaltext;
static string texts1[]={"Lorem Ipsum is simply dummy text of the printing and typesetting industry.","Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.","It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.","It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.","It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.","The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.","Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.","Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)."};
static string texts2[]={"Lorem Ipsum is simply dummy text of the printing and typesetting industry.","Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.","It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.","It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.","It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.","The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.","Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.","Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)."};
static string texts3[]={"Lorem Ipsum is simply dummy text of the printing and typesetting industry.","Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.","It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.","It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.","It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.","The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.","Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.","Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)."};
finaltext+=texts1[rand()%6+10];
finaltext+=texts2[rand()%6+10];
finaltext+=texts3[rand()%6+10];
return finaltext;
}
};
class Post{
private:
int _id;
string _title;
string _creator;
string _text;
string _date;
public:
Thread* thread;
Post(int id, string title,string creator, string text, string date){
_id=id;
_title=title;
_creator=creator;
_text=text;
_date=date;
}
void postValue(){
int PostID = 0;
int ThreadNo = 1;
int postNo = 0;
Post tempPost = new Post(PostID, thread->postTitles,thread->postCreatorNames, thread->GenerateText(),"30-1-2017");
}
};
Reading the error, you have the following constructor:
Post(int id, std::string title, std::string creator, std::string text, std::string date)
You are trying to call it with the following arguments:
Post(int&, std::string* [4], std::string* [4], std::string, const char [10])
The int& -> int conversion is allowed, as is the const char[10] -> std::string.
That leaves the std::string*[4] -> std::string conversion. That is not allowed.
Did you perhaps mean to pass *thread->postTitles[0] or somesuch?
A second problem will occure as new instanciate the Post object but returns the pointer to the allocated memory. That means you have to change
Post tempPost = new Post(...)
to
Post * tempPost = new Post(...)
Regrading the error.
Need changes some statements as given below.
string *postTitles[4];
string *postCreatorNames[4];
Post tempPost = new Post(PostID, thread->postTitles,thread->postCreatorNames, thread->GenerateText(),"30-1-2017");
to
string postTitles[4];
string postCreatorNames[4];
Post *tempPost = new Post(PostID, thread->postTitles[ThreadNo],thread->postCreatorNames[ThreadNo], thread->GenerateText(),"30-1-2017");
Hope this will work for you.

Knitr/kable: table moving to the end of page when adding caption/figure label

I'm somewhat new to R Markdown, so apologies if this is silly. I'm preparing a pdf report using R Markdown/Knitr, and running into a problem where, if I try to include a styled table using kable, it moves the table to the bottom of the page.
Here is an example of the code:
---
title: "Testing"
output:
pdf_document:
fig_caption: yes
tables: true
---
```{r setup, include=FALSE}
library(knitr)
library(kableExtra)
```
Section 1: In Table \ref{fig:table1} below...
```{r table1, echo=FALSE}
kable(head(mtcars[,1:4],4), format = "latex", align = "c", caption ="\\label{fig:table1}Table Caption") %>%
column_spec(1, bold = T, width = "6em") %>%
kable_styling(position = "center")
```
Section 2: Lorem ipsum dolor sit amet
The PDF this outputs for me has "section 1" and "section 2" following each other, with the table at the very bottom of the page.
I have tried removing the caption, and using fig.cap at the start of the chunk. Although my table stays where it should, it doesn't generate a caption at all, and references to the figure turn into ??.
What works: getting rid of kableExtra, changing format from latex to pandoc. The table stays where it should, and I get the captions, but the table loses the extra formatting that I'd really like to have for the report.
What am I doing wrong? Thanks for your help!
Edit: Sorry! I'd like for the table to show up where I put it (after "section 1", before "section 2"). Like below, only with a caption/figure label. (The linked image is what I get if I get rid of the caption arg in that code chunk above)