使用CParaFormat和TCharFormat对文本进行格式化

来源:互联网 发布:淘宝童鞋店铺介绍 编辑:程序博客网 时间:2024/06/02 04:28

 http://wiki.forum.nokia.com/index.php/%E4%BD%BF%E7%94%A8CParaFormat%E5%92%8CTCharFormat%E5%AF%B9%E6%96%87%E6%9C%AC%E8%BF%9B%E8%A1%8C%E6%A0%BC%E5%BC%8F%E5%8C%96

 

 

适用版本: Series 60 2nd, Series 60 3rd


使用文本表示时我们常常希望通过对字体颜色,字体颜色或者段落的区别来突出重点,或者区分不同内容,symbian为我们提供了CParaFormat和TCharFormat这两个类来实现这一功能,如其名所表示的CParaFormat是对段落进行格式化,而TCharFormat则是针对字符具有的属性进行格式化。在说明具体使用之前必须明白一点,CParaFormat和TCharFormat必须分别配合TParaFormatMask和TCharFormatMask这两个类才能起作用,典型的用法为:

 CParaFormat pf; TParaFormatMask pfMask; Pf.iAttribute = value;  pfMask.SetAttrib(EAttribute); ApplyParaFormatL(pf, pfMask);

以上iAttribute和EAttribute表示某种属性,一定要配合使用才有效,TCharFormat与TCharFormatMask的使用类似。

TParaFormatMask的SetAttrib()函数原型如下所示

 void SetAttrib(TTextFormatAttribute aAttribute);

传入的参数为TTextFormatAttribute类型, TTextFormatAttribute是一个枚举值,通过查看其值可以知道通过CParaFormat和TParaFormatMask可以设置段落的哪些属性。不要忘了需要CParaFormat中合适的数据对象配合才能起作用。

当设置好以后需要使用ApplyParaFormatL()函数应用格式化属性。通过查询SDK会发现ApplyParaFormatL()有两个版本,分别如下

 void ApplyParaFormatL(const CParaFormat *aFormat, const     TParaFormatMask &aMask, TInt aPos, Tint aLength); void ApplyParaFormatL(const CParaFormat *aFormat, const TParaFormatMask &aMask);

前一个版本比后一个版本多两个参数,aPos为格式化的文本的起始地址,aLength为格式化的文本长度。第二个版本用于指定整个编辑器格式的情况。

TCharFormat和TCharFormatMask的使用完全类似。

Image:Pic.PNG


以下是使用的一个例子,运行效果如上图所示,关于在文本中插入图片请参考[1]

 CRichText* richtext = iRtEd->RichText();
 // 显示时间 richtext->InsertL(iRtEd->Text()->DocumentLength()/* at the end*/, _L(" 2007/11/23 19:52")); CParaFormat* pf = new ( ELeave ) CParaFormat; CleanupStack::PushL( pf ); TParaFormatMask pfMask; // whether keep no page break with nex paragraph pfMask.SetAttrib( EAttKeepWithNext ); pf->iKeepWithNext = ETrue;  // space below paragraph pf->iSpaceAfterInTwips = 0; pfMask.SetAttrib( EAttSpaceAfter ); // space above paragraph pf->iSpaceBeforeInTwips = KLargeSpace; pfMask.SetAttrib( EAttSpaceBefore );   // set background color //pf->iFillColor = KRcvColor; //pfMask.SetAttrib( EAttFillColor );  // set alignment pfMask.SetAttrib(EAttAlignment);    // set the mask pf->iHorizontalAlignment = CParaFormat::ECenterAlign; richtext->ApplyParaFormatL( pf, pfMask, 0, 16 );   // set font size // Get a logical font to base my font on: const CFont* logicalFont = AknLayoutUtils::FontFromId(EAknLogicalFontPrimaryFont); // Extract font information TFontSpec fontspec = logicalFont->FontSpecInTwips();  TCharFormat cf( fontspec.iTypeface.iName, fontspec.iHeight ); TCharFormatMask cfMask; cfMask.SetAttrib(EAttFontTypeface); cfMask.SetAttrib(EAttFontHeight);   // set font color cf.iFontPresentation.iTextColor = TRgb(128, 128, 128); cfMask.SetAttrib(EAttColor);  // set bold on cf.iFontSpec.iFontStyle.SetStrokeWeight(EStrokeWeightBold); cfMask.SetAttrib(EAttFontStrokeWeight);  // apply the char setting richtext->ApplyCharFormatL( cf, cfMask, 0, 16 ); // 插入消息   richtext->InsertL(iRtEd->Text()->DocumentLength()/* at the end*/, _L("hello,I am davey")); // left margin pf->iLeftMarginInTwips = KLargeMargin; pfMask.SetAttrib( EAttLeftMargin ); // right margin pf->iRightMarginInTwips = KSmallMargin; pfMask.SetAttrib( EAttRightMargin ); // set border TParaBorder paraBorder; paraBorder.iLineStyle=TParaBorder::ESolid; paraBorder.iThickness=6; // border width paraBorder.iColor=TRgb(64, 224, 208); // set border color paraBorder.iAutoColor=EFalse; // iColor overrides text color  pf->SetParaBorderL(CParaFormat::EParaBorderTop,paraBorder); pf->SetParaBorderL(CParaFormat::EParaBorderBottom,paraBorder); pf->SetParaBorderL(CParaFormat::EParaBorderLeft,paraBorder); pf->SetParaBorderL(CParaFormat::EParaBorderRight,paraBorder);  pfMask.SetAttrib(EAttTopBorder); pfMask.SetAttrib(EAttBottomBorder); pfMask.SetAttrib(EAttRightBorder); pfMask.SetAttrib(EAttLeftBorder); // set margin between border pf->iBorderMarginInTwips = KMarginBetweenBorder; pfMask.SetAttrib( EAttBorderMargin );  // inter-line spacing pf->iLineSpacingInTwips = pf->iLineSpacingInTwips*KInterLineSpace; pfMask.SetAttrib( EAttLineSpacing );  // whether keep no page break with next paragraph pfMask.SetAttrib( EAttKeepWithNext ); pf->iKeepWithNext = EFalse;   // space below paragraph pf->iSpaceAfterInTwips = KSmallSpace; pfMask.SetAttrib( EAttSpaceAfter ); // space above paragraph pf->iSpaceBeforeInTwips = 0; pfMask.SetAttrib( EAttSpaceBefore );   //pf->iFillColor = KRgbYellow/*KSendColor*/; //pfMask.SetAttrib( EAttFillColor );   // set alignment pfMask.SetAttrib(EAttAlignment);    // set the mask pf->iHorizontalAlignment = CParaFormat::ELeftAlign ;  richtext->ApplyParaFormatL( pf, pfMask, 17, aParaLength );   CleanupStack::PopAndDestroy(); // pf  // set font size // Get a logical font to base my font on: const CFont* logicalFont = AknLayoutUtils::FontFromId(EAknLogicalFontPrimarySmallFont); // Extract font information fontspec = logicalFont->FontSpecInTwips();   TCharFormat cf( fontspec.iTypeface.iName, fontspec.iHeight ); cfMask.SetAttrib(EAttFontTypeface); cfMask.SetAttrib(EAttFontHeight);  // set font color cf.iFontPresentation.iTextColor = TRgb(0, 206, 209); cfMask.SetAttrib(EAttColor);  // apply the char setting richtext->ApplyCharFormatL( cf, cfMask, 17, aParaLength );

原创粉丝点击