ActionScript - setTextFormat() 在同一个字符串上多次?

发布于 2024-11-05 01:08:14 字数 1505 浏览 3 评论 0原文

我尝试将 textFormat 分配给同一字符串的两个不同部分失败,但第二次它没有注册并保留默认文本格式。字体的两种样式(常规和粗体)都被嵌入。

//Create Text Field
private function createAboutWindowTextField():TextField
    {
    var aboutWindowFont:Font = new AboutWindowFont();

    var regularFormat:TextFormat = new TextFormat();
    var boldFormat:TextFormat = new TextFormat();

    regularFormat.size = boldFormat.size = 12;
    regularFormat.font = boldFormat.font = aboutWindowFont.fontName;
    regularFormat.align = boldFormat.align = TextFormatAlign.CENTER;
    boldFormat.bold = true;

    var result:TextField = new TextField();
    result.antiAliasType = AntiAliasType.ADVANCED;
    result.autoSize = TextFieldAutoSize.LEFT;
    result.defaultTextFormat = regularFormat;
    result.embedFonts = true;
    result.multiline = true;
    result.selectable = false;
    result.type = TextFieldType.DYNAMIC;

    result.text =   "First Header\n" +
                    "Version 1.0\n" +                                                           
                    "Copyright © 2011\n\n" +

                    "Second Header:\n" +
                    "Other info";                                                       

    result.setTextFormat(boldFormat, result.text.indexOf("First Header"), ("First Header").length);
    result.setTextFormat(boldFormat, result.text.indexOf("Second Header:"), ("Second Header:").length);

    return result;
    }

上面的代码应该导致“第一个标题”和“第二个标题:”都变成粗体,但只有“第一个标题”会被设置为粗体,而“第二个标题:”似乎被简单地忽略。有什么问题吗?

i'm unsuccessfully attempting to assign a textFormat to two different parts of the same string, but the second time it doesn't register and remains the default text format. both styles (regular and bold) of the font are embedded.

//Create Text Field
private function createAboutWindowTextField():TextField
    {
    var aboutWindowFont:Font = new AboutWindowFont();

    var regularFormat:TextFormat = new TextFormat();
    var boldFormat:TextFormat = new TextFormat();

    regularFormat.size = boldFormat.size = 12;
    regularFormat.font = boldFormat.font = aboutWindowFont.fontName;
    regularFormat.align = boldFormat.align = TextFormatAlign.CENTER;
    boldFormat.bold = true;

    var result:TextField = new TextField();
    result.antiAliasType = AntiAliasType.ADVANCED;
    result.autoSize = TextFieldAutoSize.LEFT;
    result.defaultTextFormat = regularFormat;
    result.embedFonts = true;
    result.multiline = true;
    result.selectable = false;
    result.type = TextFieldType.DYNAMIC;

    result.text =   "First Header\n" +
                    "Version 1.0\n" +                                                           
                    "Copyright © 2011\n\n" +

                    "Second Header:\n" +
                    "Other info";                                                       

    result.setTextFormat(boldFormat, result.text.indexOf("First Header"), ("First Header").length);
    result.setTextFormat(boldFormat, result.text.indexOf("Second Header:"), ("Second Header:").length);

    return result;
    }

the above code should result in both "First Header" and "Second Header:" becoming bold, but only "First Header" will be set as bold while "Second Header:" seems to be simply ignored. what's the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

濫情▎り 2024-11-12 01:08:14

setTextFormat 函数的第三个参数不正确。当您想要获取结束索引时,您正在使用长度。像这样:

var indexA:int = result.text.indexOf("Second Header:");
result.setTextFormat(boldFormat, indexA, indexA + ("Second Header:").length);

我喜欢使用 StyleSheet 来格式化具有多种字体或字体粗细的文本字段。管理起来比较容易一些。

var reg:HelveticaReg = new HelveticaReg();
var bold:HelveticaBold = new HelveticaBold();

var format:TextFormat = new TextFormat(reg.fontName, 14, 0x000000);

var ss:StyleSheet = new StyleSheet();
ss.setStyle(".bold", {fontFamily:bold.fontName});

var tf:TextField = new TextField();
tf.width = 400;
tf.multiline = true;
tf.wordWrap = true;
tf.autoSize = TextFieldAutoSize.LEFT;
tf.selectable = false;
tf.antiAliasType = AntiAliasType.ADVANCED;
tf.embedFonts = true;
tf.type = TextFieldType.DYNAMIC;
tf.defaultTextFormat = format;
tf.styleSheet = ss;
tf.htmlText = "<span class='bold'>Lorem ipsum dolor</span> sit amet, consectetur adipiscing elit.";
addChild(tf);

The third parameter on your setTextFormat function is incorrect. You're using length when you want to get the ending index. Like this:

var indexA:int = result.text.indexOf("Second Header:");
result.setTextFormat(boldFormat, indexA, indexA + ("Second Header:").length);

I like to use StyleSheet to format a textfield with multiple fonts or font weights. It's a bit easier to manage.

var reg:HelveticaReg = new HelveticaReg();
var bold:HelveticaBold = new HelveticaBold();

var format:TextFormat = new TextFormat(reg.fontName, 14, 0x000000);

var ss:StyleSheet = new StyleSheet();
ss.setStyle(".bold", {fontFamily:bold.fontName});

var tf:TextField = new TextField();
tf.width = 400;
tf.multiline = true;
tf.wordWrap = true;
tf.autoSize = TextFieldAutoSize.LEFT;
tf.selectable = false;
tf.antiAliasType = AntiAliasType.ADVANCED;
tf.embedFonts = true;
tf.type = TextFieldType.DYNAMIC;
tf.defaultTextFormat = format;
tf.styleSheet = ss;
tf.htmlText = "<span class='bold'>Lorem ipsum dolor</span> sit amet, consectetur adipiscing elit.";
addChild(tf);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文