How to set qlabel text's opacity in PyQt? - html

I have a label and I want to set it html\css formatted text from text variable.
label = QtGui.QLabel()
text = '''<font face="tahoma" color="#45688E">THIS TEXT</font>'''
label.setText(text)
I need the word THIS to be colored with my color(#45688E) and word TEXT to be transparent.
The idea is that I want label to show only one word on screen, meanwhile I could programatically get two words.
How to do it?
Thank you.
Update1: photo
Code:
from PyQt4 import QtGui, QtCore
import sys
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.resize(300, 400)
window.setWindowTitle('TITLE1')
window_layout = QtGui.QVBoxLayout()
window.setLayout(window_layout)
label = QtGui.QLabel()
text = '''<font face="tahoma"><span style="color:#45688E">THIS</span><span style="opacity:0"> TEXTANOTHER_WORD</span></font>'''
label.setText(text)
window_layout.addWidget(label)
window.show()
sys.exit(app.exec_())

Wrap your first word in a span tag and style with a color. Add all other words in another span tag and set the opacity of the span tag to 0
'''<font face="tahoma"><span style="color:#45688E">THIS</span><span style="opacity:0">TEXTANOTHER_WORD</span></font>'''
Browser Snippet below
<font face="tahoma"><span style="color:#45688E">THIS</span><span style="opacity:0">TEXTANOTHER_WORD</span></font>
EDIT:
Solution 2
'''<font face="tahoma"><span style="color:#45688E">THIS</span><span style="display:none">TEXTANOTHER_WORD</span></font>'''
A somewhat odd solution with PyQT
Somewhat setting the color to transparent hides that text
style="color:transparent"
Example
text = '''<font face="tahoma" color="red" style="color:green;"><span>THIS</span><span style="color:transparent">TEXT</span></font>'''
Picture below

Related

angular ngModel style

Is it possible to style the value in the attribute ngModel of an input tag?
Example:
<input class="input" type="text" [(ngModel)] = "myService.text">
Let's say the value of text is '28 packages', can I put 28 in bold?
So if i understand correctly you want to have it bold whenever the value is 28 ?
yes its possible you can use a ng-class with a ternary expression like this
.bold{
font-weight:600;
}
<input type="text" ng-class="myService.text == '28 ? 'bold' : '''" class="input" ng-model="myService.text" />
This is not angular-related rather a CSS related question.
You cannot style only a part of an input in HTML/CSS so you won't be able to do it in angular.
Instead, you can use an input that is hidden behind a div. The idea is that when the user clicks the div, you actually focus the input. When the user types text, you capture the content of the input and fill the div with it, eventually adding <span class"highlight"> around the number of packages.
I prepared you a stackblitz in pure CSS/JS. You can adapt it in angular if you want.
Relevant pieces of code :
HTML :
<span id="hiddenSpan">This is the hidden div. Click it and start typing</span>
<div>
<label for="in">The real input</label>
<input id="in" type="text">
</div>
JS :
const input = document.getElementById('in')
const hiddenSpan = document.getElementById('hiddenSpan')
function onInputChanged() {
let text = input.value
const regex = new RegExp('(\\d+) packages')
let result = regex.exec(text)
if(result) {
hiddenSpan.innerHTML = '<span class="highlight">'+result[1]+'</span> packages'
} else {
hiddenSpan.innerHTML = text
}
}
// Capture keystrokes.
input.addEventListener('keyup', onInputChanged)
// Focus the input when the user clicks the pink div.
hiddenSpan.addEventListener('click', function() {
input.focus()
})
CSS :
#hiddenSpan {
background-color: pink;
}
.highlight {
font-weight: bold;
background-color: greenyellow;
}
Note : the downside is that the blinking caret is not visible anymore. You can take a look at this resource if you want to simulate one.
It is not possible to style certain parts of a text <input> field in bold. However, you can use a contenteditable div instead of a text <input> field. Inside the contenteditable div you can have other HTML tags like <strong> to style certain parts of the text however you like.
I created an Angular directive called contenteditableModel (check out the StackBlitz demo here) and you can use it to perform 2-way binding on a contenteditable element like this:
<div class="input" contenteditable [(contenteditableModel)]="myService.text"></div>
The directive uses regular expressions to automatically check for numbers in the inputted text, and surrounds them in a <strong> tag to make them bold. For example, if you input "28 packages", the innerHTML of the div will be formatted like this (to make "28" bolded):
<strong>28</strong> packages
This is the code used in the directive to perform the formatting:
var inputElement = this.elementRef.nativeElement;
inputElement.innerHTML = inputElement.textContent.replace(/(\d+)/g, "<strong>$1</strong>");
this.change.emit(inputElement.textContent);
You can change the <strong> tag to something else (e.g. <span style="text-decoration: underline"> if you want the text to be underlined instead of bolded).
When performing the formatting, there is an issue where the user's text cursor position will be unexpectedly reset back to the beginning of the contenteditable div. To fix this, I used 2 functions (getOriginalCaretPosition and restoreCaretPosition) to store the user's original cursor position and then restore the position back after the text formatting is performed. These 2 functions are kind of complex and they're not entirely relevant to the OP's question so I will not go into much detail about them here. You can PM me if you want to learn more about them.

