Posted By

arpo on 12/10/09


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

ClayVillage
tspitzr


Basic Rich Text Editor in flash


 / Published in: ActionScript 3
 

This is very basic but shows how to insert bold in a text a input text field. And how to view the "HTML code".

For more advance and complete editor check this out: http://code.google.com/p/as3texteditor/

  1. package com.RichTextEditor
  2. {
  3. import flash.display.MovieClip;
  4. import flash.events.*;
  5. import flash.text.*;
  6. import flash.events.*
  7. import fl.controls.*;
  8. import fl.controls.Button;
  9.  
  10. public class richTextEditor extends MovieClip
  11. {
  12. //Declarations
  13. public var btnB:Button;
  14. public var btnCode:Button;
  15. public var input_txt:TextField;
  16.  
  17. public function richTextEditor()
  18. {
  19. if (stage) init();
  20. else addEventListener(Event.ADDED_TO_STAGE, init);
  21. }
  22.  
  23. private function init(e:Event = null):void
  24. {
  25. removeEventListener(Event.ADDED_TO_STAGE, init);
  26. //Add a input textfiled to do the editing in.
  27. var abc:TextField = new TextField();
  28. input_txt = abc;
  29. abc.width = 510;
  30. abc.height = 250;
  31. abc.x = 15;
  32. abc.y = 100;
  33. abc.htmlText='Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.';
  34. abc.border = true;
  35. abc.wordWrap = true;
  36. abc.useRichTextClipboard = true;
  37. abc.multiline = true;
  38. abc.type = TextFieldType.INPUT;
  39. abc.background = true;
  40. abc.alwaysShowSelection = true;
  41. abc.backgroundColor = 0xFFFFFF;
  42. abc.doubleClickEnabled = true;
  43. abc.alwaysShowSelection = true;
  44. addChild(abc);
  45.  
  46. //Add a buttons on the stage and name it btnB
  47. btnB.addEventListener(MouseEvent.CLICK, onBClick);
  48. btnB.label = "B"
  49.  
  50. //And one called btnCode
  51. btnCode.addEventListener(MouseEvent.CLICK, onCodeClick);
  52. btnCode.label = "Code"
  53.  
  54. }
  55.  
  56. private function onCodeClick(e:MouseEvent):void
  57. {
  58. trace(input_txt.htmlText);
  59. }
  60.  
  61. private function onBClick(e:MouseEvent):void
  62. {
  63. if ((input_txt.selectionBeginIndex == 0) && (input_txt.selectionEndIndex == 0)) return;
  64. if (input_txt.selectionBeginIndex == input_txt.selectionEndIndex) return; //JMW - this fixes a crash that occurs if they don't have any text selected but change the style.
  65.  
  66. var my_fmt:TextFormat = input_txt.getTextFormat(input_txt.selectionBeginIndex, input_txt.selectionEndIndex);
  67. my_fmt.bold = !my_fmt.bold;
  68. input_txt.setTextFormat(my_fmt, input_txt.selectionBeginIndex, input_txt.selectionEndIndex);
  69. }
  70.  
  71. }
  72. }

Report this snippet  

You need to login to post a comment.