获取系统字体颜色:
标题:如何从当前主题中获取grid文本颜色
TSS000596
设备, 软件 版本:
S60 2nd Edition, S60 3rd Edition
详细描述
我们可以从当前主题中获取自定义grid(CAknGrid)的文本颜色,但在设置时第二版和第三版略有不同。
解决方案
S60第二版:
正常状态下以及高亮状态下,可以在CCoeControl派生的Container的SizeChanged()方法中设置(该Container拥有grid)。下列是示例代码:
Code:
TRgb textColor; // text color when not highlighted ---- 系统字体颜色
MAknsSkinInstance* skin = AknsUtils::SkinInstance();
AknsUtils::GetCachedColor( skin, textColor, KAknsIIDQsnTextColors, EAknsCIQsnTextColorsCG9 );
TRgb highlightColor; // text color when highlighted
AknsUtils::GetCachedColor( skin, highlightColor, KAknsIIDQsnTextColors, EAknsCIQsnTextColorsCG11 );
iGrid->ItemDrawer()->SetTextColor( textColor ); // iGrid is of type CAknGrid
iGrid->ItemDrawer()->SetHighlightedTextColor( highlightColor );
但这里ItemDrawer()->SetTextColor()方法无法用在S60第三版上。
S60第三版:
设置颜色要通过CFormattedCellListBoxData对象,可以做如下操作:
Code:
TRgb textColor; // text color when not highlighted
MAknsSkinInstance* skin = AknsUtils::SkinInstance();
AknsUtils::GetCachedColor( skin, textColor, KAknsIIDQsnTextColors, EAknsCIQsnTextColorsCG9 );
TRgb highlightColor; // text color when highlighted
AknsUtils::GetCachedColor( skin, highlightColor, KAknsIIDQsnTextColors, EAknsCIQsnTextColorsCG11 );
CFormattedCellListBoxData::TColors colors;
colors.iText = textColor;
colors.iHighlightedText = highlightColor;
iGrid->ItemDrawer()->FormattedCellData()->SetSubCellColorsL( 0, colors );
iGrid->ItemDrawer()->FormattedCellData()->SetSubCellColorsL( 1, colors );
iGrid->ItemDrawer()->FormattedCellData()->SetSubCellColorsL( 3, colors );
上面的代码可以加载在grid的SizeChanged()函数中,否则默认的SizeChanged()会覆盖自定义的操作。注意SetUpFormTextCell()函数的调用必须要在设置文本颜色前调用。
自定义字体大小
CFont* normalFont = NULL;
TFontSpec fontSpec = iEikonEnv->NormalFont()->FontSpecInTwips();
fontSpec.iHeight /= 2;
iCoeEnv->ScreenDevice()->GetNearestFontInTwips(normalFont, fontSpec);
if (normalFont != NULL)
{
// normalFont can be used now!
}
iCoeEnv->ScreenDevice()->ReleaseFont(normalFont);
获得字体的宽度
eg
TInt pos = normalFont->TextWidthInPixels(title->Des());
CFont::CharWidthInPixels(),CFont::MaxCharWidthInPixels(),CFont::MaxNormalCharWidthInPixels(),CFont::TextWidthInPixels()
windcao发表与:http://blog.csdn.net/windcao/archive/2007/09/05/1773637.aspx
先看看代码,由于标签的原有有些内容有误,
gc.DrawText(_L("SymbolFont /x671D/x5916/x5927/x8857"),TPoint(x,baseH));
被显示成了
gc.DrawText(_L("SymbolFont g1DY16Y2757"),TPoint(x,baseH));
实际上/x671D/x5916/x5927/x8857是汉字 朝外大街。
获取固定大小的字体:

void CTestView::Draw( const TRect&
/*aRect*/ ) const
{
// Get the standard graphics context
CWindowGc& gc = SystemGc();

// Gets the control's extent
TRect drawRect( Rect());

// Clears the screen
gc.Clear( drawRect );
TInt baseH=0;
TInt incH=10;
TInt x=0;
const CFont* myFont=NULL;
//--------------------------拉丁字体
myFont=LatinPlain12();
gc.UseFont( myFont );
baseH+=myFont->HeightInPixels()+1;
gc.DrawText(_L("LatinPlain12 g1DY16Y2757"),TPoint(x,baseH));
gc.DiscardFont();
myFont=LatinBold12();
gc.UseFont( myFont );
baseH+=myFont->HeightInPixels()+1;
//gc.DrawText(_L("LatinBold12 g1DY16Y2757"),TPoint(x,baseH));
gc.DrawText(_L("LatinBold12"),TPoint(x,baseH));
gc.DiscardFont();
myFont=LatinBold13();
gc.UseFont( myFont );
baseH+=myFont->HeightInPixels()+1;
//gc.DrawText(_L("LatinBold13 g1DY16Y2757"),TPoint(x,baseH));
gc.DrawText(_L("LatinBold13"),TPoint(x,baseH));
gc.DiscardFont();
myFont=LatinBold16();
gc.UseFont( myFont );
baseH+=myFont->HeightInPixels()+1;
//gc.DrawText(_L("LatinBold16 g1DY16Y2757"),TPoint(x,baseH));
gc.DrawText(_L("LatinBold16"),TPoint(x,baseH));
gc.DiscardFont();
myFont=LatinBold17();
gc.UseFont( myFont );
baseH+=myFont->HeightInPixels()+1;
gc.DrawText(_L("LatinBold17 g1DY16Y2757"),TPoint(x,baseH));
gc.DiscardFont();
myFont=LatinBold19();
gc.UseFont( myFont );
baseH+=myFont->HeightInPixels()+1;
gc.DrawText(_L("LatinBold19 g1DY16Y2757"),TPoint(x,baseH));
gc.DiscardFont();
myFont=NumberPlain5();
gc.UseFont( myFont );
baseH+=myFont->HeightInPixels()+1;
gc.DrawText(_L("1234567890"),TPoint(x,baseH));
gc.DiscardFont();
myFont=ClockBold30();
gc.UseFont( myFont );
baseH+=myFont->HeightInPixels()+1;
gc.DrawText(_L("ClockBold30 g1DY16Y2757"),TPoint(x,baseH));
gc.DiscardFont();
myFont=LatinClock14();
gc.UseFont( myFont );
baseH+=myFont->HeightInPixels()+1;
gc.DrawText(_L("LatinClock14 g1DY16Y2757"),TPoint(x,baseH));
gc.DiscardFont();
//---------------------中文字体
myFont=ApacPlain16();
gc.UseFont( myFont );
baseH+=myFont->HeightInPixels()+1;
// gc.DrawText(_L("ApacPlain16 g1DY16Y2757"),TPoint(x,baseH));
gc.DrawText(_L("ApacPlain16"),TPoint(x,baseH));
gc.DiscardFont();

myFont=ApacPlain12();
gc.UseFont( myFont );
baseH+=myFont->HeightInPixels()+1;
// gc.DrawText(_L("ApacPlain12 g1DY16Y2757"),TPoint(x,baseH));
gc.DrawText(_L("ApacPlain12"),TPoint(x,baseH));
gc.DiscardFont();
}
获取固定比例的字体。

