how to consume string from middle and show in UILabel - iphone

I have an String that is "WAKEFIELD - TRINITYIGINY - (3.15 miles)" that need to display like this in a UILabel.That means char is consume from middle.
Note that,it should be dynamic and need to display into UITableViewCell.The strings length is not fix.It is clear that,its only string.
thanks in advance

UILabel has a property for truncation (lineBreakMode)
http://developer.apple.com/library/ios/#documentation/uikit/reference/UILabel_Class/Reference/UILabel.html
If you set it to NSLineBreakByTruncatingMiddleit will truncate in the middle.

You can yet the components from your original string using [myString componentsSeparatedByString:#" - "], then you get an array. Then, create three UILabel with different font in your cell and set lineBreakMode for the second.
If you want to create this three label with cellWidth, you probably creates your cellWidth, you put your first label to the left side of the cell, the third to the right side, and you have some place left in the middle probably. This is the place of your second label. You can calculate the size of your first and third label:
CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:maxPossibleSizeOfTheLabel];

Use the lineBreakMode property of UILabel:
UILabel *label = ...
label.lineBreakMode = NSLineBreakByTruncatingMiddle;

Related

How to display dots in place of long text in UITableViewCell

I have a table view with 2 rows.
in these rows in detail TextLabel i have long text i didn't want to display all the text but
I need to display dots for last characters if it is a long text
for ex:
text in cell.detailTextLabel is :'DATABASE Entered in to the cell'
I want like as : 'DATA BASE Ente.......'
How it is possible?
use label to display and set the property like
cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
cell.textLabel.numberOfLines = 1;
dot will come automatically.
if your text is big compre to label size. then dot will come automatically.
ya you have to fix size of font.
You can set the property of UILabel:
#property(nonatomic) UILineBreakMode lineBreakMode
Set lineBreakMode of UILabel to UILineBreakModeTailTruncation, but it's the default value already. :-)
Try setting adjustFontSizeToFitWidth to NO and set lineBreakMode to UILineBreakModeTailTruncation

Create UIButton on substring with help of NSRange

