khchen / wNim

Nim's Windows GUI Framework
MIT License
319 stars 17 forks source link

[help wanted] How to use RichText? #51

Closed nnahito closed 4 years ago

nnahito commented 4 years ago

I want to use RichText. However, using wTeRich has no effect like:

import wNim

let app = App()
let frame = Frame(title = "Hello World", size = (400, 300))
let panel = Panel(frame)

let textctrl = TextCtrl(panel, value = "", style = wBorderSunken or
    wTeMultiLine or wTeRich)

frame.center()
frame.show()
app.mainLoop()

I want to change the color of certain characters in TextControl. For example, change the word "Foo" to red.

I also tried loading an rtf file and displaying it in TextControl, but the text color did not change.

import wNim

let app = App()
let frame = Frame(title = "Hello World", size = (400, 300))
let panel = Panel(frame)

var testdata = readFile("test.rtf")

echo(testdata)

let textctrl = TextCtrl(panel, value = testdata, style = wBorderSunken or
    wTeMultiLine or wTeRich)

frame.center()
frame.show()
app.mainLoop()

How do you do this? I would appreciate it if you could tell me.

Thank you.

khchen commented 4 years ago
import wNim/[wApp, wFrame, wTextCtrl, wFont]

let app = App()
let frame = Frame(title = "Hello World", size = (400, 300))
let textctrl = TextCtrl(frame, value = "", style = wBorderSunken or wTeMultiLine or wTeRich)
textctrl.value = "Foo\nBar"
textctrl.selection = 0..3
textctrl.formatSelection(Font(12), wRed, wWhite)
textctrl.setInsertionPointEnd()

frame.center()
frame.show()
app.mainLoop()

RTF related function will be added in next version.

nnahito commented 4 years ago

Thank you for your reply! I was able to do what I wanted with the sample code you gave me! Thank you.

I look forward to the next update.