ActionScript - setTextFormat() 在同一个字符串上多次?
我尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setTextFormat 函数的第三个参数不正确。当您想要获取结束索引时,您正在使用长度。像这样:
我喜欢使用 StyleSheet 来格式化具有多种字体或字体粗细的文本字段。管理起来比较容易一些。
The third parameter on your setTextFormat function is incorrect. You're using length when you want to get the ending index. Like this:
I like to use StyleSheet to format a textfield with multiple fonts or font weights. It's a bit easier to manage.