Thanks!
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
How do you display centered text on a monitor and scale the text size?
Started by Phantomizer, 08 October 2013 - 05:33 PMPosted 08 October 2013 - 07:33 PM
I've done a little searching around and found plenty of answers, but I can't seem to work them out…I have a 4 (w) by 1 (h) set of monitors and I want to display a line of text on them that is centered and scale the text so it's easy to read, the computer is behind the monitors.
Thanks!
Thanks!
Posted 08 October 2013 - 08:04 PM
Split into new topic.
Posted 08 October 2013 - 09:47 PM
local function cPrint(string)
x, _ = term.getSize()
_, y = term.getCursorPos()
term.setCursorPos((x-#string)/2,y)
print(string)
end
Is this what you are looking for?
Posted 08 October 2013 - 09:56 PM
local function cPrint(string) x, _ = term.getSize() _, y = term.getCursorPos() term.setCursorPos((x-#string)/2,y) print(string) end
Is this what you are looking for?
Umm…that doesn't do anything?
I replaced string on the last line with the text I want to display on the monitors, still didn't display anything…not even an error.
Posted 08 October 2013 - 10:01 PM
it does work, you have to call it like solocal function cPrint(string) x, _ = term.getSize() _, y = term.getCursorPos() term.setCursorPos((x-#string)/2,y) print(string) end
Is this what you are looking for?
Umm…that doesn't do anything?
I replaced string on the last line with the text I want to display on the monitors, still didn't display anything…not even an error.
cPrint("text to print on in middle")
however there are a few problems with the code that I'll address now
local function cPrint(str)
local x = term.getSize()
local _,y = term.getCursorPos()
term.setCursorPos(math.ceil((x-#str)/2), y)
print(str)
end
The problems with the code were:- naming the variable string, was overriding the string api, this is bad, avoid naming variables the same as apis and default variables/functions
- no localisation of variables, everything should be localised when not needed elsewhere
- no rounding of the x position, the rounding I performed makes it look good on the screen when it is an odd or even length string
cPrint("Centred text")
Posted 08 October 2013 - 10:13 PM
Ok…after some some poking around with it, it does work, but i'm trying to output text to a series of monitors, not the computer itself :P/>
Posted 08 October 2013 - 10:22 PM
take a look into term.redirect and term.restore.
Posted 08 October 2013 - 10:30 PM
take a look into term.redirect and term.restore.
Works, but doesn't output the text centered or scaled.
Posted 08 October 2013 - 10:33 PM
Are you putting any effort into working this out yourself? everything you need to know are either in the answers provided or on the link I providedWorks, but doesn't output the text centered or scaled.
"Here in Ask a Pro, the effort people are willing to expend helping you is usually about equal to the effort you are putting in." — Lyqyd
As a side note for you, if you're trying to do multiple text scales, you cannot, there can only be one text scale on the monitor.
Post your code as well.
Posted 08 October 2013 - 10:43 PM
local function cPrint(str)
local x = term.getSize(3)
local _,y = term.getCursorPos()
term.setCursorPos(math.ceil((x-#str)/2), y)
print(str)
end
term.redirect(peripheral.wrap("back"))
cPrint("BLACKSMITH")
It mostly works, but the problem is: The text is centered at the top, not the center of the monitor and I can't get the text to scale size.
Posted 08 October 2013 - 10:53 PM
Well of course, you didn't specify the absolute centre, so its printing the centre of the lineThe text is centered at the top, not the center of the monitor
y = term.getCursorPos()
term.setCursorPos(math.ceil((x-#str)/2), y)
make it math.floor(y/2)
Hereand I can't get the text to scale size.
Posted 09 October 2013 - 12:18 AM
Got it, thanks.