CSS background for text only & not parent span/div/body?

I'm not sure this is possible, but i was wondering if it is possible to set a background color for text only, & not the span/div/P tags that contain the text.
EG
This text here...
each character of "this" will be a black background with white text, the 'space' will have the blue background the word "text" will be a black background with white text etc....
Something like what deaf people see on some TV shows - captions...
But I don't want to contain each / every word with a div or span - as that will make the total HTML coding huge...
From what i have gathered / googled, I can set a background for an entire 'container' but not just for "text" in the container.
example: How do I set background color of text only in CSS?
The above sets the whole h1 tag as a green background.
PS - i'm only using 'green' as an example - but i've got other colours in mind, or even pictures as the background. but i want the text content to be visible..
PS, if the above can be done, is it also possible to 'opaque' the text-background ? so the actual / main background is partially visible, but keep the text "solid".
Ive used opaque, but it makes the foreground text opaque (not kept as solid).
This is the solution I found using JavaScript. It is definitely not only CSS, but it's as far as I could get it with minimal code:
Note: check the updated JSFiddles below! http://jsfiddle.net/fq4ez69t/1/
It finds all spaces in your "p" tags (i.e. change this to whatever you need) and substitutes them with a span with class .space so that you can style it in your CSS.
Here's the JS:
var str = document.getElementsByTagName("p")[0].innerHTML;
var newstring = str.replace(/ /g, '<span class="space"> </span>');
document.getElementsByTagName("p")[0].innerHTML = newstring;
Update #1
Just thought of this. Maybe change the getElementsByTagName("p")[0].innerHTML; to something like document.getElementsByClassName('shaded')[i]; and use this class on whatever text you want to look like that. This is done using a for loop like so:
http://jsfiddle.net/fq4ez69t/2/
Just add the class shaded to the text element you want to look like the image above and voila!
var shadedtextblocks = document.getElementsByClassName("shaded");
for(var i = 0; i < shadedtextblocks.length; i++)
{
var str = shadedtextblocks[i].innerHTML;
var newstring = str.replace(/ /g, '<span class="space"> </span>');
shadedtextblocks[i].innerHTML = newstring;
}
Update #2 - Works with background images now.
http://jsfiddle.net/37s7ex2j/
Here's an updated version that works for p and h1 tags and uses jQuery. It won't print backgrounds on top of your background image. It looks much better, but the script is a bit slower. Here's the result:
$('.shadedtext').each(function(){
//FOR P ELEMENTS
var text = $.trim($('p').text()),
word = text.split(' '),
str = "";
$.each( word, function( key, value ) {
if(key != 0) { str += " "; }
str += "<span class='shade'>" + value + "</span>";
});
$('p').html(str);
});
Choosing text in a container
In short: No you can not set the background of only the text in a div, or spesialy choose each color for each word.
What you can do is set the text of each container ofc:
<div class="class"></div>
.class {
//background, can also use rgba(0,0,0,0.5) <- semi transparent black.
background-color: white;
}
But I don't want to contain each / every word with a div or span - as that will make the total HTML coding huge...
Keeping your text inside inline-block elements will keep the selection to some what text only.
Example:
p {
background-color: rgba(5, 5, 5, 0.5);
color: white;
}
p.inline {
display: inline-block;
}
Inline
<p>Lorem ipsum dolar si amet</p>
Inline-block
<br>
<p class="inline">Lorem ipsum dolar si amet</p>
If you want something like subtitles on TV, you can add a text-shadow on your text:
.myTextClassSelector{
text-shadow: green 1px 1px, green -1px 1px, green -1px -1px, green 1px -1px;
}
<div>
<p class="myTextClassSelector">This text here</p>
And this other text here.
</div>

