Having difficulty wrapping text - ios

I use the following code to add text in IOS
//Set up label frame
UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, 210, 80)];
self.answer_text_label = tempLabel;
[tempLabel release];
[self.preview_answer_container addSubview:self.answer_text_label];
//Populate label with text
self.answer_text_label.text = self.answer.text;
self.answer_text_label.numberOfLines = 4;
self.answer_text_label.lineBreakMode = UILineBreakModeWordWrap;
[self.answer_text_label sizeToFit];
However, the result I get is as such, the text seems to overflow to the right instead of staying within the frame as stipulated in my label setup 'CGRectMake(100, 10, 210, 80)'
The wrapping works if I change to self.answer_text_label.numberOfLines = 0. But this will not work for me since I need to constrain the text within my stipulated label frame.
Any way I can wrap the text and keep to only 4 lines?
EDIT:
Try the suggested code
self.answer_text_label.text = self.answer.text;
[self.answer_text_label sizeToFit];
CGRect labelRect = self.answer_text_label.frame;
labelRect.origin.y = CGRectGetMaxY(labelRect);
labelRect.size = self.answer_text_label.frame.size;
self.answer_text_label.frame = labelRect;
result as follows. Did not seem to solve my problem

Try setting frame explicitly -
[self.answer_text_label sizeToFit];
CGRect labelRect = self.answer_text_label.frame;
labelRect.origin.y = CGRectGetMaxY(labelRect);
labelRect.size = self.answer_text_label.frame.size;
self.answer_text_label.frame = labelRect;
EDIT - Don't need to use this, just use following -
remove these of code just use below, no other property of frame, remove sizeToFit as well -
self.answer_text_label.numberOfLines = 4;
self.answer_text_label.lineBreakMode = UILineBreakModeWordWrap;
For vertical alignment - (With above line of code, use this as well, and do don't use size to fit)
CGSize textSize = [self.answer_text_label.text sizeWithFont:self.answer_text_label.font
constrainedToSize:CGSizeMake(self.answer_text_label.frame.size.width, MAXFLOAT)
lineBreakMode:self.answer_text_label.lineBreakMode];
self.answer_text_label.frame = CGRectMake(20.0f, 20.0f, textSize.width, textSize.height);

In iOS 6 and later, use NSLineBreakByWordWrapping, not UILineBreakModeWordWrap.
self.answer_text_label.lineBreakMode = NSLineBreakByWordWrapping;

Related

UIButton width or line break mode problem

I'm programmatically adding UIButton objects to an UIScrollView. The result I'm getting is that the height of the button is calculated properly, i.e. there is a space for a second line, but the text does not wrap, it rather continues to flow as if the button has infinite width. I used to have UITextView objects instead of the buttons, and that worked flawlessly. I just cannot set the buttons to layout the same way the text views did.
Here's a code snipplet:
UIButton* sButton = [[UIButton alloc] initWithFrame:CGRectMake(0, yPos, sWidth - 5, height)];
sText = [[NSMutableString alloc] initWithString:#"quite a long string that does not fit in one line, no chance"];
sButton.titleLabel.font = font;
sButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
[sButton setLineBreakMode:NSLineBreakByWordWrapping];
[sButton.titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
[sButton setTitle:sText forState:UIControlStateNormal];
[sButton sizeToFit];
And this is the result I'm getting:
How can I make the button text wrap?
This is the only solution I've found that works: https://stackoverflow.com/a/4978003/963022
I ended up creating a custom button implementation and overriding:
- (CGSize)sizeThatFits:(CGSize)size {
int diff = 0;
// for the width, subtract DIFF for the border
// for the height, use a large value that will be reduced when the size is returned from sizeWithFont
CGSize tempSize = CGSizeMake(size.width - diff, 1000);
CGSize stringSize = [self.titleLabel.text
sizeWithFont:self.titleLabel.font
constrainedToSize:tempSize
lineBreakMode:UILineBreakModeWordWrap];
return CGSizeMake(size.width - diff, stringSize.height);
}

Calculate the minimum rectangle around text in UIlabel

I programmatically create some text.
The text gets attributes and then returns to the view controller where is displayed.
Originally i create a rectangle UIlabel with max height 40.
Therefore if the the text is too big the font size decreases to fit into the rectangle by
applying to the text adjustsFontSizeToFitWidth.
If the text is small there is a lot of empty space in the rectangle (on top and below).
Is it possible at that point to get the minimum rectangle eclosing my text.
Thank you
.
NSAttributedString * Text=[circleModel.Selected_set objectForKey:#"sentence_text"];
CGRect recty;
recty= CGRectMake(mainScreen.size.width*0.55, 100, mainScreen.size.width*0.43,40);
UILabel *Latex_text = [[UILabel alloc] initWithFrame:recty];
Latex_text.AttributedText = Text;
Latex_text.numberOfLines = 0;
Latex_text.adjustsFontSizeToFitWidth = YES;
Latex_text.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:Latex_text];
//Latex_text.backgroundColor = [UIColor whiteColor];
What about using textRectForBounds? It should give you at least an estimate if the adjustsFontSizeToFitWidth doesn't play well with it.
UILabel *Latex_text = [[UILabel alloc] init];
Latex_text.AttributedText = Text;
Latex_text.numberOfLines = 0;
Latex_text.adjustsFontSizeToFitWidth = YES;
Latex_text.textAlignment = NSTextAlignmentCenter;
recty.size = [Latex_text textRectForBounds:recty limitedToNumberOfLines:0].size;
[Latex_text setFrame:recty];

Adding UILabels to UIView dynamically

I have a web service that brings some text data back. The idea is to extract the text and create UILabels for the text to show on screen. I however, do not know two things:
How many labels are needed
The length of the text
Due to having no prior knowledge of these things I need a way to create some labels that have the right length for the text it contains.
I've managed to store the data into some objects that are in an array and then iterate through them creating the labels.. something like this:
for (BBItemAttributes *attribute in self.item.productAttributes){
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 50, 10)];
label.text = attribute.displayTemplate;
[self.scrollView addSubview:label];
}
Obviously the problem here is due to creating the UILabels in code and having each one a hard coded CGRect size they are all onto of each other and sometimes don't fit in their respective boxes due to text being too long.
I need a way to line the labels up all on the same X axis point and on different Y axis points, so that they sit next to each other a certain space apart.
Is there a better way to do this?
You need to do something like this;
CGFloat yOrigin = 100;
CGFloat fixedSpace = 10;
for (BBItemAttributes *attribute in self.item.productAttributes)
{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, yOrigin, 50, 10)];
label.text = attribute.displayTemplate;
label.numberOfLines = 0;
[label sizeToFit]; // Resizes label to fit the text.
[self.scrollView addSubview:label];
yOrigin += label.frame.size.height + fixedSpace;
}
Try this. it will help you.
float yAxis = 0;
for (int i = 0; i< 50; i++)//for (BBItemAttributes *attribute in self.item.productAttributes){
{
CGSize size;
UIFont *font = [UIFont systemFontOfSize:15.0]; // Set Your Font
//Your String = attribute.displayTemplate
if (ios7)//Condition to check if ios7
{
//iOS 7
CGRect frame = [#"Your String" boundingRectWithSize:CGSizeMake(50, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName:font}
context:nil];
size = CGSizeMake(frame.size.width, frame.size.height+1);
}
else
{
//iOS 6.0
size = [#"Your String" sizeWithFont:font constrainedToSize:CGSizeMake(50, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
}
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, yAxis, 50, size.height)];
label.text = #"Your String";//attribute.displayTemplate
label.numberOfLines = 0;
label.tag = i; // Set tag if you want to access in future. :)
[self.scrollView addSubview:label];
// Increase yAxis
yAxis = yAxis + size.height + 10;//10 is extra space if you want between two label
}
// Do Not forget to set Contentsize of your ScrollView.
self.scrollView.contentSize = CGSizeMake(320, yAxis + 20);
If your showing a data structure that is a like a list of labels, I would suggest that you use UITableViewController for showing the list.It has the built in facility and properties that make it easy to display a list of objects.
Now the answer to your first Question.You can get the number of labels by determining the number of objects that are in your array. Something like this :
NSInteger *noOfLabels = yourArray.count;
As far as the length of the text is concerned, I suggest not keeping it very lengthy because Labels are not good for lengthy texts. You can however, specify the number of a lines for a particular label in case if it has lengthy text.This will make the text appear in two lines. Though you will have to adjust width and height accordingly.
You can get the text length by doing something similar to this :
NSString *text;
NSInteger *i = (NSInteger*)[text length];
Hope this helps.

