NSAttributedString: Is it more expensive to redeclare attributes? - nsattributedstring

Given this string
Lorem Ipsum dolor sit amet
And given I want it to be styled like this
underlined: 0 - 26 (Lorem Ipsum dolor sit amet)
bold: 6 - 21 (Ipsum dolor sit)
red color: 12 - 17 (dolor)
Does it matter performance wise if this is declared like this
0-6 (Lorem )
underlined
6-12 (Ipsum )
underlined, bold
12-17 (dolor)
underlined, bold, red color
17-21 ( sit)
underlined, bold
21-26 ( amet)
underlined
VS if it is declared like this
0-26 (Lorem Ipsum dolor sit)
underlined
6-21 (Ipsum dolor sit)
bold
12-17 (dolor)
red color
??
This is a very simplified example of my much more complex attributed string.
I wonder if this matters performance wise either when rendering or when building this string.

Related

Conditional rendering in streamlit

I have a form where I need to use some conditional rendering. Basically, the form needs to change dynamically based on what the user input is. For instance, if I ask "Where did you hear about us?" and give the users some default options (e.g. "Linkedin", "Our website"..), I want that if the user selects "Other" a st.text_input appears where the user can type the answer to the question.
The problem I am facing is that:
If I use st.form (with the st.submit_form_button), what happens is that the form does not dynamically adapt to the user's input. So the text_field won't show up at all when the user ticks "Other" in the example above.
If I do not use st.form, then the form reloads every time the user clicks on any widget. This does not affect the functionality of the form, but it does make for a very bad user experience!
Any tips on how I could either include conditional rendering within st.form or just avoid the bad user experience of the form being reloaded every time the user clicks on any widget?
Any help would be appreciated!
Instead of using form, you can create the form manually and create several containers to separate the code.
import streamlit as st
########################################################
# Sidebar section
########################################################
sb = st.sidebar # defining the sidebar
sb.markdown("🛰️ **Navigation**")
page_names = ["🏠 Home", "⚙️ Other"]
page = sb.radio("", page_names, index=0)
if page == "🏠 Home":
st.subheader("Example home")
st.subheader("What is Lorem Ipsum? ?")
lorem_ipsum = "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"
st.markdown(lorem_ipsum, unsafe_allow_html=True)
else:
other_title = '<h3 style="margin-bottom:0; padding: 0.5rem 0px 1rem;">⚙️ Other</h3>'
st.markdown(other_title, unsafe_allow_html=True)
lorem_ipsum = "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"
st.markdown(lorem_ipsum, unsafe_allow_html=True)
container_selection = st.container()
col1_cs, col2_cs = container_selection.columns([1, 2])
with col1_cs:
slider_val = st.slider("Form slider")
see_text = st.checkbox("See text")
if see_text:
st.warning("**See text** will take more time. Are you sure ?")
other = st.checkbox("Other")
if other:
st.info("Other checkbox")
result = st.button(label="other buttom")
with col2_cs:
st.caption(" ")
if result:
ccp = st.container()
with ccp:
information_title = '<h3 style="margin-bottom:0; padding: 0.5rem 0px 1rem;">📋 Ipsum is simply dummy text </h3>'
st.markdown(information_title, unsafe_allow_html=True)

R plot - how to add notes and description to the plot?

I have a long description for my plot. And I also need to put some other information under the plot as well.
But I can't figure it out how I can do it. This is what I have so far,
plot(1:10)
title(
main = "Foo",
sub = 'Description \n Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. ',
font.sub = 1,
font.main = 1
)
result is not good as the description is too long and it has been cut off, also the word description is overlapping with the xlable,
How can I box the description under the plot?
This is what I am after,
an additional info - time period.
wrap the description in a grey box.
adjust the text to the left.
Are these possible?
Just a little lesson about typography: the sub argument of your function allows you to put a sub-title. To he honest such elements are not used very often. What you are trying to achieve is to put a caption (which is a very different elements). Typographic conventions say that captions are placed below the plots for graphs and images and above tables. If you use a preparation system like LaTeX or a word processors I advise you to do not put a caption "inside" the plot (say, as a part of the plot itself) by using R functions but to just put your plain plot without captions in your document and then put captions by using tools available for your editors (word etc. or LaTeX or Knitr or even Powerpoint etc. ).
Anyway, my little and boring lesson about typography does not solve your problem. Here I provide a solution with the layout function of base R and the textplot function of the gplots package but as always I bet there is a more efficient and elegant way to achieve the same goal.
library(gplots)
tex <- "Description Lorem ipsum dolor sit amet, consectetuer adipiscing elit. \nAenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque \npenatibus et magnis dis parturient montes, ascetur ridiculus mus."
layout(matrix(c(2,1)), 0.5, 2)
par(mar=c(1,1,1,1))
textplot(tex, halign = "center")
par(mar=c(1,1,1,1))
plot(1:10)
title(main = "Foo", font.main = 1)
just to go back to boring stuff: this way of doing is very tricky and inefficient from my point of view so I advise you to use other tools as I've said previously.
Last but not least, I think that the right syntax for line breaks is \nword to break and not \n word to break
Here is how the result look:
It can be done by using "mtext", and no space between "\n" and following words, eg. "\nLorem".
text<- "Description \nLorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. \nAenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus \nmus. "
par(mar = c(8, 4, 3, 3))
plot(1:10, main = "Foo")
mtext(text, side = 1, line = 6, cex = 0.8, adj = 0)
## line is margin line, starting at 0 counting outwards; adj=0, left or bottom align, 1, right or top align

