intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Formatting Text Fields with Cascading Style Sheets

Chia sẻ: Qweqwdasd Qweqdasda | Ngày: | Loại File: PDF | Số trang:15

80
lượt xem
6
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Định dạng văn bản lĩnh vực Cascading Style Sheets Cascading Style Sheets (CSS) đã trở thành một công cụ thiết yếu trong việc tạo ra các trang Web hiện đại vì những khả năng định dạng phong phú, họ cung cấp. Với Flash MX 2004, các trường văn bản bây giờ có quyền truy cập (mặc dù phần nào hạn chế) cho rằng sức mạnh như nhau. Chú ý Bài học này không phải là để được một tổng quan rộng lớn của Cascading Style Sheets...

Chủ đề:
Lưu

Nội dung Text: Formatting Text Fields with Cascading Style Sheets

  1. < Day Day Up > Formatting Text Fields with Cascading Style Sheets Cascading Style Sheets (CSS) have become an essential tool in the creation of modern Web pages because of the extensive formatting capabilities they provide. With Flash MX 2004, text fields now have access (albeit somewhat limited) to that same power. NOTE This lesson is not intended to be an extensive overview of Cascading Style Sheets, but rather a simple introduction. A Cascading Style Sheet is a set of rules that describe how various elements are styled and formatted in a tag-based document. Although this might sound complicated, it's actually very simple. As always, the best learning tool is an example. Imagine you have an HTML document structured like this: Hello! I hope to see you soon. Derek This document doesn't contain any of the normal HTML tags. Instead, it contains text placed between custom tags that we made up. This is the first part of the Cascading Style Sheet equation; next, we create our style sheet to describe the formatting that each of these tags represents: //this is our cascading style sheet greeting { color: #663399; font-family: Arial, Helvetica, sans-serif;
  2. font-size: 16px; font-weight: bold; display: block; } message { color: #FF3300; font-size: 12px; font-weight: normal; display: block; } signature { color: #990000; font-size: 14px; font-weight: italic; display: block; } This style sheet contains three rules: greeting, message, and signature. The formatting of each rule is defined within the curly braces. Each line within the curly braces represents a formatting property (color, font-family, font-size, and so on) followed by a colon (:), followed by the value of the property, followed by a semicolon (;). When this style sheet is applied to our HTML document, text between specific tags takes on the formatting of that tag as defined in the style sheet; therefore, the text "Hello!"
  3. appears in a purple, bold, 16-pixel, sans-serif font. There are major benefits to using style sheets. Formatting your document is a breeze because the styling of elements requires the use of a single tag, as opposed to a bunch of nested tags. Another benefit is that if you have several hundred or even thousand HTML documents that get their styling via a style sheet, updating the rules in that single style sheet automatically updates the formatting of all those documents. Although the standard that defines modern Cascading Style Sheet usage allows for some extensive formatting capabilities, Flash's implementation of Cascading Style Sheet functionality is somewhat limited. We'll discuss this in a moment, but now let's look at how Cascading Style Sheets are used in Flash. Cascading Style Sheets can be used in Flash to define how text displayed in a dynamic text field should be formatted. The CSS can be defined internally within a movie using ActionScript, or can exist externally from Flash in a text document with a .css file extension. Creating and using Cascading Style Sheets within Flash requires use of the StyleSheet object. The syntax used to define the rules in the style sheet varies depending on whether the style sheet is internal or external. To help bring this all together, let's look at an example: var myStyleSheet:TextField.StyleSheet = new TextField.StyleSheet(); This line creates a StyleSheet object named myStyleSheet. The setStyle() method is used to define rules for this object within Flash: myStyleSheet.setStyle("greeting", { color: '#663399', fontFamily: 'Arial, Helvetica, sans-serif', fontSize: '16',
  4. fontWeight: 'bold', display: 'block' }); This syntax creates a greeting rule (similar to the one discussed earlier) within the myStyleSheet object. However, there are some syntactical differences between this definition and the one shown earlier. The syntax shown earlier represents the standard syntactical structure of a Cascading Style Sheet (including external style sheets that would be loaded into Flash). The syntax shown above for defining a style is unique to ActionScript, and should only be used when defining rules using the setStyle() method. The main differences to note here are that the style is defined within parentheses; the style's name is within quotes, followed by a comma; property values are between single quotes (''), property settings are terminated by commas instead of semicolons; and the last property definition (in this case, display) is not terminated by anything. Another subtle difference is the fact that standard style sheet syntax defines property names using hyphens (font-family, font-weight, and so on), but internally these same properties are defined by removing the hyphen and using an uppercase character instead (fontFamily, fontWeight, and so on). To apply this style sheet to a text field, you simply set that text field's styleSheet property like this: myTextField_txt.styleSheet = myStyleSheet; Now, whenever any text loaded into that field makes use of the tag, that text
  5. is styled according to the greeting rule: myTextField_txt.htmlText = "Hello!"; NOTE Although it should be obvious, a text field must be set to render HTML for style sheets to work in that field. Using external .css files is a bit different, yet still requires the use of a StyleSheet object. Let's look at another example (which assumes that there is an external .css file named myCSS.css containing style rules): var myStyleSheet:TextField.StyleSheet = new TextField.StyleSheet(); myStyleSheet.onLoad = function(){ myTextField_txt.styleSheet = myStyleSheet; } myStyleSheet.load("myCSS.css"); The first line of this script creates a StyleSheet object for holding the external style sheet rules. The next several lines assign that object an onLoad event handler so that when the external style sheet has completely loaded into it, that object is set as the styleSheet property of the myTextField_txt text field. The last line begins the loading of the external CSS file. As mentioned earlier, Flash's implementation of Cascading Style Sheets is somewhat limited. It offers support for the following style properties (shown using their standard HTML names followed by their ActionScript names): • color (color) • display (display)
  6. • font-family (fontFamily) • font-size (fontSize) • font-style (fontStyle) • font-weight (fontWeight) • margin-left (marginLeft) • margin-right (marginRight) • text-align (textAlign) • text-decoration (textDecoration) • text-indent (textIndent) NOTE For a complete listing of acceptable values for these properties, consult the ActionScript dictionary. In the following exercise, we set up our Flash browser so the user can change the formatting of the loaded HTML pages via submenu choices on the Style menu. The exercise uses both internally and externally defined style rules. The externally defined rules are contained in an external .css file that will be loaded as needed. 1. Open flashBrowser2.fla. Because this exercise is a continuation of the preceding one, you should be familiar with the elements within the project, including the MenuBar component instance and the text field named window_txt where the external HTML files are loaded. In this exercise, we'll simply add ActionScript to Frame 1 of the Actions layer. 2. Using your operating system's directory-exploring application, navigate to the Lesson14/Assets directory and locate the following files: o home.htm o science.htm o external.css The first two files are the HTML documents that our browser is already set up to load. The other file is an external .css file, which our application will load and use. 3. Using your favorite HTML editor, open home.htm. We opened and looked at this document in the preceding exercise, but with a different purpose. This time, notice how various portions of text are placed
  7. between tags that we defined. A total of seven custom tags are used, including these: o o o o In addition, the text includes a hyperlink (). This is not a custom tag, but rather a normal HTML tag; you'll soon see how style sheet formatting can be applied even to standard HTML tags. NOTE The science.htm file uses the same tags. Let's look at the external.css file. 4. Using your favorite text editor (or HTML editor, if it supports displaying CSS files), open external.css. Using standard CSS syntax, this file contains rules for every one of the custom tags contained in our HTML files. You are now armed with enough information to complete the browser project.
  8. 5. Return to Flash. With the Actions panel open, select Frame 1 of the Actions layer and add the following script at the end of the current script: 6. 7. var windowCSS:TextField.StyleSheet = new TextField.StyleSheet(); 8. This step creates the windowCSS StyleSheet object, which handles the application's style sheet needs. 6. Add the following function definition: 7. 8. function setCSS(cssStyle:String){
  9. 9. 10. if(cssStyle == "internal"){ 11. 12. }else{ 13. 14. } 15. 16. } 17. This step is the beginning of the setCSS() function. We'll add script to this function over the next couple of steps. Before we do that, however, it's important to remember that our application was set up in the preceding exercise to call this function when either the Style 1 or Style 2 submenu choice was selected from the Style menu. At the time we coded that functionality, we specified that this function would be passed a value of "internal" if Style 1 was selected, but a value of "external" if Style 2 was selected. The if statement within the function evaluates this passed-in value and causes the function to react accordingly. Over the next couple of steps, we'll use this if statement to tell the function to use internal style rules when "internal" is passed, but load an external CSS file and use those style rules if "external" is passed. 7. Add the following script within the if leg of the conditional statement: 8. 9. with(windowCSS){ 10. 11. setStyle("headline", { 12. 13. color: '#333333', 14. 15. fontFamily: 'Arial, Helvetica, sans-serif', 16. 17. fontSize: '20', 18. 19. fontWeight: 'bold', 20. 21. marginLeft: '4', 22. 23. display: 'block' 24.
  10. 25. }); 26. 27. setStyle("headlineemphasis", { 28. 29. color: '#8F1D03', 30. 31. fontWeight: 'normal', 32. 33. fontStyle:'italic', 34. 35. display: 'inline' 36. 37. }); 38. 39. setStyle("subheadline", { 40. 41. color: '#333333', 42. 43. fontFamily: 'Arial, Helvetica, sans-serif', 44. 45. fontSize: '15', 46. 47. fontWeight: 'bold', 48. 49. marginLeft: '6', 50. 51. display: 'block' 52. 53. }); 54. 55. setStyle("author", { 56. 57. color: '#8F1D03', 58. 59. fontFamily: 'Arial, Helvetica, sans-serif', 60. 61. fontSize: '12', 62. 63. fontWeight: 'normal', 64. 65. fontStyle:'italic', 66. 67. marginLeft: '8',
  11. 68. 69. display: 'block' 70. 71. }); 72. 73. setStyle("mainbody", { 74. 75. color: '#666666', 76. 77. fontFamily: 'Arial, Helvetica, sans-serif', 78. 79. fontSize: '12', 80. 81. fontWeight: 'normal', 82. 83. marginLeft: '4', 84. 85. marginRight: '6', 86. 87. display: 'block' 88. 89. }); 90. 91. setStyle("bodybold", { 92. 93. fontWeight: 'bold', 94. 95. display: 'inline' 96. 97. }); 98. 99. setStyle("bodyitalics", { 100. 101. fontStyle: 'italic', 102. 103. display: 'inline' 104. 105. }); 106. 107. setStyle("a:link", { 108. 109. color: '#00389E' 110.
  12. 111. }); 112. 113. setStyle("a:hover", { 114. 115. textDecoration: 'underline' 116. 117. }); 118. 119. } 120. 121. window_txt.styleSheet = windowCSS; 122. Although this script is lengthy, it's actually quite simple. When the function is passed the value "internal", this section of script is executed. The first part of the script uses a with statement to create the styles in the windowCSS StyleSheet object for all the custom tags that our HTML document uses. The last line of the script applies the windowCSS StyleSheet object to the window_txt text field, causing text within that field to take on the formatting shown here. There are a couple of important points to mention about these styles before we move on. Note that some styles have been defined using a display property of block; others have this property set as inline. (This is also true of the external .css file.) A rule where the display property has been set to block indicates that Flash should automatically place a line break before and after the section of text styled with that rule. Consider this as having the same effect as the paragraph () HTML tag. A value of inline indicates that content formatted with that rule should appear on the same line as preceding and following content. This is similar to the effect of bold () or italic () tags in HTML. Flash also supports a value of none for this property. This setting hides any content styled with that rule. Switching this property value from none to block or inline within the style sheet is an easy way to turn certain sections of content on and off.
  13. Finally, the last two style sets refer to hyperlinks—how they're normally styled, and how a link looks when the user mouses over the link. Yes, you can also define how content formatted with built-in HTML tags looks. For example, to define the appearance of text within an HTML paragraph tag, you use the following syntax: myStyleSheet.setStyle("p", { //property settings }); 8. Add the following script within the else leg of the conditional statement: 9. 10. windowCSS.onLoad = function(){ 11. 12. window_txt.styleSheet = windowCSS; 13.
  14. 14. } 15. 16. windowCSS.load("external.css"); 17. This script takes care of what occurs when the function is passed a value of "external". When this happens, an onLoad event is assigned to the windowCSS StyleSheet object, which states that when the external .css file has finished loading into the windowCSS object, that object will be set as the styleSheet property of window_txt. This action causes the rules defined in the external style sheet to be applied to the HTML document currently loaded into window_txt. The last line begins loading the external style sheet into windowCSS. When we load an external style sheet into the same StyleSheet object that had internally defined styles (as discussed in Step 7), only styles with the same name in both definitions are updated. For example, in Step 7 we internally created a style named subheadline and defined its properties. If the externally loaded .css file defines a property with the same name, the external definition of the rule overwrites the internally defined rule when that file is loaded. If the external .css file has no rule named subheadline, however, the formatting of the internal version of the rule remains intact. The same logic is true conversely: internally defined styles can overwrite externally loaded styles. 9. Add the following two lines at the end of the current script: 10. 11. setCSS("internal"); 12. 13. loadPage("home.htm"); 14. Our application is currently set up to load and style content only when a menu choice is selected, meaning that the content window of the browser is blank on loading. These two lines fix that problem by calling the setCSS() and loadPage() functions immediately when the application loads. It's time to do a final test.
  15. 10. Choose Control >Test Movie. When the movie appears, the home.htm page is loaded into the window_txt text field and is formatted using the internally defined style rules, as discussed in Step 7. If you select Style 2 from the Style menu, the external CSS file is loaded into the application, and the styles defined in it are used as discussed in Step 8. 11. Close the test movie and save your work as flashBrowser3.fla. This lesson taught you the extreme versatility of text fields and how they can be created, controlled, and configured dynamically to enhance your projects in many ways. < Day Day Up >
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2