How to change font to bold, italic, underline with SetDefaultStyle()

514 views
Skip to first unread message

Andreas Falkenhahn

unread,
Aug 22, 2015, 9:41:34 AM8/22/15
to wx-u...@googlegroups.com
I want to change the font for a number of characters inside wxTextCtrl
to bold or italic or underline. However, it doesn't seem to work right.
wxWidgets changes the style correctly but it automatically seems to
choose a larger font size which makes everything look rather ugly.

Here's how I'm doing it:

wxTextAttr ta;

tc->AppendText("Some normal text ");

ta.SetFontUnderlined(true);
tc->SetDefaultStyle(ta);
tc->AppendText("Some underlined text ");

ta.SetFontWeight(wxFONTWEIGHT_BOLD);
tc->SetDefaultStyle(ta);
tc->AppendText("Some bold and underlined text. ")

ta.SetFontStyle(wxFONTSTYLE_ITALIC);
tc->SetDefaultStyle(ta);
tc->AppendText("Some bold and underlined and italic text.");

You can see in the screenshot that the font size of the styled text
is larger than the font size of the normal text which makes this
look ugly. How can I make wxWidgets use the same font size for
the styled text and normal text?

Also, how can I reset the text style back to normal? Doing

tc->SetDefaultStyle(wxTextAttr());

doesn't seem to work...

--
Best regards,
Andreas Falkenhahn mailto:and...@falkenhahn.com
shot.png

Igor Korot

unread,
Aug 23, 2015, 4:57:44 PM8/23/15
to wx-u...@googlegroups.com
Hi, Andreas,

On Sat, Aug 22, 2015 at 9:43 AM, Andreas Falkenhahn
<and...@falkenhahn.com> wrote:
> I want to change the font for a number of characters inside wxTextCtrl
> to bold or italic or underline. However, it doesn't seem to work right.
> wxWidgets changes the style correctly but it automatically seems to
> choose a larger font size which makes everything look rather ugly.

What OS do you running it on?
Are you using MSVC or MinGW?
Can you reproduce it in either text or richtext sample?

Thank you.

>
> Here's how I'm doing it:
>
> wxTextAttr ta;
>
> tc->AppendText("Some normal text ");
>
> ta.SetFontUnderlined(true);
> tc->SetDefaultStyle(ta);
> tc->AppendText("Some underlined text ");
>
> ta.SetFontWeight(wxFONTWEIGHT_BOLD);
> tc->SetDefaultStyle(ta);
> tc->AppendText("Some bold and underlined text. ")
>
> ta.SetFontStyle(wxFONTSTYLE_ITALIC);
> tc->SetDefaultStyle(ta);
> tc->AppendText("Some bold and underlined and italic text.");
>
> You can see in the screenshot that the font size of the styled text
> is larger than the font size of the normal text which makes this
> look ugly. How can I make wxWidgets use the same font size for
> the styled text and normal text?
>
> Also, how can I reset the text style back to normal? Doing
>
> tc->SetDefaultStyle(wxTextAttr());
>
> doesn't seem to work...
>
> --
> Best regards,
> Andreas Falkenhahn mailto:and...@falkenhahn.com
>
> --
> Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
>
> To unsubscribe, send email to wx-users+u...@googlegroups.com
> or visit http://groups.google.com/group/wx-users

Andreas Falkenhahn

unread,
Aug 23, 2015, 5:16:55 PM8/23/15
to Igor Korot
On 23.08.2015 at 22:57 Igor Korot wrote:

> Hi, Andreas,

> On Sat, Aug 22, 2015 at 9:43 AM, Andreas Falkenhahn
> <and...@falkenhahn.com> wrote:
>> I want to change the font for a number of characters inside wxTextCtrl
>> to bold or italic or underline. However, it doesn't seem to work right.
>> wxWidgets changes the style correctly but it automatically seems to
>> choose a larger font size which makes everything look rather ugly.

> What OS do you running it on?
> Are you using MSVC or MinGW?
> Can you reproduce it in either text or richtext sample?

Is this an automated reply? ;-) See my mail again... I think all the
essential info is in there.

Brian Ravnsgaard Riis

