We have label with text in 2 lines. In second line the text is not aligning exactly to the left as in the first line. we had set property for the label as number of lines = 0.
for Ex:
label.text = #"This is the text for the label."
In Result We are getting this as
This is the text
for the label.
But we want this as
This is the text
for the label.
try setting the word wrap of button:
[label setNumberOfLines:0]
Edit:
just realised there's linebreak property of label, that should help
label.lineBreakMode = UILineBreakModeWordWrap;
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 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
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.
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...
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.