Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning half line to the left and the second to the right in CoreText

I am creating a Point-of-Sale (POS) receipt and need to have item names with a certain width and need to have a price on the right (like float-right in CSS).

I am using https://github.com/FuerteInternational/FTCoreText to create the content. Any ideas how to achieve this without having to create two views and place them on top of each other?

Just to clarify, I need to have this:

Coffee                  £1.45
   milk, skinny
Danish Pastry           £1.75
Coca Cola               £1.10
Full English Bfst      £12.50

So pretty much I need a string to be floating on the left and other string floating on the right ...

like image 753
Ondrej Rafaj Avatar asked Dec 18 '13 09:12

Ondrej Rafaj


People also ask

How to align text to left and right in Word document?

In the Word file that you want to insert the text and align to left and right, and then, click Home, in the Paragraph group, click the Paragraph Settings icon, see screenshot: 2. In the Paragraph dialog box, select Left from the Alignment drop down, and then, click Tabs button, see screenshot: 3.

Can I align content to the left and right margins?

You can’t align content to the left and right margins on the same line using Microsoft Word’s alignment options, but you can still do it using a special tab. We may be compensated by vendors who appear on this page through methods such as affiliate links or sponsored partnerships.

How to insert text directly from right hand side of keyboard?

After entering the text in right hand, please put the cursor at the left location where you want to insert text, enter the first line text normally, press the Down arrow in the keyboard to enter a new line (or, you can put the cursor at the next beginning line then type directly). Now, you will get the result as you need, see screenshot:

How to change the position of right hand text in word?

(1.) Enter a tab number into the Tab stop position text box that means the position where you would like the right hand text to end; (2.) Check Right option in the Alignment section; (3.) Then, click Set button; (4.) At last, click OK button to close this dialog box.


2 Answers

The only thing I can think of is inserting in each string f.e. 30 spaces after the first string and assigning wordWrap = truncate middle. Not so sure that this is exactly what you're looking for.

I agree with Brian Shamblen it'l be pretty easy to do with 2 labels.

like image 163
Stas Zhukovskiy Avatar answered Oct 22 '22 09:10

Stas Zhukovskiy


It looks like what you want isn't really a second column, which Core Text cannot do out of the box, but rather a tabular layout (like good ol' <table>). Fortunately tabular layout can be acheived in Core Text by using tab stops. The only 2 things you need are:

  • the CHARACTER TABULATION (U+0009) character,
  • and a paragraph style with the correct tab stops setting.

That can be done this way:

NSString *string = @"Coffee\t£1.45\n  milk, skinny\nDanish Pastry\t£1.75\nCoca Cola\t£1.10\nFull English Bfst\t£12.50";
CGFloat width = 200;

NSArray *tabs = @[CFBridgingRelease(CTTextTabCreate(kCTTextAlignmentRight, width, NULL))];
CTParagraphStyleSetting setting = {kCTParagraphStyleSpecifierTabStops, sizeof(tabs), &tabs};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(&setting, 1);
NSDictionary *attributes = @{CFBridgingRelease(kCTParagraphStyleAttributeName): CFBridgingRelease(paragraphStyle)};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:string attributes:attributes];

There are two caveats:

  • You need to know the width of the view when creating the paragraph style.
  • Core Text won't wrap lines on tab stops which means you have to make sure your text is short enough to fit.
like image 1
Nicolas Bachschmidt Avatar answered Oct 22 '22 10:10

Nicolas Bachschmidt