unread,
Aug 23, 2015, 5:25:51 PM8/23/15
to wx-u...@googlegroups.com
No, actually it is not. You're on Windows 7, evidently, but which
compiler did you use? This makes quite a difference. I don't see that
information in your post.

/Brian

David Connet

unread,
Aug 23, 2015, 7:17:41 PM8/23/15
to wx-u...@googlegroups.com
On 8/23/2015 2:18 PM, Andreas Falkenhahn wrote:
> On 23.08.2015 at 22:57 Igor Korot wrote:
>
>> Hi, Andreas,
>
>> On Sat, Aug 22, 2015 at 9:43 AM, Andreas Falkenhahn
>> <and...@falkenhahn.com> wrote:
>>> I want to change the font for a number of characters inside wxTextCtrl
>>> to bold or italic or underline. However, it doesn't seem to work right.
>>> wxWidgets changes the style correctly but it automatically seems to
>>> choose a larger font size which makes everything look rather ugly.

Unless it's rich text, you can't. A plain text control has one font.
(And wxTE_RICH/wxTE_RICH2 only works on Windows)

Dave

Andreas Falkenhahn

unread,
Aug 24, 2015, 3:56:57 AM8/24/15
to Brian Ravnsgaard Riis
I use MSVC but seriously, the compiler is the last thing I'd look into
here. I'd be *extremely* surprised if this was actually a compiler issue.

Andreas Falkenhahn

unread,
Aug 24, 2015, 4:00:41 AM8/24/15
to David Connet
As you can see from the screenshot attached to the previous mail, I'm using
wxTE_RICH of course. Otherwise I wouldn't have been able to use different
styles at all.

But the documentation of wxTextCtrl suggests that styling is supported on
all major platforms, there's no mentioning that it only works on Windows.
From the doc:

>Multi-line text controls support styling, i.e. provide a possibility to
>set colours and font for individual characters in it (note that under
>Windows wxTE_RICH style is required for style support). To use the styles
>you can either call SetDefaultStyle() before inserting the text or call
>SetStyle() later to change the style of the text already in the control
>(the first solution is much more efficient).

This implies to me that on Windows I need to set wxTE_RICH but on all
other platforms (GTK, Cocoa) it's working by default.

Andreas Falkenhahn

unread,
Aug 24, 2015, 4:23:09 AM8/24/15
to Andreas Falkenhahn
Btw, because nobody has commented on the actual problem I'm having here yet, let me
just rephrase what is actually the problem here, just to make this clear:

1) As you can see in the screenshot, the styled text appears visibly
larger than the non-styled text although I am *not* modifying the
font size. All I am doing is this:

wxTextAttr ta;

tc->AppendText("Some normal text ");

ta.SetFontUnderlined(true);
tc->SetDefaultStyle(ta);
tc->AppendText("Some underlined text ");

ta.SetFontWeight(wxFONTWEIGHT_BOLD);
tc->SetDefaultStyle(ta);
tc->AppendText("Some bold and underlined text. ")

ta.SetFontStyle(wxFONTSTYLE_ITALIC);
tc->SetDefaultStyle(ta);
tc->AppendText("Some bold and underlined and italic text.");

Why does wxWidgets choose a larger font size for the styled text
although I am *not* making any modifications to the font size inside
the wxTextAttr?

And how can I change this behaviour?

What I want to achieve is this: I want the styled text to appear
in the *same* font size as the non-styled text. The look should
be consistent. Currently, it's clearly visible that the styled
text uses a larger font size although I didn't specify a larger
font size.


2) How can I reset all styles back to their default values? I tried
the following but it doesn't work:

tc->SetDefaultStyle(wxTextAttr());


These are my two questions. Please help!

And to the "What compiler are you using?"-guys on here: No, this is really
not compiler related. I'd be willing to bet a crate of beer on that :)
shot.png

Iwbnwif Yiw

unread,
Aug 24, 2015, 4:47:03 AM8/24/15
to wx-users
You can see in the screenshot that the font size of the styled text
is larger than the font size of the normal text which makes this
look ugly. How can I make wxWidgets use the same font size for
the styled text and normal text?
 

You need to use the same font for the wxTextCtrl and the wxTextAttr. At the moment you (probably) have something like Segoe UI for the wxTextCtrl and Arial for the wxTextAttr. Try a cut and paste into Word to confirm this.

