How to set font style in qml rich format Text - qt

I'm using SF Pro Text fonts in the app. They are loaded from main.cpp:
id = QFontDatabase::addApplicationFont(":/SF-Pro-Text-Light.otf");
id = QFontDatabase::addApplicationFont(":/SF-Pro-Text-Regular.otf");
id = QFontDatabase::addApplicationFont(":/SF-Pro-Text-Medium.otf");
In main.qml I can use SF Pro Text Light font using combination of font.family and font.styleName properties:
Text {
text: "Some Text";
font.family: "SF Pro Text"
font.styleName: "Light"
}
font.styleName here can be set to Light/Medium/Regular.
But my goal is to create text line with few mixed font styles, so I tried to use the following approach:
Text {
text: "<span style=\"font-family:SF Pro Text;\">
Outer
<span style=\"font-family:SF Pro Text;\">Inner Text</span>
Text
</span>";
textFormat: Text.RichText
}
Unfortunately, I don't understand how can I pass style (medium, light or regular) inside the rich text. Could you please point me to the correct property in html or to a better approach?
Thanks.

Related

Is there a way to replace <img> tags with alternate text in selectedText property of QML TextEdit

I have a TextEdit component used to show text messages. When parsing messages I replace common emoticons shortcuts with emojis using <img> tag and setting textFormat: TextEdit.RichText on my TextEdit component. When somebody copies a message containing an emoji, selectedText attribute of TextEdit inserts an OBJECT REPLACEMENT CHARACTER 0xFFFC instead of selected <img> tag.
Example:
import QtQuick 2.9
import QtQuick.Controls 2.2
Item {
id: root
width: 500
height: 500
TextEdit {
id: myTextEdit
anchors.centerIn: parent
text: "Test <img src=\"app.icns\"></img> image"
textFormat: TextEdit.RichText
readOnly: true
selectByMouse: true
onSelectedTextChanged: {
console.log(selectedText);
}
}
}
When you select everything inside of the myTextEdit it prints "Test  image". Is there a way to specify some other way of replacing <img> in selectedText so I can choose some alternate text instead of 0xFFFC.
You can use selectionStart and selectionEnd to get the actual text in that range from text instead of relying on the value of selectedText.
Something like:
console.log(text.slice(selectionStart, selectionEnd));

QML: How to display certain parts of Text in different colors [duplicate]

Hello i would like to add different color on specific words of a string to be used in QML Text
Text {
text: "Blue Red Yellow Green"
}
I know that you can add color to the whole text, but i would like to add specific color on specific words. is this possible? and how is it achieved?
Text items can display both plain and rich text. For example you can have:
Text {
text: "<font color=\"#0000FF\">Blue</font> <font color=\"#FF0000\">Red</font>"
}

how to get set font of text with another text's font

I have two Text items in qml and I want to set font of first text to the font of second text. How can I do that?
e.g
Text{
id:t1
//some code
//anchors ..etc
}
Text{
//set font = t1.font or something similar
}
You are almost there, you need to use colon : to assign the property:
Text {
id: txt1
font.bold: true
text: "Hello"
}
Text {
id: txt2
font: txt1.font
text: "World"
}

Qml: use Text's elide property with textFormat: RichText

I have an RSS feed with escaped HTML characters that I want to display in a Text component where I trim the excess content with elide: Text.ElideRight and wrapMode: text.WordWrap.
While this works very well for plain text, when I use textFormat: Text.RichText the trimming does not work.
How can I make the trimming to work or, if this is impossible, encode the HTML easily prior to binding it to the text component?
Indeed Text doesn't support elide for Text.RichText.
There is a bug open on the Qt bug tracker, and after the first reply there is a possible solution, that I copy and paste here for an easy read:
TextEdit {
property string htmlText: "<b>"+workingText.text+"</b>"
text: htmlText
width: parent.width
onHtmlTextChanged: {elide();}
onWidthChanged: elide();//Yes, this will be slow for dynamic resizing and should probably be turned off during animations
function elide(){//Also, width has to be set, just like elide, or it screws up
text = realText;
var end = richText.positionAt(width - 28,0);//28 is width of ellipsis
if(end != realText.length - 7)//Note that the tags need to be taken care of specially.
text = realText.substr(0,end + 3) + '…' + '</b>';//3 is + <b>
}
font.pixelSize: 22
}
I figured a simple workaround which works quite well in my case, where the width and height of the Text component are fixed, i.e. trimming the string manually. The end result depends a bit on how monospace your font is.
text: {
var name = "my very very long text string with HTML markup that goes on forever and ever and ever"
var text = name.substring(0,45)
if (name.length > 45) text += "..."
return text
}

How to find out which fonts are available in qml?

I would like to know which fonts I can use in a QML environment for the font.family property. Are these fonts system-specific or are they built into the framework? Is there any way to list all available fonts?
This code will list all the accepted font families:
ListView {
anchors.fill: parent;
model: Qt.fontFamilies()
delegate: Item {
height: 40;
width: ListView.view.width
Text {
anchors.centerIn: parent
text: modelData;
}
}
}
The fonts are system-specific so you should see what your system proposes.
If you are using QtCreator :
try putting your mouse over the end of your component name
Text <here> {
...
}
You should see a yellow light, click on it and you'll have an interface that allows to choose the font.
You can also access the interface with ctrl + alt + space while inside the component. Or with right click.
This is a system-specific list of fonts, but you can specify external font from resources (QRC)
You can improve the previous answer by adding this
Text {
font.family: modelData
anchors.centerIn: parent
text: modelData;
}

Resources