×

Discussion Board

Results 1 to 9 of 9

Threaded View

  1. #1
    Registered User
    Join Date
    Sep 2012
    Location
    Bandung, Indonesia
    Posts
    27

    DrawText Problem - Texts on Same Line

    Hi,

    As an alternative to CEikLabel which has always caused much trouble for me, as explained here, I decided to draw text using the DrawText() function.

    The problem I am having with DrawText is: I have two texts which I intend to draw on the SAME line (i.e. the same Y position), one at 5 pixels from the left margin and the other would be 5 pixels from the right. However, I can only draw either of them, not both.
    Code:
    void CPengawas_Logs::Draw( const TRect& aRect ) const
    	{
    	CWindowGc& gc = SystemGc();
    	gc.Clear();
    	gc.SetBrushColor(KRgbDarkBlue);
    	gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
    	gc.SetPenColor(KRgbYellow);
    	TRect rect(0, 0, aRect.Width(), 20);
    	gc.DrawRect(rect);
    
    	_LIT(KFontName, "LatinPlain12");
    	const TInt KFontSize = 130;
    	TFontSpec fontSpec(KFontName, KFontSize);
    	fontSpec.iFontStyle.SetStrokeWeight(EStrokeWeightBold);
    	CGraphicsDevice* screenDevice = iEikonEnv->ScreenDevice();
    	CFont* font;
    	screenDevice->GetNearestFontInTwips(font, fontSpec);
    	
    	gc.UseFont(font);
    	screenDevice->ReleaseFont(font);
    
    	const TInt KOffset = 5;
    	TInt iItemNo_Width = font->TextWidthInPixels(iItemNoText);
    	TInt iYPos = (rect.Height() + font->AscentInPixels()) / 2;
    	
    	gc.DrawText(iDateTimeText, rect, iYPos, CGraphicsContext::ELeft, KOffset); //"Date and Time" text
    	gc.DrawText(iItemNoText, rect, iYPos, CGraphicsContext::ELeft, rect.Width()-KOffset-iItemNo_Width); //"Item No" text in the format of "1/1000" (lots of items as this lists SMS/Call logs)
    
    	gc.DiscardFont();	
    	}
    The calling function which calls the Draw() method is:
    Code:
    void CPengawas_Logs::DisplayDateTimeAndItemNo(const TInt iItemCount)
    	{
    		TBuf<20> iDateTimeParts = iListBox->Model()->ItemText(iActiveIndex).Right(20);
    		iDateTimeText = iDateTimeParts.Left(19);
    		iItemNoText = _L("");
    		iItemNoText.AppendNum(iActiveIndex+1);
    		iItemNoText.Append(_L("/"));
    		iItemNoText.AppendNum(iItemCount);
        	
        	       DrawNow();
    	}
    Only one of the two strings was printed, either the first or the second, but not both, depending on which one was not commented out.
    Then I "improvised" by using a series of space characters to separate the first and second text.
    Code:
    void CPengawas_Logs::Draw( const TRect& aRect ) const
    	{
    	CWindowGc& gc = SystemGc();
    	gc.Clear();
    	gc.SetBrushColor(KRgbDarkBlue);
    	gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
    	gc.SetPenColor(KRgbYellow);
    	TRect rect(0, 0, aRect.Width(), 20);
    	gc.DrawRect(rect);
    
    	_LIT(KFontName, "LatinPlain12");
    	const TInt KFontSize = 130;
    	TFontSpec fontSpec(KFontName, KFontSize);
    	fontSpec.iFontStyle.SetStrokeWeight(EStrokeWeightBold);
    	CGraphicsDevice* screenDevice = iEikonEnv->ScreenDevice();
    	CFont* font;
    	screenDevice->GetNearestFontInTwips(font, fontSpec);
    	
    	gc.UseFont(font);
    	screenDevice->ReleaseFont(font);
    
    	_LIT(KSpace, " ");
    	const TInt KOffset = 5;
    	TInt iDateTime_Width = font->TextWidthInPixels(iDateTimeText);
    	TInt iItemNo_Width = font->TextWidthInPixels(iItemNoText);
    	TInt iSpace_Width = font->TextWidthInPixels(KSpace); 
    	TInt iAllTexts_Width = rect.Width()- (2*KOffset);
    	TInt iRemainingWidth = iAllTexts_Width - (iDateTime_Width + iItemNo_Width);
    	TInt iSpaceCount = 0;
    	if (iRemainingWidth > 0)
    		{
    		iSpaceCount = (iRemainingWidth/iSpace_Width);
    		if (iSpaceCount < 0) iSpaceCount = 0;
    		}
    	TInt iDateTimeText_Width = iDateTimeText.Length();
    	TInt iItemNo_Width = iItemNoText.Length();
    	
    	RBuf iInformationText;
    	iInformationText.CreateL(iDateTimeText_Width+iSpaceCount+iItemNo_Width);
    	CleanupClosePushL(iInformationText);
    	
    	iInformationText.Copy(iDateTimeText);	
    	if (iSpaceCount > 0)
    		for (TInt i=0; i<iSpaceCount; i++) iInformationText.Append(KSpace);
    	iInformationText.Append(iItemNoText);
    	
    	iAppUi->SaveToLog(KLogFile, iInformationText);
    	
    	TInt iYPos = (rect.Height() + font->AscentInPixels()) / 2;
    	
    	//gc.DrawText(iDateTimeText, rect, iYPos, CGraphicsContext::ELeft, KOffset);
    	//gc.DrawText(iItemNoText, rect, iYPos, CGraphicsContext::ELeft, rect.Width()-KOffset-iItemNo_Width);
    	
    	gc.DrawText(iInformationText, rect, iYPos, CGraphicsContext::ELeft, KOffset);
    	
    	CleanupStack::PopAndDestroy(&iInformationText);
    
    	gc.DiscardFont();	
    	}
    This one worked.

    My question: Does DrawText print only one "text" on a single line? I haven't tried drawing more than one line, though, but I will for another container soon after I find the method used for this one sufficient.

    Thanks in advance.

    P.S. The above variable names are converted/translated to English for the ease of understanding. There might be some words incorrectly replaced using Find/Replace in Notepad.
    P.P.S. I've googled a lot and browsed the forum/DiBo and all that (as I always do everyday since I began writing this app) but have found no similar problem (i.e. drawing "twice" on the same line). Others mention drawing problems with multiline texts, which I will have to take into account when I am at the next container real soon.

    Regards,
    Asep
    Last edited by asepm; 2013-02-07 at 10:00.

Similar Threads

  1. How to change line spacing in QPainter::drawText
    By ziggx in forum [Archived] Beginning Qt
    Replies: 2
    Last Post: 2011-01-30, 17:50
  2. copying texts 5800 problem.
    By vegeta_sayf in forum [Archived] Flash Lite on Nokia Devices
    Replies: 3
    Last Post: 2010-04-15, 13:42
  3. Problem with DrawText..
    By gb0071 in forum Symbian
    Replies: 9
    Last Post: 2009-03-04, 09:46
  4. Problem with DrawText()
    By gowthamgowtham in forum Symbian
    Replies: 3
    Last Post: 2008-02-24, 12:19
  5. New line in DrawText
    By kaisub in forum Symbian
    Replies: 1
    Last Post: 2003-05-30, 11:15

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×