A Flash Developer Resource Site

Results 1 to 17 of 17

Thread: CSS styling from AS, not external file

Hybrid View

  1. #1
    Junior Member
    Join Date
    Oct 2000
    Posts
    20

    CSS styling from AS, not external file

    I'm trying to apply some simple CSS coding to an html'd dynamic text box, from *inside* Flash 8, with AS, rather than from an external .css doc.

    I found the code below online; it is similar to many others I've seen. I think it should work, but doesn't. (I tried adding a period in front of customClass--nuthin'; I also tried taking out the fontFamily altogether, as I'm on a Mac and I've heard bad things.)

    I'd like eventually to modify it to allow loading from an html'd external file, which I generally know how to do. I've tried coding that purports to apply ActionScript-based CSS styles in that circumstance, but I couldn't get them to work either.

    Frankly, I've tried this on two other forums and no one's replied.

    Any ideas? TIA.

    /*
    var styles = new TextField.StyleSheet();
    styles.setStyle("p", {textDecoration:"none", color:"#000000"});
    styles.setStyle("customClass", {fontFamily:"_serif",fontWeight:'bold', color:"#FF0000", fontSize:'24px'});
    html_fld.html = true;
    html_fld.styleSheet = this.styles;
    html_fld.htmlText = "<p class='customClass'>Should be red</p><p>Should be black and <span class='customClass'>red again</a></span></p>";
    */

  2. #2
    Member
    Join Date
    May 2007
    Posts
    39
    try reformatting your content:

    Code:
    import TextField.StyleSheet;
    
    var styles = new TextField.StyleSheet();
    styles.setStyle("p", {textDecoration:"none", color:"#000000"});
    styles.setStyle("customClass", {fontFamily:"_serif",fontWeight:'bold', color:"#FF0000", fontSize:'24px'});
    html_fld.html = true;
    html_fld.styleSheet = this.styles;
    html_fld.htmlText = "<p><customClass>Should be red</customClass>Should be black and <customClass>red again</customClass></p>";

  3. #3
    Junior Member
    Join Date
    Oct 2000
    Posts
    20
    Hi, thanks for this.

    But I can't get it to compile. It balks at the first line: // import TextField.StyleSheet;
    saying it is an error (otherwise unspecified).

  4. #4
    Member
    Join Date
    May 2007
    Posts
    39
    oh, you can kill that first line. i used it just to test the code.

  5. #5
    Junior Member
    Join Date
    Oct 2000
    Posts
    20
    Thanks. I did try killing it. It compiled then but still doesn't do anything. My sample version in the original forum query here would bring in the type, it just wouldn't style it.

    I'm afraid your version leaves the text box totally blank. Maybe there's just one little thing off in my set up or your code.

  6. #6
    Member
    Join Date
    May 2007
    Posts
    39
    works for me, see attached.
    Attached Files Attached Files

  7. #7
    Junior Member
    Join Date
    Oct 2000
    Posts
    20
    Got to hand it to you. Sure does work. And when I make a copy, mine works, too.

    Trouble is, when I copy you exact code into my situation, it fails again. I think it has something to do with my having made the text box with "components."

    I don't know that for sure, because I can't replicate what I did; I can't get the UIScrollbar to show up even!

    Anyway, you've given me somthing to go on! Thanks so much!

  8. #8
    Member
    Join Date
    May 2007
    Posts
    39
    so you're instance named "html_fld" is a TextArea component? if so, change
    Code:
    html_fld.htmlText = "<p><customClass>Should be red</customClass>Should be black and <customClass>red again</customClass></p>";
    to

    Code:
    html_fld.text = "<p><customClass>Should be red</customClass>Should be black and <customClass>red again</customClass></p>";

  9. #9
    Junior Member
    Join Date
    Oct 2000
    Posts
    20
    Tried it; didn't work--got error about import line again. Took that out, still didn't work.
    If your altruism borders on sainthood, could you send me another .fla with the same code, bringing in greek html'd text from an external file and loading it into a dynamic text box that scrolls?

    I've been through some many permutations with this I feel like I'm trying to crack the Nazi u-boat cypher.

  10. #10
    Member
    Join Date
    May 2007
    Posts
    39
    post your FLA (or a stripped down version) so i can see exactly what it is you're trying to do.

  11. #11
    Junior Member
    Join Date
    Oct 2000
    Posts
    20
    Apparently my .fla is too big to upload.

    I've stripped it and zipped it and it's still 8.8 megs. What ought I to do?

    Meanwhile, here are my thoughts on it:

    The first page has been denuded. Just click arrow on the right to get to the scrolling text box.

    I checked this stripped down version; it works just like my original.

    I'd like to be able to add some links to this and similar pages. And generally, doll them up with CSS.

    I wanted to avoid CSS external files because I heard there's a timing-conundrum with them--undocumented by Flash but with workarounds abounding among web-lurking gurus. I just want to avoid that, if I could, and just put the CSS styles in AS. But I could go either way, if the timing problem is easily overcome.

    Whew!

    Thanks for taking a look.

    BTW, here's an extra credit question. I *have* been able to put a link in such a box, and color and underline it with HTML and it worked. Except when I tried to add a blank target; then it failed.

    So all this might be an academic exercise vis-a-vis links, because I'd need them to open in blank pages.

    Thanks for taking yet another look.

    If I think of a way of getting it to you, I'll let you know here.

  12. #12
    Junior Member
    Join Date
    Oct 2000
    Posts
    20
    Dear DongermanD

    I've uploaded the zip file to my FTP storage space on my website. You'd need Fetch or Filezilla to gain access and I'd have to put the code here or in an email to you--or is there another way. (There's only one file -- the zip-- in the folder. So no big deal.)

    Let me know. (BTW, the zip file is 8.8 megs--should I try it again?)

  13. #13
    Junior Member
    Join Date
    Oct 2000
    Posts
    20
    Hi, DongermanD

    I was able to get this .fla down from 9 megs to 76KB! (Who knew empty frames took up so much space?!)

    I've rewritten my last missive somewhat for this post.

    I checked this stripped down version; it works just like my original.

    I'd like to be able to add some links to this and similar textfields. And generally, doll them up with CSS. As I mentioned, the code you sent me for my earlier sample worked--but not in my textfield.

    I wanted to avoid CSS external files because I heard there's a timing-conundrum with them--undocumented by Flash but with workarounds abounding among web-lurking gurus. (Something about the CSS externals needing to load *before* the external text file.) I just wanedt to avoid that, if I could, and just put the CSS styles in AS. But I could go either way, if the timing problem is easily overcome.

    Whew!

    Thanks for taking a look.

    BTW, here's an extra credit question. I *have* been able to put a link in such a box, and color and underline it with HTML and it worked. Except when I tried to add a blank target; then it failed. So all this might be an academic exercise vis-a-vis links, because I'd need them to open in blank pages.

    Thanks DongermanD (or anybody else) for taking a look.
    Attached Files Attached Files

  14. #14
    Member
    Join Date
    May 2007
    Posts
    39
    Looks fine when i publish. here's a screenshot. you can set a style in flash to target <a> tags as well.

    also, i've never had a problem with using external CSS files to drive my design, and target="_blank" has always worked for me as well.
    Attached Images Attached Images

  15. #15
    Junior Member
    Join Date
    Oct 2000
    Posts
    20
    Could you try wrapping an <a> tag around a word in my external text file (linking it to, say, Google, and opening in a blank) and then set whacky CSS style for it in an AS in the .fla and see if that works?

    That's what I can't do in that textfield of mine that seems so easy for you to do in yours!

    Thanks again for all your help and patience! You've been great!

  16. #16
    Junior Member
    Join Date
    Feb 2006
    Posts
    8

    Global styling from within actionscript

    Hi guys,

    Sorry for butting in here but I'm trying to figure out this stuff myself, maybe you can help.

    I have a bunch of dynamic text boxes in my movie, and I want to be able to control the text color via CSS in the Actionscript (not an external file, as this is for deployment on a handheld device - flash lite).

    I thought I had my answer from this thread, but it seems from the code above that you must specify the instance name of each text box you want to change to achieve the desired effect (i.e. html_fld.styleSheet = this.styles;).

    Does anyone know of an easy way to set all text boxes to use the same style? Something like *.styleSheet = this.styles; (but obviously that's not the answer).

    What I'm hoping is to have the content of every text box wrapped in a span tag, and then I'd just have to change the CSS once in the AS to have all text color change.

  17. #17
    Junior Member
    Join Date
    Oct 2000
    Posts
    20
    I don't know the answer to holdyerhorses' question. But I was about to answer my own question, so it might help him, somehow.

    I found my answer, as I usually do, by buying yet another Flash book. This one is an O'Reilly called "Flash 8: Projects for Learning Animation and Interactivity" by Rich Shupe & Robert Hoekman, jr.

    Why their code worked for me when all others failed, I have no idea. But I've included a zip'd folder with a little test file and ancillaries for anyone who's interested. I was actually able to use an external CSS file, too. What may have helped was I also created a new text field from scratch (appending an UIScrollBar). Hope my folder of their stuff helps somebody!

    Shupe/Hoekman book otherwise looks very good but have only glanced. They do have about 10 pages on Flash Lite, with lots o' links. So their book might be helpful to holdyerhorses.

    Any how, thanks to DongermanD for hanging in so long with my incessant questions.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center