I have some text coming from server. It may be single line or multiline text. I have to display the text on UILabel, which is no problem for me. The problem is, I have to display UIButton on finding a particular substring of the same text. For example the text is Nitish\n435-234-6543\nIndia which is being displayed as follows :
Nitish
435-234-6543
India
So, when I find 435-234-6543 I have to display UIButton on 435-234-6543.
Notes:
The text is dynamic - coming from server. Above is only an example.
UIButton will be a subview of UILabel.
I tried different ways like OHAttributedLabel, rectForLetterAtIndex and this too. But not getting success. What my idea is, to create the button when substring is found and to set the frame of button based on NSRange of substring. Is this a possibility? How can it be done? Or is there some other way to do this?
I guess it is the approach I am worried about.
-->I have tried to calcluate position but didnt get success. Seems lots of work to calcualate position. One immediate solution come to my mind is why are you not taking one or two labels and one button.
Suppose. You got dynamic string from webservice is: "Nitesh-56789-Test".
Find range of string suppose i.e. "56789".
String before starting location of that range assign to one label i.e. assign "Nitesh" to lable one.
Now add one custom button with our searched string as a text(56789).
Now make sure in main string there something after our substring or not. Here I mean after our search string "56789" still "Test" remain so assign it to third lable.
Here you have to figue out frame of all labels and button using dynamic height width calculation by using sizeWithFont method.
1) Easy solution:
Make your UILabel a UITextView and use the property dataDetectorTypes to have phone numbers as links automatically.
2) More involved solution:
There is a convenient method to determine the size any text will need to be drawn:
CGSize size = [label.text sizeWithFont:label.font
constrainedToSize:CGSizeMake(label.frame.size.width, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
You could now determine which field is the phone number by splitting the string into its lines with:
NSArray *comp = [label.text componentsSeparatedByString:#"\n"];
and then checking which one is numeric. Now you would have to calculate the exact frame from the height of your size variable, maybe like this:
CGFloat positionOfNumber; // the index of your line in comp cast to CGFloat
CGFloat buffer = 10; // fiddle with this
CGFloat buttonHeight = (size.height- 2*buffer)/[comp length];
CGFloat buttonY = buffer + positionOfNumber * buttonHeight;
CGRect buttonFrame = CGRectMake(0, buttonY, label.frame.size.width, buttonHeight);
For those who want perfect solution here's the solution on how to get CGRect of a substring.

How to dynamically change the position of a label according to the string length of another label?

I had two labels label1 and label2 and a tableview ,when the user click the tableview the content of the tableview is displayed in two labels.means if the tableview cell contain stack and 1 ,1 in the text-label.text. i need to display stack in the label1 and 1 in the label2 ,i have done this .but the problem is when the tableview cell content is big i.e.,instead of stack there is stackoverflow it was overlapped with label2 due to big string content
For example stack:1 is ok but when the content is stackoverflow it will appears stackoverflow1
or stackove...1 something like that.so My need is label2 must change its position according to the string length of label1.How to do this?.i hope u understand my question.
Thanks in advance.
You can get string size with the help of following code
CGSize stringSize = [myString sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE]];
Now you can get frame of the first label and then its text size as well. I hope now it will be easy for you to adjust the postion of second label. In case of any problem let me know.
Here is my code for getting number of lines of a label so that I can get the first label's height to add the second label after it.
CGSize stringSize = [myString sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE]];
int labelwidth = firstLabel.width;
int number_of_lines = stringSize.width/labelwidth;
int size = stringSize.width;
int reminder = size % labelwidth;
if(reminder != 0){
number_of_lines++;
}
// now first label height will be
rowHeight = number_of_lines*FONT_SIZE + GAP_BETWEEN_ROWS
Now you can use this rowHeigh as frame.origin.y of your second label.
Best of luck
you can set any frame you your label when you setting new text to it:
[yourLabel setFrame: CGRectMake(x, y, w, h)];
just calculate it according length of new text.
But... do you REALLY need 2 labels? If thay placed in one line and you do so only becouse of you have 2 words, you may do this much easer:
Use 1 label with widgh of all screen and set new text like this:
[yourLabel setText:[NSString stringWithFormat(#"%# %#", text1, text2)]];
Another way to achieve this if you want show on two separate labels you have to calculate the width of first label according to you text set the frame of first label and then add say 5px to the width of first label this will become x position of your next label.

How to calculate dynamic size on scrollview for iphone?

I have dynamic no of textviews and their size can also be dynamic, after each text view there are also dynamic no of labels, and each item is place on scroll view, so that scroll view also has dynamic size, So Someone guide me how to accomplish this task?
forgive me if this is repetitive question plz!
For setting dynamic height of UILabel or UITextView, you can implement following method
This example is for UILabel, Remember, you need to set noOfLines property before setting dynamic height, you can set noOfLines to max number.
NSString *text = #"Your text here";
CGSize size = [text sizeWithFont:lblName.font constrainedToSize:CGSizeMake(lblName.frame.size.width, 10000)];
scrollview.contentSize = CGSizeMake(scrollView.frame.size.width, size.height);
Hope this helps
What you need,
1 calculate textsize which you are going to to show on differnt controls.
for this use this line
[titleString sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(285,9999) lineBreakMode:UILineBreakModeWordWrap];
2 Also use labels instead of textView at each place if you only need to show text.
because textViews justify the text means your same line can be fit in one line but also in two lines
3 set the scrollView contentSize as above answers says.by adding all textSizes with consider some spaces between various controls.
you can set the size of scrollview using setContentSize: and query size [scrollView contentSize]

iphone programming obj-c : removing the "..."

when you enter a too long sentence for the iphone it automaticaly add a "..." at the end to show you there is other stuff you don't see right. I want to delete those "...".
image :
alt text http://img691.imageshack.us/img691/2159/screenshot20100602at095.png
Well, I'm assuming you're using a label. Look into the "lineBreakMode" property. Your solution will probably involve some combination of that property in conjunction with the "numberOfLines" property. For example, setting the "numberOfLines" property to 0 will automatically increase the height of a label to fit all text. So using that with a UILineBreakModeWordWrap would probably do the trick.
UILabel *label = [[UILabel alloc] init];
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
label.text = #"Light beer 5% 10oz Glass served cold";
[label release];
You have several options for that:
Set label's lineBreakMode property to UILineBreakModeClip - that way your sentence will just be clipped without "..." on the end
Set label's adjustsFontSizeToFitWidth property to YES - label will automatically reduce font size to fit string into available space
Make your UILabel have multiple lines - set its numberOfLines property to 0 and lineBreakMode to UILineBreakModeWordWrap. Although with this approach your label's height must be big enough to contain several lines...