2011-08-25 96 views
0

我試圖用Flash CS5.5製作rss閱讀器。它幾乎已經完成,但我無法設計新聞標題。問題是文本框的某些部分需要大膽和有顏色。這是我的代碼:需要setTextFormat幫助(AS3)

var num:int = 0; 
var tempText:String; 
var titleStr:String; 
var timeStr:String; 
var descriptionStr: String; 

function rssLoaded(evt:Event):void { 
    rssXML = XML(rssLoader.data); 
    // trace(rssXML); 

    num = 0; 
    for (var rssTitle:String in rssXML.channel.item) { 

     // Set title 
     tempText = rssXML.channel.item[rssTitle].title; 
     tempText = tempText.slice(6); 
     titleStr = tempText + "\r\n"; 

     // Set description 
     tempText = rssXML.channel.item[num].description; 
     // Detect if beginning with tags 
     if ((tempText.charCodeAt(0) == 60) && (tempText.charCodeAt(1) == 73)) { 
      tempText = tempText.slice(tempText.search("/>") + 2, tempText.search("<img")); 
     } else { 
      tempText = tempText.slice(0, 140); 
     } 
     // Detect if still contains tags 
     if ((tempText.indexOf("<") != -1) || (tempText.indexOf(">") != -1)) { 
      num++; 
      continue; 
     } 
     // Detect if beginning with space 
     for (var num2:int=0; tempText.charCodeAt(0) == 32 || tempText.charCodeAt(0) == 160; num2++) { 
      tempText = tempText.slice(1); 
     } 
     descriptionStr = tempText + "...\r\n\r\n"; 

     main_txt.appendText(titleStr); 

     main_txt.setTextFormat(title_tf, main_txt.text.length - titleStr.length, titleStr.length-2); 

     main_txt.appendText(descriptionStr); 
     num++; 
    } 
} 

var title_tf:TextFormat=new TextFormat(); 
title_tf.bold=true; 

當我測試代碼時,我看到只有第一行是粗體,而所有標題都需要加粗。對不起我的英語不好。

真誠

回答

0

這將是更簡單的使用TextField.htmlText屬性文本樣式:

title_tf.htmlText = "<b>" + titleTextString + "</b><br/>" + bodyTextString; 

這種方法也將允許你使用CSS這樣的:

title_tf.styleSheet = myImportedStyleSheet; 
title_tf.htmlText = "<span class='titleClass'>" + titleTextString + "</span><br/><span class='bodyClass'>" + bodyTextString + "</span>"; 
+0

woow這工作正常:main_txt.htmlText = main_txt.htmlText +「」+ titleStr +「」; main_txt.htmlText = main_txt.htmlText + descriptionStr;謝謝您的幫助。 – nikel