applying formatting to a character range in textflows in Flex 4.10 SDK

When using any version of Flex 4.10 SDK the following code apply's the format to an entire paragraph instead of a specific character range.
https://issues.apache.org/jira/browse/FLEX-33791
<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" creationComplete="OnCreationComplete(event)">
<s:TextArea width="100%" height="100%" id="txt" editable="true">
<s:content>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.<s:br/>
Vivamus eu erat ac est ullamcorper egestas eget nec mauris.<s:br/>
</s:content>
</s:TextArea>
<fx:Script><![CDATA[
import flashx.textLayout.edit.EditManager;
import flashx.textLayout.formats.TextLayoutFormat;
import mx.events.FlexEvent;
private function OnCreationComplete(event:FlexEvent):void
{
var objFormat:TextLayoutFormat = new TextLayoutFormat();
objFormat.backgroundColor = 0xB9CCFF;
txt.selectRange(5, 8);
var objManager:EditManager = txt.textFlow.interactionManager as EditManager;
objManager.applyFormat(objFormat, objFormat, objFormat);
}
]]></fx:Script>
</s:Application>
The three parameters for applyFormat are for three different ways the format can be applied.
The first parameter, "leafFormat" will get applied to LeafElement objects like SpanElement (or nodes, if you prefer to think of the XML that TLF generates) and will actually create a new leaf if the current (or supplied) SelectionState doesn't encompass an entire LeafElement.
The second parameter, "paragraphFormat" will get applied to the entire paragraph that the current (or supplied) SelectionState is a part of. So if I select only a few characters from a paragraph and then call applyFormat, passing in a background color for the "paragraphFormat" parameter, the entire paragraph will get the background color.
The third parameter, "containerFormat" I've never used and haven't really looked into at all. I would guess that it applies the format to the entire ContainerController object that helps lay out the text.
You can safely pass null (or completely different formats) in for any of the four parameters.
So, in short, I think to fix your problem you just change you function call to:
objManager.applyFormat(objFormat, null, null);

QCheckBox word-wrap

Is there a way to wrap text in QCheckBox like it is done with QLabel?
label = QLabel( QString.fromUtf8('long text in here'))
label.setWordWrap(True)
I tried \n which will add a linebreak but this is not dynamic if I resize the window.
It seems there is a feature request for this at https://bugreports.qt.io/browse/QTBUG-5370.
The issue is not closed, so likely this feature is not implemented. This means that it is not currently possible to add word wrap to QCheckBox.
You can try various workarounds, such as having an empty-text QCheckBox and a QLabel to the right, or try putting a shorter text for the checkbox, with a label below it with the long explanation.
You can develop a Custom widgets for QtDesigner to use this feature graphically (or not).
CheckBoxWordWrap (dynamic when resize):
In QtDesigner:
You can also use the class and modify it if the behavior is not exactly what you want.
"This is my double \n line checkbox". Now you have double line checkbox
There unfortunately is no short and simple solution for this. We have to implement this on our own.
The complete implementation of a LineWrappedRadioButton class is already provided in this related answer. You can easily covert this code into a LineWrappedCheckBox class by simply deriving from QCheckBox instead of from QRadioButton there.
Use it like this:
int main(int argc, char **argv) {
QApplication app(argc, argv);
LineWrappedCheckBox checkBox("Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua.");
checkBox.show();
return app.exec();
}
This will produce the following result:

Qt how to break the LabelText of QInputDialog

Here is my example code:
QInputDialog* inDialog = new QInputDialog();
inDialog->setMaximumWidth(100);
inDialog->setLabelText(QString("long and very long......you can say very long"));
The input box showing really long (as long as the string), I was expected the way to set word-wrap for the LabelText, but it seem QInputDialog has no method for that!!!
What can I do now? Write my own InputDialog class? Oh no...!
I hope there is a better way for it!
I would do it myself, like this for example :
QString s = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut" ;
QString wrapped ;
if(s.length()>35)
{
wrapped = s.left(15) + QString(".....") + s.right(15) ;
}
else
{
wrapped = s ;
}
inDialog->setLabelText(wrapped) ;
I'm just starting with QT so this may not be the best way to get what you want but heres what I would do.
I would create my own custom input dialog which inherits QInputDialog. I would then override the setLabelText function to check if the string length is less than 100.
If it is less than 100 then you can go ahead and display it. If not then you can choose where to add yours dots and remove words in order to bring the size down.
Once its equal to 100 characters or less, you can display it.
I will try and write an example when I get home if you would like.

Resources