How to find all characters in the first line of the uilabel

I want to calculate number of lines and find all characters in the first line of the UILabel dynamically from given text. I'm using wordwrapping for uilabel. Is it possible. Please help and guide me.
try like this
UILabel *thisyourlabel = [[UILabel alloc] initWithFrame:CGRectZero];
thisyourlabel.numberOfLines = 0;
thisyourlabel.lineBreakMode = UILineBreakModeWordWrap;
thisyourlabel.text = #"I want to calculate number of lines and find all characters in the first line of the UILabel dynamically from given text. I'm using wordwrapping for uilabel. Is it possible. Please help and guide me";
CGRect frame = thisyourlabel.frame;
frame.size.width = 200.0f;
frame.size = [thisyourlabel sizeThatFits:frame.size];
thisyourlabel.frame = frame;
CGFloat lineHeight = thisyourlabel.font.leading;
NSUInteger linesInLabel = floor(frame.size.height/lineHeight);
NSLog(#"Number of lines in label: %i", linesInLabel);
[self.view addSubview: thisyourlabel];

iOS, how to draw the dynamic length of text?

The picture is: http://www.flickr.com/photos/71607441#N07/6641626163/
The background is a UIImageView, and the blue part I want to show as "title" of the image.
I used UILabel, but the length of the text is dynamic. It can be one line or two line, at most two line. If the text is longer than two lines, it will be truncated.
The blue part looks like "highlight in Microsoft Word", but it is not "highlight in iOS UILabel.text"
Is there anyone can help me?
Try this code :----
CGSize maximumSize = CGSizeMake(320, 30);
UILabel *newsLabel = [[UILabel alloc] init];
newsLabel.textColor = [UIColor whiteColor];
newsLabel.font = [UIFont boldSystemFontOfSize:11];
newsLabel.backgroundColor = [UIColor clearColor];
newsLabel.lineBreakMode = UILineBreakModeWordWrap;
newsLabel.numberOfLines = 0;
lineBreakMode:newsLabel.lineBreakMode];
CGSize dateStringSize = [#"Text Input" sizeWithFont:newsLabel.font
constrainedToSize:maximumSize
lineBreakMode:newsLabel.lineBreakMode];
CGRect dateFrame = CGRectMake(5, 5, 320, dateStringSize.height); //breath can be any desired float value
newsLabel.text = #"Text Input";
newsLabel.textAlignment = UITextAlignmentCenter;
newsLabel.frame = dateFrame;
You can find the size in pixels required for your title using:
CGSize size = [UILabel.text sizeWithFont:yourFont];
there's also:
CGSize size = [UILabel.text sizeWithFont:yourFont lineBreakMode: yourLineBreakMode];
You could then use these dimensions (size.width, size.height) to set the frame of your UILabel.
Hope this helps. :)

Resources