Iwbnwif Yiw

unread,
Aug 24, 2015, 4:49:48 AM8/24/15
to wx-users

You need to use the same font for the wxTextCtrl and the wxTextAttr. At the moment you (probably) have something like Segoe UI for the wxTextCtrl and Arial for the wxTextAttr. Try a cut and paste into Word to confirm this.

p.s. I just tried adding  ta.SetFont(tc->GetFont()); to your code and it seems to do the trick.

Andreas Falkenhahn

unread,
Aug 24, 2015, 5:10:59 AM8/24/15
to Iwbnwif Yiw
On 24.08.2015 at 10:49 Iwbnwif Yiw wrote:

> p.s. I just tried adding ta.SetFont(tc->GetFont()); to your code and it seems to do the trick.

Right, this indeed does the trick. Thanks. I was under the impression that everything
not explicitly set in the wxTextAttr would fall back to what was already there. So
I thought that because I didn't explicitly specify any font it would automatically
use the control font's automatically but apparently this doesn't seem to be the case.

This leaves only the second question open: How do I reset the style to the default
style? i.e. default font, no formatting. I can't seem to figure out the answer to
this pretty trivial question...

Iwbnwif Yiw

unread,
Aug 24, 2015, 6:00:55 AM8/24/15
to wx-users
Right, this indeed does the trick. Thanks. I was under the impression that everything
not explicitly set in the wxTextAttr would fall back to what was already there. So
I thought that because I didn't explicitly specify any font it would automatically
use the control font's automatically but apparently this doesn't seem to be the case.

I agree that this is a bit misleading, especially when wxTextAttr::SetFlags implies that other attributes (such as the font name) shouldn't be changed. 

I am guessing, but probably wxTextAttr needs a font family to work so it sets a default one if none is specified during construction. As the wxTextAttr is standalone and not linked to the wxTextCtrl it doesn't know what the control is using.
 
This leaves only the second question open: How do I reset the style to the default
style? i.e. default font, no formatting. I can't seem to figure out the answer to
this pretty trivial question...


My knowledge of wxTextCtrl is limited and I don't know an elegant / efficient way to do this. If you only have a relatively small amount of text you could try:

    ta.SetFont(tc->GetFont());
    tc->SetStyle(0, tc->GetValue().size(), ta);


 

Brian Ravnsgaard Riis

unread,
Aug 24, 2015, 7:21:36 AM8/24/15
to wx-u...@googlegroups.com
Not the compiler in itself, but the code base does sometimes do things
differently based on the compiler used. I agree, it's unlikely to be the
case here.

And, to make sure we're on the same page, I am also a bit baffled about
the issue you're seeing. I can't help you, unfortunately, but I'm also
hoping someone has an answer.

Regards,
/Brian

Vadim Zeitlin

unread,
Aug 24, 2015, 9:12:29 AM8/24/15
to wx-u...@googlegroups.com
On Mon, 24 Aug 2015 10:25:03 +0200 Andreas Falkenhahn wrote:

AF> 1) As you can see in the screenshot, the styled text appears visibly
AF> larger than the non-styled text although I am not modifying the
AF> font size. All I am doing is this:

I think Iwbnwif Yiw has already answered this. In practice, what you
should do is to add:

AF> wxTextAttr ta;

tc->GetStyle(0, ta);

AF> tc->AppendText("Some normal text ");
AF>
AF> ta.SetFontUnderlined(true);
AF> tc->SetDefaultStyle(ta);
AF> tc->AppendText("Some underlined text ");
AF>
AF> ta.SetFontWeight(wxFONTWEIGHT_BOLD);
AF> tc->SetDefaultStyle(ta);
AF> tc->AppendText("Some bold and underlined text. ")
AF>
AF> ta.SetFontStyle(wxFONTSTYLE_ITALIC);
AF> tc->SetDefaultStyle(ta);
AF> tc->AppendText("Some bold and underlined and italic text.");
AF>
AF> Why does wxWidgets choose a larger font size for the styled text
AF> although I am not making any modifications to the font size inside
AF> the wxTextAttr?
...
AF> 2) How can I reset all styles back to their default values? I tried
AF> the following but it doesn't work:
AF>
AF> tc->SetDefaultStyle(wxTextAttr());

