×
Namespaces

Variants
Actions

使用AknTextUtils 工具类进行字符串处理

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

文章
chengkai 在 21 Nov 2007 创建
最后由 hamishwillee 在 15 Dec 2011 编辑

AknTextUtils类提供了几个方便的文本处理方法,包括字符替换分割等.这里给出较为常用的示例使用方法更详细的信息请参考SDK文档.

Contents

通过指定的象素宽度截取文本

使用AknTextUtils::ClipToFit()方法可以实现通过指定的象素从文本开头或结尾对文本串进行截取.

示例代码

	_LIT(KExampleText,"This is my test for AknTextUtils::ClipToFit");
HBufC * buffer = TPtrC(KExampleText).AllocLC();
TPtr bufPtr = buffer->Des();
//截取最大长度为50个象素的文本.
//TClipDirection 参数可控制从开头截取或从末尾截取,默认情况下从开头进行截取
AknTextUtils:: ClipToFit(bufPtr,*CEikonEnv::Static()->SymbolFont(),50);
LOG->WriteLineL(buffer->Des());
CleanupStack::PopAndDestroy();

输出结果

This is...

默认情况下会在字符缓冲区加入"...",你可以根据需要定制末尾加入的字符.详细信息请参考SDK

根据每行指定的宽度分割文本到多行

以下几组方法提供了按照用户指定的象素宽度分割文本的方法,可以根据指定的宽度分割文本到数组或描述符缓冲区.

 - AknTextUtils::WrapToArrayL ()
 - AknTextUtils::WrapToArrayAndClipL()
 - AknTextUtils::WrapToStringL()
 - AknTextUtils::WrapToStringAndClipL()

后缀为"ClipL"的方法意味着根据指定的最大像素长度对文本进行分割,它可能会把一个单词分成两部分.否则分割以空格区分的单词为单位.


分割字文本到数组

示例代码

	_LIT(KExampleText,"This is my test for AknTextUtils::WrapToArrayL");
HBufC * buffer = TPtrC(KExampleText).AllocLC();
TPtr bufPtr = buffer->Des();
CArrayFix<TInt > * lineWidthArray = new CArrayFixFlat<TInt>(5);
CArrayFix<TPtrC> * wrappedArray = new CArrayFixFlat<TPtrC>(5);
//设定每行的宽度以象素为单位
lineWidthArray->AppendL(50);
lineWidthArray->AppendL(70);
lineWidthArray->AppendL(100);
 
//AknTextUtils::WrapToArrayAndClipL (bufPtr,*lineWidthArray,*CEikonEnv::Static()->SymbolFont(),*wrappedArray);
AknTextUtils::WrapToArrayL(bufPtr,*lineWidthArray,*CEikonEnv::Static()->SymbolFont(),*wrappedArray);
 
for (TInt i = 0; i < wrappedArray->Count(); i++)
{
LOG->WriteLineL(wrappedArray->At(i));
}
CleanupStack::PopAndDestroy();
delete lineWidthArray;
delete wrappedArray;

输出结果

AknTextUtils::WrapToArrayL
This is
my test for
AknTextUtils::


AknTextUtils::WrapToArrayAndClipL
This is
my test for
AknTextUtils::Wr...

分割字文本到以换行符分割的描述符缓冲区

示例代码

	_LIT(KExampleText,"This is my test for AknTextUtils::WrapToStringL");
HBufC * buffer = HBufC::NewLC(200);
TPtr bufPtr = buffer->Des();
CArrayFix<TInt > * lineWidthArray = new CArrayFixFlat<TInt>(5);
 
//设定每行的宽度以象素为单位
lineWidthArray->AppendL(50);
lineWidthArray->AppendL(70);
lineWidthArray->AppendL(100);
//AknTextUtils::WrapToStringAndClipL(KExampleText,*lineWidthArray,*CEikonEnv::Static()->SymbolFont(),bufPtr);
AknTextUtils::WrapToStringL (KExampleText,*lineWidthArray,*CEikonEnv::Static()->SymbolFont(),bufPtr);
LOG->WriteLineL(bufPtr);
CleanupStack::PopAndDestroy();
delete lineWidthArray;

输出结果

AknTextUtils::WrapToStringL
This is
my test for
AknTextUtils::


AknTextUtils::WrapToStringAndClipL
This is
my test for
AknTextUtils::Wr...

过滤和替换字符

AknTextUtils::StripCharacters () 和 AknTextUtils::ReplaceCharacters () 用来过滤和替换字符,以下给出使用示例

过滤字符序列

示例代码

	_LIT(KExampleText,"This *is& my% test for #AknTextUtils::StripCharacters");
HBufC * buffer = TPtrC(KExampleText).AllocLC();
TPtr bufPtr = buffer->Des();
//过滤掉指定的字符序列
AknTextUtils::StripCharacters (bufPtr,_L("*#%&"));
LOG->WriteLineL(bufPtr);
CleanupStack::PopAndDestroy();

结果

This is my test for AknTextUtils::StripCharacters

替换字符序列

示例代码

	_LIT(KExampleText,"This *is& my% test for #AknTextUtils::ReplaceCharacters");
HBufC * buffer = TPtrC(KExampleText).AllocLC();
TPtr bufPtr = buffer->Des();
//替换指定的字符序列为空格
AknTextUtils::ReplaceCharacters (bufPtr,_L("*#%&"),' ');
LOG->WriteLineL(bufPtr);
CleanupStack::PopAndDestroy();

结果

This  is  my  test for  AknTextUtils::ReplaceCharacters 
This page was last modified on 15 December 2011, at 09:20.
160 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.

×