Hi all!
I’m in need of a method to display dynamic text in a scrolling box that auto-wraps the text. I’ve tried various methods found by searching this site and what I’ve come up with is a combination of a newScrollView widget with a mask containing a display.newText box. However, for the life of me I cannot get the mask to work! The mask does not line up with my “width” and “height” with an origin point at “left”, “top”. I made sure that the mask and newScrollView dimensions are multiples of 4.
I clearly don’t understand how to create the mask and your help in this would be greatly appreciated.
local group = display.newGroup() local width = 252 local height = 168 local left = 100 local top = 200 local widget = require "widget" --------------------------------------- -- getMask --------------------------------------- -- Function from Corona post -- create mask -- width: width of visible area -- height: height of visible area -- dir: system directory or nil to use default system.TemporaryDirectory -- grp: display group to use instead of visible rectangle (removed from display during call) function getMask( width, height, dir, grp, forcenew ) print("getMask") print(width, height) local filename = "mask-"..width.."x"..height..".png" local path = system.pathForFile( filename, dir or system.TemporaryDirectory ) local fh, errStr = io.open( path, "r" ) if (forcenew or not fh) then local mwidth = width+8; mwidth=mwidth+(8-(mwidth%8)) local mheight = height+8; mheight=mheight+(8-(mheight%8)) print(mwidth, mheight) -- create the mask group and the surrounding black area local maskgroup = display.newGroup() local invisible = display.newRect(maskgroup,0,0,mwidth,mheight) local visible = grp -- create the visible rectangle or use the parameter group if (grp) then maskgroup:insert( grp ) visible = grp else visible = display.newRect(maskgroup,0,0,width,height) end invisible.x,invisible.y = 0,0 visible.x,visible.y = 0,0 invisible:setFillColor(0,0,0) maskgroup.x,maskgroup.y = display.contentCenterX, display.contentCenterY display.save( maskgroup, filename, dir or system.TemporaryDirectory ) maskgroup:removeSelf() print("created mask: "..filename) else io.close( fh ) print("loaded mask: "..filename) end return graphics.newMask( filename, dir or system.TemporaryDirectory ) end local descriptionScrollView = widget.newScrollView { left = left, top = top, width = width, height = height, hideBackground = true, horizontalScrollDisabled = false, verticalScrollDisabled = false, hideScrollBar = true, } group:insert(descriptionScrollView) local mask = getMask( width, height, nil, nil, true) descriptionScrollView:setMask(mask) descriptionScrollView.maskX = descriptionScrollView.contentWidth \* 0.5 descriptionScrollView.maskY = descriptionScrollView.contentHeight \* 0.5 descriptionScrollView.maskScaleX = display.contentScaleX descriptionScrollView.maskScaleY = display.contentScaleY local txt = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." local description = display.newText(txt, 0, 0, width, 0, native.systemFont, 14 ) description:setTextColor( 255 ) descriptionScrollView:insert(description)
Note, the getMask function is an awesome piece of code taken from a post elsewhere on this site that dynamically generates a mask file.
Thanks!
