4

I need to a have smart multiline cut of my long multiline string of text.

Originally I have the following: enter image description here

On the screen above I pointed the narrow region which I actually have for showing the text. Finally I want to get the following:

enter image description here

The main idea which I look for is how I can truncate multiline text as a whole (not separating it by strings, truncating each of them and then concatenating back).

In more detail, I have 3 strings separated by \n and concatenated in one long string.

I want to put this long string in the definite UILabel as attributed string under the following conditions:

  1. the first string must have 1 line
  2. the 2nd and the 3rd string must have 2 lines maximally

So I want to truncated not only tail of the whole long string by label frame but tail of each string separated by \n independently.

I know that I could do it dynamically: split each substring by words and concatenate words until the will need more then maximally allowed lines.

But I believe that there is some elegant way to do this using formatting or CoreText.

malex
  • 9,874
  • 3
  • 56
  • 77
  • 1
    use `UILabel` word-wrap and set `UILabel` frame width – falcon143 Jul 30 '14 at 11:25
  • There was an example of advanced tail truncation using Text Kit (altough the case was different than yours) on WWDC: https://developer.apple.com/videos/wwdc/2013/ session "Advanced Text Layouts and Effects with Text Kit" – Michał Ciuba Jul 30 '14 at 12:41
  • @RameshKumar And how can I control number of lines of separate substring in the whole UILabel text? Suppose that it is essentially different problem. – malex Jul 30 '14 at 13:22
  • @malex hope this link help you http://stackoverflow.com/questions/15113461/decrease-the-width-of-the-last-line-in-multiline-uilabel – falcon143 Jul 31 '14 at 04:21
  • I would do it but the bounty is too small :) – Rafał Sroka Aug 01 '14 at 14:34
  • @reecon, The bounty is not for full new solution (grand parser with huge amount of code). It would be nice to get some simple idea from coreText and attributed string formatting. – malex Aug 01 '14 at 14:37
  • Ok got it. I'm on it :) – Rafał Sroka Aug 01 '14 at 14:41
  • I dont understand why you dont just use 3 separate `UILabel` containers arranged in a stack. – Brad Allred Aug 02 '14 at 21:12
  • @BradAllred, the main task is to show a tableView with such arbitrary content in one label. It seems that using of one attributed string of dynamic height could be good solution. The 1st tableCell could contain 1 string without \n, but some another string could contain 10 separated strings. So we can for sure dynamically add and remove appropriate number of label on drawing but the idea with one formatted attributed string is very tasty. – malex Aug 02 '14 at 21:18

1 Answers1

0

You could use some elegant regexp :)

s/^(.{0,80}).*$/\1/g

this one, for example, will leave only 80 first symbols from every substring.

s/^(.{1,80})(?<!\s)(?!\w)/\1/g

and this one will left no more than 80, but break only in whitespace

Arenim
  • 4,097
  • 3
  • 21
  • 31
  • The main problem is to count dynamically the number of letters for each string. No I have the similar realization and it is not appropriate, looks not good. – malex Aug 05 '14 at 20:14