different text alignment in the same field

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
SteveTX
Posts: 170
Joined: Sun Jan 17, 2010 9:00 pm

different text alignment in the same field

Post by SteveTX » Mon Jul 22, 2013 8:58 pm

Is it possible to have a set of works on line 1 of a field to align left, and the other words on line 1 align right?

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: different text alignment in the same field

Post by Simon » Mon Jul 22, 2013 9:21 pm

Isn't this possible with a <div align> and setting the htmlText of a field?
It's been ages since I did any html

Simon
EDIT: Oh htmlText does not support align
Last edited by Simon on Mon Jul 22, 2013 10:22 pm, edited 1 time in total.
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

Re: different text alignment in the same field

Post by dunbarx » Mon Jul 22, 2013 10:11 pm

Simpler than that.

Set the textAlign of line "yourLine" of fld "yourField" to {left|center|right}

You can mix and match at will.

Craig Newman

SteveTX
Posts: 170
Joined: Sun Jan 17, 2010 9:00 pm

Re: different text alignment in the same field

Post by SteveTX » Mon Jul 22, 2013 11:05 pm

Craig, I want half the line aligned left, the other half aligned right, not one or the other.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: different text alignment in the same field

Post by Simon » Mon Jul 22, 2013 11:09 pm

I think you will have to break it into 2 fields.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

Re: different text alignment in the same field

Post by dunbarx » Mon Jul 22, 2013 11:57 pm

Hi.

Ah.

This will be more difficult unless you use a monospaced font. But let's say you do, like courier. I did the following just because I was intrigued by your question. I left all the test and development code in my experiment. Not that this is too big a deal. Make a reasonably sized field (field "f9")on a card. Put this into a button script:

Code: Select all

on mouseUp
   put "aaa bbb" & "," & "ccc ddd" into fld "f9"
   put the formattedWidth of line 1 of fld "f9" into wholeWidth
   put the formattedwidth of item 1 of line 1 of fld "f9" into leftWidth
   put the formattedwidth of item 2 of line 1 of fld "f9" into rightWidth
   
   put the width of fld "f9" into fldWidth
   put the length of line 1 of fld "f9" into charWidth
   put  (the width of fld "f9" / the length of line 1 of fld "f9") into charWidth
   put the formattedWidth of char 1 of fld "f9" into charWidth
   put round(the width of fld "f9" / charwidth) into sepWidth
   
   put "leftWidth" && leftWidth into line 3 of fld "f9"
   put "rightWidth" && rightWidth into line 4 of fld "f9"
   put "wholeWidth" && wholewidth into line 5 of fld "f9"
   put "fieldWidth" && the width of fld "f9" into line 6 of fld "f9"
   put "charWidth" & charwidth into line 7 of fld "f9"
   put sepWidth - the length of item 1 of line 1 of fld "f9" - the length of item 2 of line 1 of fld "f9"
   repeat sepWidth - the length of item 1 of line 1 of fld "f9" - the length of item 2 of line 1 of fld "f9" - 2
      put space after separator
   end repeat
   put item 1 of line 1 of fld "f9" & separator &  item 2 of line 1 of fld "f9" into line 9 of fld "f9"
   
end mouseUp
Line 9 contains your separated text. Like I said, I left the test stuff in. If you change the text in the first line on either side of the tab, it works pretty well. Just an example of text massaging.

To do this with a proportional font is much dicier.

EDIT. This was improved a lot over the half hour I first posted.
EDIT. Things go south if your field is not wide enough for the text of interest to sit comfortably on one line.
Craig Newman

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

Re: different text alignment in the same field

Post by dunbarx » Wed Jul 24, 2013 1:12 pm

The process is actually fairly straightforward even with proportional fonts. Since the formattedWidth of each char in any font can be obtained, it is only a matter of creating a lookup table for each character, and looping through the text in question to obtain the total pixel width of each block. The string of spaces (the formattedWidth of the space char can also be determined) can then be assembled and inserted between the blocks as in the previous example.

Craig Newman

Post Reply