I create an AS3 TextField set to Input mode dynamically through code, but when the user tries to input some special characters (eg. á à é è í ì ó ò ú ù ç) they simply do not appear on the TextInput.
Copying and pasting them to the textfield works, but I'd prefer if the user could directly type them.
Here's a quick test demonstrating this:
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
public class TextInput extends Sprite
{
public function TextInput()
{
super();
var t:TextField = new TextField;
t.type = TextFieldType.INPUT;
addChild(t);
}
}
}
This creates a textfield where the user can type, but I can't type special characters like à.
Many thanks.
If you can paste it into the Input field, you should be able to type it.
If you start a new Flash document, using the same font as you are above create an Input textfield on the stage with the following settings:
Embed the normal glyphs
Embed the extended latin glyphs
And this should work, as:
Now if all this works, it might have something to do with the way the class is written.
Writing classes that embeds fonts is frankly a pain. Make sure you embed the font in the library and export it for action script:
Following that, you need to use the following code:
// The name of the font class
var _font:Calibri = new Calibri();
var _textFormat:TextFormat = new TextFormat();
_textFormat.font = _font.fontName;
_textFormat.size = 16;
// For some weird reason the ordering here is important. I remember mucking around with this for ages for an old project. EmbedFonts must come last
var _textField:TextField = new TextField();
_textField.defaultTextFormat = _textFormat;
_textField.type = TextFieldType.INPUT;
_textField.embedFonts = true;
addChild(_textField);
And that should have it all working:
** EDIT **
To those using FlashDevelop, etc you can use the following method:
public class Main extends MovieClip {
[Embed(source='assets/HOBOSTD.OTF', fontName='_hobo', embedAsCFF="false")] public static const HOBO:Class;
public function Main() {
var _font:Font = new HOBO() as Font;
var _textFormat:TextFormat = new TextFormat();
_textFormat.font = _font.fontName;
_textFormat.size = 22;
var _textField:TextField = new TextField();
_textField.embedFonts = true;
_textField.defaultTextFormat = _textFormat;
_textField.autoSize = TextFieldAutoSize.LEFT;
_textField.antiAliasType = AntiAliasType.ADVANCED;
_textField.type = TextFieldType.INPUT;
addChild(_textField);
}
}
And you will get the following:
Now note, the font file must be either relative to your project, or the source can point to the C:\windows\font folder if you choose. In the above example, I copied the font to my assets folder.
Related
Can I check if I'm using an actual font when I create TextFormat/TextField ? If I specify a not embedded font or use any random string, no text is displayed and I don't know why.
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class SimpleSprite extends Sprite
{
//[Embed(source="C:/Windows/Fonts/Arial.ttf",fontName="default_font",mimeType="application/x-font",fontWeight="normal",fontStyle="normal",advancedAntiAliasing="true",embedAsCFF="false")]
//private static var defaultFont: String;
public function SimpleSprite()
{
var t: TextField = new TextField;
t.autoSize = TextFieldAutoSize.LEFT;
t.defaultTextFormat = new TextFormat("default_font", 16, 0xff0000);
t.embedFonts = true;
t.text = "hello world";
addChild(t);
}
}
It doesn't display any text when the embed lines are missing.
Important: My package that creates TextFields does not embed anything and I wish to keep it that way. The embedding must be done by the programmer who uses the package. I want to check if the font is embedded and throw an error if not.
You can use Font.enumerateFonts which will return an array of available embedded fonts. You could use that to create a function like the following:
private function hasEmbeddedFont(fontName:String):Boolean
{
var fonts:Array = Font.enumerateFonts();
for each(var font:Font in fonts)
{
if (font.fontName == fontName)
{
return true;
}
}
return false;
}
And then use it something like this:
t.autoSize = TextFieldAutoSize.LEFT;
t.defaultTextFormat = new TextFormat("default_font", 16, 0xff0000);
t.embedFonts = hasEmbeddedFont("default_font");
t.text = "hello world";
If you're building a library for others to use, you might consider abstracting it into your own custom subclass of TextField so it's all handled automatically.
im trying to use (well succeeding) to use this sound class
http://www.mcfunkypants.com/2011/as3-pitch-shift-mp3/
the example code looks like this. . .
public class Pitch_Shift_Example extends Sprite
{
[Embed(source='Pitch_Shift_Example.mp3')]
private var engine_mp3 : Class;
public var engine_loop:Pitch_Shift_MP3;
public function Pitch_Shift_Example()
{
engine_loop = new Pitch_Shift_MP3(engine_mp3);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
var someText:TextField = new TextField();
someText.x = 200;
someText.y = 0;
someText.textColor = 0xFFFFFF;
someText.selectable = false;
someText.autoSize = TextFieldAutoSize.LEFT;
someText.text = "Pitch Shift MP3 Demo by Breakdance McFunkypants\nMove your mouse to change the sample rate. Enjoy!";
addChild(someText);
}
private function onMouseMove(event:MouseEvent):void
{
engine_loop.rate = (mouseX / stage.width) * 2;
}
}
Now what is confusing me is how the engine_mp3 class uses the sound.
line one embeds the sound.
line two creates an empty class called engine_mp3.
line 3 creates a new pitch shift class which in line 7 we pass the (empty) engine_mp3 class.
Basically how is engine_mp3 getting the mp3 data??
Any help is appreciated.
Andy
It works like something like this:
You embed the mp3 file with these two lines:
Embed(source='Pitch_Shift_Example.mp3')]
private var engine_mp3 : Class;
Those two lines work together. The first line is embed metadata that describes the line below it. What it's doing is embedding the mp3 file and making it accessible as a class. You therefore have a reference to this embedded sound class with the variable name engine_mp3.
Later on in your code, you instantiate a new instance of the Pitch_Shift_MP3 class, and you pass in the reference to your embedded sound class engine_mp3:
engine_loop = new Pitch_Shift_MP3(engine_mp3);
The constructor for the pitch shift class is expecting a class (that represents a sound) as its single argument. What it must be doing in its own code is instantiating the class that engine_mp3 represents by doing this:
var instantiatedSound:Sound = new engine_mp3() as Sound;
Hope that makes sense!
So I am creating a module and I have a screen that I need to be able to allow the users to write questions that they have on their screens in a text box. Does anyone know how to do this?
This is the basic setup that I use for every screen:
package screens
{
import flash.filters.*;
import flash.text.*;
import mapSystem.screenSystem.*;
import mapSystem.*;
import screens.*;
import caurina.transitions.Tweener;
public class screen4 extends screenBase
{
public function screen4(pSystem:mapManager)
{
super(pSystem);
numActions = 1;
}
public override function onAction()
{
if (actionStep == 1)
{
map.fID("54");
}
}
public override function onEnter()
{
map.zoomTo("full");
}
}
}
For users to input text, simply create a textfield and set its "type" property to TextFieldType.INPUT. When you go to retrieve this data, just access the textFields "text" prop.
Update -
Ok = simple google search on "AS3 textField tutorial", first hit was this tutorial, which I yanked and added a couple things to for you. Its fairly basic and well documented, so, depending on your level of experience, should prove illuminating.
//Creating the textfield object and naming it "myTextField"
var myTextField:TextField = new TextField();
//Here we add the new textfield instance to the stage with addchild()
addChild(myTextField);
//Here we define some properties for our text field, starting with giving it some text to contain.
//A width, x and y coordinates.
myTextField.text = "input text here";
myTextField.width = 250;
myTextField.x = 25;
myTextField.y = 25;
//#b99 addition
myTextField.type = TextFieldType.INPUT;
//This is the section for our text styling, first we create a TextFormat instance naming it myFormat
var myFormat:TextFormat = new TextFormat();
//Giving the format a hex decimal color code
myFormat.color = 0xAA0000;
//Adding some bigger text size
myFormat.size = 24;
//Last text style is to make it italic.
myFormat.italic = true;
//Now the most important thing for the textformat, we need to add it to the myTextField with setTextFormat.
myTextField.setTextFormat(myFormat);
Hope that helps!
I'm trying to create dynamic textfield with font embedding. Embeding is dynamic like this:
public class TextFormats extends TextFormat {
private var TF:TextFormat = new TextFormat();
[Embed(source = "/fonts/tahoma.ttf", fontWeight = "normal", fontFamily = "tahomaNormal")]
var fontTahoma:Class;
private var fTahoma:Font;
public function TextFormats():void {
fTahoma = new fontTahoma();
}
public function format(fmb:String):TextFormat {
TF.letterSpacing = -1;
TF.font = fTahoma.fontName;
switch(fmb) {
case "combolist_label":
TF.color = 0x383838;
TF.size = 13;
TF.letterSpacing = 0;
break;
}
return TF;
}
}
When I compile it in flash CS4, embeded text appears on stage fine! But, when I tried to compile it with flash CS5, the text do not appear and no error warnings.
What is the reason? Should I use another methods for font embeding?!
There are a few articles published about the big difference in font embedding that is new in CS5. I think this one is quite good:
Having trouble with embedded fonts and HTML text in Flash CS5?
The manner in which fonts are embedded is an improvement in CS5 -- but it means that all your CS5 dynamic text fields break when you open the FLA for editing in CS5! Which sucks! (Everything still works fine as deployed in SWFs.)
If you open the CS4 FLA in CS5, you basically need to rebuild the dynamic text fields and reapply the embedding.
There is code here:
import flash.text.*;
var font:Font1=new Font1();
var txt_fmt:TextFormat=new TextFormat();
txt_fmt.font=font.fontName;
txt_fmt.size=24
var txt:TextField=new TextField();
txt.autoSize=TextFieldAutoSize.LEFT;
txt.defaultTextFormat=txt_fmt;
txt.embedFonts=true
txt.text="Designscripting.com"
txt.selectable=false
addChild(txt);
Because I want to avoid repetitive code, and I'm using a lot of text formats, I created a CustomTextFormat class in Flex Builder.
Another class, called CustomInputBox.as is using this object to create a format:
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
public class CustomInputBox extends Sprite
{
public function CustomInputBox(xLoc:int, yLoc:int, width:uint, height:uint, password:Boolean = false, text:String = "", font:String = "Arial", fontColor:uint = 0x000000, fontSize:uint = 18, fontBold:Boolean = false)
{
var inputBox:TextField = new TextField();
inputBox.type = TextFieldType.INPUT;
inputBox.mouseEnabled = true;
inputBox.selectable = true;
inputBox.multiline = false;
inputBox.x = xLoc;
inputBox.y = yLoc;
inputBox.width = width;
inputBox.height = height;
inputBox.displayAsPassword = password;
var format:CustomTextFormat = new CustomTextFormat();
inputBox.defaultTextFormat = format;
inputBox.text = text;
addChild(inputBox);
}
}
}
The code of CustomTextFormat is as follows:
package
{
import flash.display.Sprite;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
public class CustomTextFormat extends Sprite
{
public function CustomTextFormat(font:String = "Arial", fontColor:uint = 0x000000, fontSize:uint = 18, fontBold:Boolean = false, fontAlign:String = TextFormatAlign.LEFT)
{
var format:TextFormat = new TextFormat();
format.font = font;
format.color = fontColor;
format.size = fontSize;
format.bold = fontBold;
format.align = fontAlign;
}
}
}
Now, I'm getting error 1067 in the CustomInputBox.as file, it's a Dutch error unfortunately (any way to set flex errors to english?):
1067: Impliciete afgedwongen omzetting van een waarde van het type
CustomTextFormat in een niet-gerelateerd type flash.text:TextFormat.
CustomInputBox.as
It's difficult to translate, but hopefuly the error number and the code are enough to identify my problem. I'm new to Flash, and searched but couldn't find out what I am doing wrong.
Thanks in advance.
Something's wacky here. If you want to assign your custom format like this:
var format:CustomTextFormat = new CustomTextFormat();
inputBox.defaultTextFormat = format;
Then CustomTextFormat needs to extend TextFormat, and the code in CustomTextFormat's constructor should be modifying the inherited TF properties. Alternately, if you want to leave CustomTextFormat extending Sprite, then you need to change CustomTextFormat's "format" property to be a public property, and change your assignment to something like:
var customFormat:CustomTextFormat = new CustomTextFormat();
inputBox.defaultTextFormat = customFormat.format;
Does that make sense? Right now you're trying to set the input's default text format to a class object that extends Sprite. And the inputBox doesn't know anything about CustomTextFormat's internal "format" property, which is both private and temporary.
(Incidentally none of this precisely explains the error message you're getting, but in my experience it's somewhat rare for Flash compiler errors to really tell you what's wrong... they tend to claim you're using classes illegally when all you did is leave out a semicolon. I tend not to trust the error messages too much.)