Doing SetDefaultStyle(ta) should work for doing this if you start by
filling it with the information corresponding to the default style.

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/

Andreas Falkenhahn

unread,
Aug 25, 2015, 8:59:12 AM8/25/15
to Vadim Zeitlin
On 24.08.2015 at 15:12 Vadim Zeitlin wrote:

> On Mon, 24 Aug 2015 10:25:03 +0200 Andreas Falkenhahn wrote:

AF>> 1) As you can see in the screenshot, the styled text appears visibly
AF>> larger than the non-styled text although I am not modifying the
AF>> font size. All I am doing is this:

> I think Iwbnwif Yiw has already answered this. In practice, what you
> should do is to add:

AF>> wxTextAttr ta;

tc->GetStyle(0, ta);

So do I need to cache this initial style somewhere in order to be able
to completely reset styles later or is there another way to obtain the
wxTextCtrl's initial style?

Consider the following case:

First, I want the complete text to be bold. Thus, I do:

tc->Clear();
wxTextAttr ta;
tc->GetStyle(0, ta);
ta.SetFontWeight(wxFONTWEIGHT_BOLD);
tc->SetDefaultStyle(ta);
tc->AppendText(wxString("This is bold text"));

Later, I want all the text in the same control to be in italics, but not
in bold. Doing the following here will *not* work now because GetStyle()
obviously returns that bold is active for position 0:

// THIS CODE DOESN'T WORK!
tc->Clear();
wxTextAttr ta;
tc->GetStyle(0, ta);
ta.SetFontStyle(wxFONTSTYLE_ITALIC);
tc->SetDefaultStyle(ta);
tc->AppendText(wxString("This is italic text"));

So do I need to store the initial wxTextAttr returned by GetStyle()
and keep it for the purpose of completely resetting all styles back
to the default ones?

Also, the documentation of GetStyle() could be a little more verbose
as to which platforms actually support it because currently it just
says: "Not all platforms support this function." --- which isn't very
enlightening ;)

Vadim Zeitlin

unread,
Aug 25, 2015, 10:15:35 AM8/25/15
to wx-u...@googlegroups.com
On Tue, 25 Aug 2015 15:01:09 +0200 Andreas Falkenhahn wrote:

AF> So do I need to cache this initial style somewhere in order to be able
AF> to completely reset styles later or is there another way to obtain the
AF> wxTextCtrl's initial style?

Using the style with the controls font and colours (i.e. the values
returned by GetFont() and Get{Fore,Back}groundColour()) should work too.

Iwbnwif Yiw

unread,
Aug 25, 2015, 10:25:58 AM8/25/15
to wx-users
Consider the following case:

First, I want the complete text to be bold. Thus, I do:

tc->Clear();
wxTextAttr ta;
tc->GetStyle(0, ta);
ta.SetFontWeight(wxFONTWEIGHT_BOLD);
tc->SetDefaultStyle(ta);
tc->AppendText(wxString("This is bold text"));

Later, I want all the text in the same control to be in italics, but not
in bold. Doing the following here will *not* work now because GetStyle()
obviously returns that bold is active for position 0:

// THIS CODE DOESN'T WORK!
tc->Clear();
wxTextAttr ta;
tc->GetStyle(0, ta);
ta.SetFontStyle(wxFONTSTYLE_ITALIC);
tc->SetDefaultStyle(ta);
tc->AppendText(wxString("This is italic text"));

So do I need to store the initial wxTextAttr returned by GetStyle()
and keep it for the purpose of completely resetting all styles back
to the default ones?


If it is just the font styles that you are modifying you could use the control's font as default (i.e. as returned by tc->GetFont() or possibly tc->GetDefaultAttributes()). 

If you want to change other parameters, such as paragraph spacing, then I think you need to cache your 'default style' wxTextAttr.

If you are populating the wxTextCtrl programmatically (as opposed to the user typing in text), you could theoretically leave the default style untouched and call SetStyle() on your text, however I think this would be pretty inefficient for anything apart from short strings.


Reply all
Reply to author
Forward
0 new messages