Field text position

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Field text position

Post by MaxV » Mon Sep 16, 2013 1:28 pm

Hello,
I'm working on a field with a vertical scroller, that wraps its very long text. User has to zoom the text, i.e. changing font size.
No problem for changing font size using a slider control.
Text is automatically wrapped in the field, so changing font size change current text position displayed. How can set again the displayed text at the same point previous changing font size?
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Field text position

Post by dunbarx » Mon Sep 16, 2013 4:52 pm

Hi.

Is there a cursor blinking in the field when you resize? I can think of a way to do this if there is, though I have not tried it, Something along the lines of (pseudocode):

load the selectedLine into a custom property whenever the user clicks in the field
resize with the slider
scroll to the selectedLine with the new textSize showing

If not, the initial scroll of the field is something that might be loaded instead, and the scale of the new textSize could be used to scale the new scroll value. The lineHeight may need to come into play, but this seems doable.

Craig Newman

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Field text position

Post by bn » Mon Sep 16, 2013 5:42 pm

Hi MaxV,

Code: Select all

local sNoOfLines, sNoOfWords

on scrollbarDrag pValue
   
   lock screen
   set the textsize of field 1 to pValue
   
   -- this selects first word of in topmost visible paragraph and keeps track of it, unblock to activate
   --   select word 1 of  line sNoOfLines of field 1
   --   put item 2 of the formattedRect of word 1 of line sNoOfLines of field 1 into tTop
   
   -- this selects a word in topmost visible line and keeps track of it, block if you want above code active
   select word sNoOfWords of field 1
   put item 2 of the formattedRect of word sNoOfWords of field 1 into tTop
   
   put the top of field 1 - tTop into tDiff
   set the vScroll of field 1 to the vScroll of field 1 - tDiff
   select empty -- block to see which word was selected
   unlock screen
   
end scrollbarDrag

on mouseDown
   put the formattedText of field 1 into tFormatted
   put the vScroll of field 1 + the topMargins of field 1 + the borderWidth of field 1  into tVScroll
   put the effective textHeight of field 1 into tTextHeight
   put tVScroll div tTextHeight + 1 into tLine
   
   put the number of words of line 1 to (tLine - 1) of tFormatted + 1 into sNoOfWords
   put the number of lines of word 1 to sNoOfWords of field 1 into sNoOfLines
end mouseDown
this works on locked fields. In testing I set the scrollbar start value to 11 and end value to 36. Could not come up with easier code. Would be interested in more straight forward approaches though.
Kind regards
Bernd

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Field text position

Post by MaxV » Tue Sep 17, 2013 9:58 am

Thank you bn for you suggestion, vscroll is the key!
I added this code to the scrollbar that set textsize (field "testo" is the field that changes Font size):

Code: Select all

on mouseUp   
   put  (the effective textheight of fld "testo") * (the number of lines in fld "testo") into totalscroll
   put the vscroll of field "testo" into currScroll
   put currScroll / totalScroll into percentscroll   
   set the textSize of field "testo" to the thumbpos of me
   # now totalscroll is changed I need to recalculate it
   put  (the effective textheight of fld "testo") * (the number of lines in fld "testo") into totalscroll
   set the vscroll of fld "testo" to trunc( totalScroll * percentScroll  )
end mouseUp
In the end I just put the vScroll at the same percent before text resizing. :D
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Field text position

Post by bn » Tue Sep 17, 2013 12:17 pm

Hi MaxV,

good you found a solution for your task. Just mind you that the hard part of my solution was to be consistent and precise when changing font sizes. As to not confuse the user when his text is in/decreased in size.

Kind regards
Bernd

Post Reply