void CTest2View::Draw( const TRect&
/*aRect*/ ) const
{
// Get the standard graphics context
CWindowGc& gc = SystemGc();

// Gets the control's extent
TRect drawRect( Rect());

// Clears the screen
gc.Clear( drawRect );
TInt baseH=0;
TInt incH=10;
TInt x=0;
const CFont* myFont=NULL;

myFont=CEikonEnv::Static()->TitleFont();
gc.UseFont( myFont );
baseH+=myFont->HeightInPixels() +1;
gc.DrawText(_L("TitleFont g1DY16"),TPoint(x,baseH));
gc.DiscardFont();


myFont=CEikonEnv::Static()->NormalFont();
gc.UseFont( myFont );
baseH+=myFont->HeightInPixels()+1;
gc.DrawText(_L("NormalFont g1DY16Y27"),TPoint(x,baseH));
gc.DiscardFont();

myFont=CEikonEnv::Static()->AnnotationFont();
gc.UseFont( myFont );
baseH+=myFont->HeightInPixels()+1;
gc.DrawText(_L("AnnotationFont g1DY16Y2757"),TPoint(x,baseH));
gc.DiscardFont();

myFont=CEikonEnv::Static()->LegendFont();
gc.UseFont( myFont );
baseH+=myFont->HeightInPixels()+1;
gc.DrawText(_L("LegendFont g1DY16Y2757"),TPoint(x,baseH));
gc.DiscardFont();

myFont=CEikonEnv::Static()->SymbolFont();
gc.UseFont( myFont );
baseH+=myFont->HeightInPixels()+1;
gc.DrawText(_L("SymbolFont g1DY16Y2757"),TPoint(x,baseH));
gc.DiscardFont();

myFont=CEikonEnv::Static()->DenseFont();
gc.UseFont( myFont );
baseH+=myFont->HeightInPixels()+1;
gc.DrawText(_L("DenseFont g1DY16Y2757"),TPoint(x,baseH));
gc.DiscardFont();
}
下面是各个版本的s60 平台上面模拟器的截图。

1. 设置 下划线
- gc.SetUnderlineStyle(EUnderlineOn);
2. 设置 颜色
- //使用系统皮肤颜色,当然你也可是使用自己的TRgb
- TRgb textColor; // text color when not highlighted
- MAknsSkinInstance* skin = AknsUtils::SkinInstance();
- AknsUtils::GetCachedColor( skin, textColor, KAknsIIDQsnTextColors, EAknsCIQsnTextColorsCG9 );
- gc.SetPenColor(textColor);
3. 设置 粗体,斜体,高度
-
- //这里使用了一个系统的字体的风格,然后修改它,生成自己的字体
- //当然你也可以直接生成一个TFontSpec,设置它的字体名字和大小
- const CFont* font = AknLayoutUtils::FontFromId(EAknLogicalFontPrimaryFont);
- TFontSpec fontSpec = font->FontSpecInTwips();
- //设置字体平滑
- fontSpec.iFontStyle.SetBitmapType(EAntiAliasedGlyphBitmap);
- //设置字体高度
- fontSpec.iHeight = aStyle.iFontSize;
- //设置粗体
- fontSpec.iFontStyle.SetStrokeWeight(EStrokeWeightBold);
- //设置斜体
- fontSpec.iFontStyle.SetPosture(EPostureItalic);
使用字体
- gc.UseFont(font);
- 还得生成字体
- CFont* useFont = NULL;
- screenDevice.GetNearestFontInTwips(useFont,fontSpec);
还有使用完释放它
- screenDevice.ReleaseFont(font);
乱码是因为字体不对 CScreenDevice.GetNearestFontInTwips找到的不一定就完全好用 调整一下 AknLayoutUtils::FontFromId 或者 font height 一般能解决 或者干脆自己在fontspec中自己指定font family |