UIlabel has numberOfLines property. It says my string has 2 lines. But when I use UItextview , I want to split the number of lines based on length of text in UITextview. How can I do it? UIlabel automatically splits, but UITextview does not do it.
The lines should be separated by "\r"
Here's an example:
lblNeedSubscription = [[UILabel alloc] initWithFrame:frame];
[lblNeedSubscription setNumberOfLines:0]; // allows as many lines as needed
[lblNeedSubscription setText:#"To access content\ryou need to be a paid subscriber"];
UITextView inherently allows multiple lines of text. You just separate your lines with the "/n" character and you're done.
Related
i am novice in iPhone.
I have a textView. I am changing the background Color of selected texts in text View.
But problem is that when I am selecting more than 1 line in textView ,only first line color is being changed , not other lines color.
so can anyone tell me about this how can I change background color of all texts which i m selecting.??
tagValue = textView.tag;
NSRange r = textView.selectedRange;
UITextRange *selectedRange = [textView selectedTextRange];
if (!selectedRange)
return;
CGRect result1 = [textView firstRectForRange:selectedRange];
frame_selectedText = result1;
self.str_selected =[NSString stringWithFormat:#"%#", [textView.text substringWithRange:NSMakeRange(r.location, r.length)]];
UIButton *btnView = [UIButton buttonWithType:UIButtonTypeCustom];
[btnView setFrame:result1];
[btnView addTarget:self action:#selector(buttonColorClicked:) forControlEvents:UIControlEventTouchUpInside];
btnView.backgroundColor = [UIColor colorWithRed:220.0f/255.0f green:248.0f/255.0f blue:188.0f/255.0f alpha:0.5];
[textView addSubview:btnView];
The reason this is happening is that firstRectForRange will give you a rectangle that will not cover more than 1 line. As long as the text/selected text remains in one line, the rect will cover that.
Reason being: Imagine you select text that spans to a line and a half. So when you select the text, the selection color will show you that the selection boundary is not a rectangle. It is more like an inverted L. Hence, a single rect cannot cover it.
If you want to highlight just the selected text, you will have to use multiple rects. See my code here. I have covered multiple lines, and words with different rectangles. You can set a color and transparency (alpha) to give a feeling of highlighting. But the drawback here would be, you will not be able to interact with that text.
If you want to create a single rectangle that covers all of your selected text, then it will cover text succeeding the selected text, but you can work with a single rectangle. For this you will have to use firstRectForRange twice. Once on the first word selected, and second on the first word selected in the last line of selected text. The use MAX and MIN to create a single rect that covers all your text.
Alternate method
UITextViews support AttributedTexts. With this you can set text of UITextView with a string with multiple attributes (bold, italic, colored text, colored background etc). Use NSMutableAttributedStrings to store your text. Add attributes like this:
[myAttriButedText addAttribute:NSBackgroundColorAttributeName value:UIColorFromRGB(0x333333) range:NSMakeRange(0, [myAttriButedText length])];
And set the text of UITextView using setAttributedText, or textView.attributedText =. This way, you easily add a background color on your text, without the hassles of all the above mentioned. But if you want more that just the attributes supported by NSAttributedStrings, you will have to use above mentioned methods.
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;
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
As a variant:
UILabel *someLabel = ...
[someLabel setText:...]
[someLabel sizeToFit]
And then calculate the coordinates of the new insert UILabel.
[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
I'm using an underscore character below each form field and related label in my view to logically seperate them. I've added the underscore in a UILabel but I'm looking for a way to repeat the _ without having to manually type it many times in the text property of the label in IB. Any idea on how to do this ?
I tried checking/unchecking "adjust to fit" but it's not working.
Thx for helping
Stephane
This is vague question, but if you want a line, I think you should rather insert generic UIView with backgroundColor that imitates the line.
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(linex, liney, thickness, length)];
line.backgroundColor = [UIColor blackColor];
[view addSubview:line];
[line release];
No matter how you'd look at this, this is much better solution than inserting undersocres.
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...