×
Namespaces

Variants
Actions

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

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

兼容于
平台:
Symbian

文章
User:Davey 2 在 26 Nov 2007 创建
最后由 hamishwillee 在 08 May 2013 编辑
适用版本:

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的使用完全类似。

Pic.PNG


以下是使用的一个例子,运行效果如上图所示,关于在文本中插入图片请参考使用CPicture在文本(RichTextEditor)中插入图像。

 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 );

davey_2

best regards

This page was last modified on 8 May 2013, at 02:56.
177 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×