1

In my project, there's a UILabel in each UITableViewCell. Text in each label varies from 1 line to 2 or 3 lines. (I get each text dynamically.) I wonder how I can append another UILabel to the end of each text in UILabel.

I found this Q&A but the author didn't mention solution specifically. Please let me share your ways and problem-solving. Thank you guys in advance!^^

I think I should add some information more. For example, these are 2 labels.


This is the test Label for put in UITableView,

UITableViewCell 01/20



This is another label 01/19


Those dates(01/20, 01/19) that you can see next to each text are the another labels I wanna append. I can't append dates as a string directly cause the normal text and date are have different color and style. I tried 'sizeToFit' as some people told me, but that only show me a frame around whole text. What should I do T_T

Community
  • 1
  • 1
ROMM
  • 15
  • 7
  • Or is it possible to append some string(text) to the end of text in UILabel? That new string(text) has different font and color from those UILabel's text. – ROMM Jan 19 '12 at 14:03
  • As mentionend in the link, can you try with an UIWebView instead of an UILabel? – ott-- Jan 19 '12 at 15:16
  • Thank you for ur comment and then, should I make an web page like .html and call it in UIWebView? Is it ur meaning? – ROMM Jan 20 '12 at 01:52
  • No, just make your label-string look like `NSString *html = @"
    some text
    some more text
    "` and call it with `[webview loadHTMLString:html baseURL:nil];`.
    – ott-- Jan 20 '12 at 10:44
  • Thank you 'ott--' very much!! I could make it thanks to ur help! I'm sad that I can't check your answer as my accepted answer. – ROMM Jan 22 '12 at 07:44

2 Answers2

1

As a variant:

UILabel *someLabel = ...
[someLabel setText:...]
[someLabel sizeToFit]

And then calculate the coordinates of the new insert UILabel.

Ihor Shubin
  • 10,628
  • 3
  • 25
  • 33
  • OK, Thank u for you answer and I understand what u explain. Actually I am a kind of starter, so I hope u can let me know more detail.. – ROMM Jan 20 '12 at 02:00
  • sizeToFit only let me know the whole frame for text... It doesn't detect the exact location(position) of the text ended. – ROMM Jan 20 '12 at 03:19
0
[firstLabel sizeToFit];
CGRect frameForSecondLabel = CGRectMake(firstLabel.frame.origin.x+firstLabel.frame.size.width, firstLabel.frame.origin.y,width,height);
UILabel *secondLabel = [[UILabel alloc] initWithFrame:frameForSecondLabel];

I hope this is what you are looking for

Krrish
  • 2,256
  • 18
  • 21
  • Thank u for ur answer, but I think it's not exact answer for me. I updated my question, so please read my question again... – ROMM Jan 20 '12 at 04:25