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

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值