Abel Toy
Abel Toy

Reputation: 328

AS3 Text Input accents

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.

Upvotes: 2

Views: 2732

Answers (1)

Chris
Chris

Reputation: 58182

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 enter image description here

Embed the extended latin glyphs enter image description here

And this should work, as: enter image description here

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:

enter image description here

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:

enter image description here

** 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:

enter image description here

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.

Upvotes: 3

Related Questions