How to change the font?

552 views Asked by At

I want to change the font and set its style to bold. I have two problems:

  • changing CharWeight works but not CharFontName
  • it applies "bold" to the whole paragraph, not only to the selection

Here's my code:

sub AddAnimation
    xTextCursor = ThisComponent.CurrentController.Selection(0)
    xText = xTextCursor.getText()
    xText.CharFontName = "Consolas"
    xText.CharWeight = com.sun.star.awt.FontWeight.BOLD
end Sub
1

There are 1 answers

3
Jim K On BEST ANSWER

Calling getText() gets the entire text, not just the selected part.

Sub ChangeFont
    xTextCursor = ThisComponent.CurrentController.Selection(0)
    xTextCursor.CharFontName = "Consolas"
    xTextCursor.CharWeight = com.sun.star.awt.FontWeight.BOLD
End Sub

The font name changed when I tried it, using both LO and AOO.

result

Are you using CTL or CJK scripts? If so, then it needs to be CharFontNameComplex or CharFontNameAsian. However if CharWeight worked, then that must not be the issue.

One more guess: Maybe a style is overriding it.