Multiline Radiobutton

I've radio buttuons in my WP-8 application.
All contents of my radiobuttons are overlapping. I've fixed height and width but words are not wrapping to second line. How can I solve this problem?
Please do not say something like "\n" content is dynamic.
You are probably setting text to Content property as a string. But you can also use TextBlock.
XAML
<RadioButton>
<TextBlock Text="This is very long text that I want to wrap. Is it long enough?"
TextWrapping="Wrap"/>
</RadioButton>
C#
RadioButton rb = new RadioButton();
rb.Content = new TextBlock()
{
Text = "This is very long text that I want to wrap. Is it long enough?",
TextWrapping = TextWrapping.Wrap,
};
Result

Showing html text in FlowDocumentReader

I have a FlowDocumentReader control in my application.
<FlowDocumentReader Document="{Binding FlowDocument}" Style="{DynamicResource FlowDocumentStyle}" />
And here's how I set the text to the FlowDocumentReader:
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add("some <b>book</b>");
FlowDocument.Blocks.Add(paragraph);
The problem is that 'book' isn't shown like html, the tags are visible in the wpf application.
I tried using this converter:
http://code.msdn.microsoft.com/windowsdesktop/XAML-to-HTML-Conversion-ed25a674/view/SourceCode
but then the text shown in my wpf application looks like this:
<FlowDocument xml:space="preserve" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><Paragraph>some <Run FontWeight="bold">book</Run></Paragraph></FlowDocument>
and again it's not bold. How can I do this?
Use UI Elements to show formatted text and them to InlineUIContainer of Paragraph.
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add("some");
Label lb = new Label();
lb.FontWeight = FontWeights.Bold;
lb.Content = " Book";
paragraph.Inlines.Add(new InlineUIContainer(lb));
this.Doc.Document = new FlowDocument();
this.Doc.Document.Blocks.Add(paragraph);

img tag is not working properly in actionscript3 TextField

I have a TextField.
Here is a snippet:
this.textField = new TextField();
this.textField.defaultTextFormat = font.format;
this.textField.selectable = true;
this.textField.border = true;
this.textField.wordWrap = true;
this.textField.multiline = true;
And here is the method to add new line to the textField box:
this.textField.htmlText = 'some text <IMG SRC="http://gravitatestudio.com/wp-content/themes/gravitate/images/facebook.png">';
The issue is that an image is always placed UNDER text instead of the END OF THE TEXT.
Here is the trace(this.textField.htmlText) after html text has been added:
<P ALIGN="LEFT"><FONT FACE="Verdana" SIZE="12" COLOR="#FFFFFF" LETTERSPACING="0" KERNING="0">some text <FONT SIZE="2"><IMG SRC="http://gravitatestudio.com/wp-content/themes/gravitate/images/facebook.png"> </FONT></FONT></P>
Also I need to add an image to any side of the text like a chat message using smiles (left, middle, right side of the text).
Any suggestions?
TextField html text is very limited and thats why Adobe released TLFTextFields which will give you much more control over the layout of elements in the field. I have personally used it and have made entire chats out of one textfield with images, links etc.
resource:
adobe: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/text/TLFTextField.html
tutorial:
http://flashsimulations.com/2010/09/10/handling-image-loading-in-tlf-textflow-and-textfield/