Remove space from top of text in UILabel - ios

There is a UILabel in UITableView cell. If label have long text then it adds some space on top from text in label. After scrolling this space is automatically remove. I want to remove this space initially. What should i do. I have adjusting height of label using this code:
- (void) adjustHeightOfLbl : (UILabel *) lbl : (NSIndexPath *)indexPath {
lbl.numberOfLines = 0;
NSAttributedString *attributedText =
[[NSAttributedString alloc] initWithString:(lbl.text?:#"")
attributes:#{NSFontAttributeName: lbl.font}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){lbl.frame.size.width, CGFLOAT_MAX}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
CGRect newFrame = lbl.frame;
newFrame.size.height = size.height;
lbl.frame = newFrame;
}

Related

How can I set uitextview size from the attributed HTML string?

I am just developing and application just like news feeding. Some of the cell view I have to set NSAttributedString to textview and get the exact height of textview.
In my NSAttributedString there is HTML content. I have to set in textview because its takes too much time in web view.
The problem is that some of the time I get the perfect height of of textview and some of the time I'm not getting the height of textview. Because ofNSAttributedString some time it considers font height and some time it is not considering it a font height.
You can get more idea if you see my code about what I have done. templbl2 is a UITextview temptext2 is UIView.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *str3 = [NSString stringWithFormat:#"<span style=\"font-family: Frank-regular; font-size: 13\">%#</span>", strTerms];
NSAttributedString * attrStr2 = [[NSAttributedString alloc] initWithData:[str3 dataUsingEncoding:NSUTF8StringEncoding] options:#{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSFontAttributeName : [UIFont fontWithName:#"Frank-regular" size:13.0]} documentAttributes:nil error:nil];
templbl2.attributedText = attrStr2;
[templbl2 sizeToFit];
[self textViewHeightForAttributedText:attrStr2];
}
- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text
{
CGFloat width = [UIScreen mainScreen].bounds.size.width; // whatever your desired width is
CGRect paragraphRect =
[text boundingRectWithSize:CGSizeMake(300.f, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
context:nil];
temptext2.frame = CGRectMake(0, 0, width, paragraphRect.size.height+60);
return paragraphRect.size.height;
}
Try below method:
- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text
{
CGFloat width = [UIScreen mainScreen].bounds.size.width; // whatever your desired width is
UITextView *txtView;
txtView.attributedText = attrStr2;
CGSize size = [tvDummyForHeightTemp sizeThatFits:CGSizeMake(width, FLT_MAX)];
temptext2.frame = CGRectMake(0, 0, width, size.height+60);
return size.height;
}
Hope this will help:)

boundingRectWithSize gives height with truncated text

I am using the following method to determine the height required to display the text that i have. However i cannot get the correct height without truncating the text at the end.
CGRect labelRect = [comment boundingRectWithSize:headerMax
options:NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading
attributes:#{NSFontAttributeName:font}
context:nil];
How can i say not to determine the required label height without truncating the text at the end. I want the whole text to fit into the label.
-(CGRect)getLabelSizeWithText:(NSString *)textLabel forFontSize:(CGFloat) fontSize
{
CGRect textRect;
if (textLabel != nil)
{
NSAttributedString *actualString = [[NSAttributedString alloc] initWithString:textLabel attributes:#{NSFontAttributeName:[UIFont fontWithName:#"HelveticaNeue" size:fontSize]} ];
NSStringDrawingContext *targetContext = [[NSStringDrawingContext alloc] init];
CGFloat commentLabelWidth = [[UIScreen mainScreen]bounds].size.width;
textRect = [actualString boundingRectWithSize:CGSizeMake(commentLabelWidth, 900) options:NSStringDrawingUsesLineFragmentOrigin context:targetContext];
}
return textRect;
}
This one should work. Try it
Use -
1. If you want word wrap -
labelView.lineBreakMode = NSLineBreakByWordWrapping;
labelView.numberOfLines = 2;
CGRect labelRect = [comment boundingRectWithSize:headerMax
options:NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading | NSLineBreakByWordWrapping
attributes:#{NSFontAttributeName:font}
context:nil];
2. If you Don't want truncating tail at all -
// No line break mode
labelView.numberOfLines = 0;
CGRect labelRect = [comment boundingRectWithSize:headerMax
options:NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading
attributes:#{NSFontAttributeName:font}
context:nil];
Try this
 
NSString *cellText = restaurant.review;
UIFont *cellFont = [UIFont systemFontOfSize:14.0];
NSDictionary *attributes = #{NSFontAttributeName: cellFont};
CGRect labelSize = [cellText boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-20, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
I am not really sure whether I understood your question correctly; it sounds like you are trying to calculate the frame size needed to display the text and setting the frame manually with that. Additionally, by setting .numberOfLines = 2, you are limiting the text that you can show. My suggestion would be for you to set up constraints via autolayout, optionally add a .minimumScaleFactor and let it dynamically resize itself.
If you still want to calculate size, with numberOfLines taken into consideration, you can use a prototype label and have use its height
- (CGSize)preferredSizeForString:(NSString *)string
width:(CGFloat)width
{
UILabel *prototypelabel = [UILabel new];
prototypelabel.frame = CGRectMake(0, 0, width, 0);
prototypelabel.text = #"Your string";
prototypelabel.numberOfLines = 2;
[prototypelabel sizeToFit];
return prototypelabel.frame.size;
}

Dynamically adding multiline label in a view

I am dynamically creating UILabel in my app to display a list of directions for a recipe. The labels populate correctly displaying all the items one after another.
The problem is when the text goes on next line of the label, it is overlapped with the next label.
I have set numberOfLines to 0 and also set lineBreakMode to NSLineBreakByWordWrapping. This helps the label to display text on multiple lines.
I have tried to adjust the height of the label, as you will see in the code, but it doesn't work.
How do I prevent the overlapping of labels due to multiline text in label?
Here is the code for populating the labels with multilines:
//add all the directions to the uiview
for (int i = 0; i < self.recipe.directions.count; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0,(i+1)*25,280,25)];
label.lineBreakMode = NSLineBreakByWordWrapping; //multiple lines in a label
label.numberOfLines = 0;
label.text =[NSString stringWithFormat: #"%#", self.recipe.directions[i]];
[label sizeToFit]; // resize the width and height to fit the text
NSLog(#"Actual height is: %f", label.frame.size.height); // Use this for spacing any further elements
CGSize expectedLabelSize = [label.text sizeWithFont:label.font
constrainedToSize:label.frame.size
lineBreakMode:NSLineBreakByWordWrapping];
//adjust the label the the new height.
CGRect newFrame = label.frame;
newFrame.size.height = expectedLabelSize.height;
label.frame = newFrame;
[self.directionsView addSubview:label];
}
Instead of initialising your label with an y position of (i+1)*25, you should store your last label's bottom position
CGFloat lastLabelBottomCoordinate = 25;
CGFloat spaceBetweenLines = 10;
for (int i = 0; i < 10; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, lastLabelBottomCoordinate + spaceBetweenLines,280,25)];
label.lineBreakMode = NSLineBreakByWordWrapping; //multiple lines in a label
label.numberOfLines = 0;
label.text =[NSString stringWithFormat: #"This is a very long text to see if the text have more than 2 lines"];
[label sizeToFit]; // resize the width and height to fit the text
NSLog(#"Actual height is: %f", label.frame.size.height); // Use this for spacing any further elements
CGSize expectedLabelSize = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName : label.font}
context:nil].size;
//adjust the label the the new height.
CGRect newFrame = label.frame;
newFrame.size.height = expectedLabelSize.height;
label.frame = newFrame;
lastLabelBottomCoordinate = label.frame.origin.y + label.frame.size.height;
[self.view addSubview:label];
}
This is how it looks:

UITextView add ...more button at the end of visible text

UITextView add ...more button at the end of visible text like on facebook.
I am sorry if it already mentioned somewhere. Specifically I am interested how to count position of the end of visible text with wrapping words
1 . Check UItextView size base on text and Font:
// return the size for UITextView for both IOS7 and IOS6
-(CGSize) getSizeWithMaxLabelSize:(CGSize) maxLabelSize forTextViewWithText:(NSString *) text
{
CGSize expectedLabelSize;
CGSize maximumLabelSize = maxLabelSize;
if (SYSTEM_VERSION_GREATER_THAN(#"6.2")) {
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont fontWithName:#"Arial" size:EPCommentViewFontSize] forKey: NSFontAttributeName];
CGRect rect =[text boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil];
expectedLabelSize = rect.size;
expectedLabelSize.height = ceilf(expectedLabelSize.height);
expectedLabelSize.width = ceilf(expectedLabelSize.width);
} else {
expectedLabelSize = [text sizeWithFont:[UIFont fontWithName:#"Arial" size:EPCommentViewFontSize] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
}
return expectedLabelSize;
}
2 . Compare your UITextView height with expectedLabelSize.height to show ViewMore or not
3 . Change UITextView frame.size.height when touched viewMore
CGRect frame = _textView.frame;
frame.size.height = [self getSizeWithMaxLabelSize:CGSizeMake(_textView.frame.size.height, NSIntegerMax) forTextViewWithText:_textView.text].height;
_textView.frame = frame;

UILabel with dynamic height into UITableviewcell

I am developing an app which required to display UILabel into UITableviewCell. I also need to resize UILabel as per text size.
I am using following code for get contentsize of text size.
CGRect rect = [as boundingRectWithSize:CGSizeMake(220.0, 2000.0) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName: font} context:nil];
For update frame of UILabel I use following code.
rect.origin.x = cell.lblDescription.frame.origin.x;
rect.origin.y = cell.lblDescription.frame.origin.y;
rect.size.width = cell.lblDescription.frame.size.width;
[cell.lblDescription setFrame:rect];
It set wrong frame.
Please find attached screenshot.
NSString *text = [NSString stringWithFormat:#"%#",[[arr_cart objectAtIndex:indexPath.row] objectForKey:#"name"]];
UIFont *font = [UIFont fontWithName:#"ArialMT" size:12];
CGSize size = [(text ? text : #"") sizeWithFont:font constrainedToSize:CGSizeMake(200, 9999) lineBreakMode:NSLineBreakByWordWrapping];
UILabel *lbl_desc=[[UILabel alloc]init];
lbl_desc.numberOfLines = 0;
lbl_desc.frame=CGRectMake(70,18, size.width, size.height);
lbl_desc.lineBreakMode = NSLineBreakByWordWrapping;
lbl_desc.text = (text ? text : #"");
lbl_desc.font = font;
lbl_desc.backgroundColor=[UIColor clearColor];
lbl_desc.textColor = [UIColor darkTextColor];
[cell.contentView addSubview:lbl_desc];
[lbl_desc release];
You have to check the height of the label in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath UITableViewDelegate Method.
CGSize maximumLabelSize = CGSizeMake(your_label_width, FLT_MAX);
CGSize expectedLabelSize = [label_text sizeWithFont:[UIFont fontWithName:#"your_font" size:your_font_size] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
then set the cell height as expectedLabelSize.height.
also do the same thing in your custom cell.
Use following code
[self setDynamicHeightOfLabel:lblName withLblWidth:97 andFontSize:13];
In above method you just need to pass your UILabel name with specific width and fontSize of label.
-(void) setDynamicHeightOfLabel:(UILabel *) myLabel withLblWidth:(CGFloat) width andFontSize:(int) fontSize
{
CGSize myLabelSize = CGSizeMake(width, FLT_MAX);
CGSize expecteingmyLabelSize = [myLabel.text sizeWithFont:myLabel.font constrainedToSize:myLabelSize lineBreakMode:myLabel.lineBreakMode];
CGRect lblFrame = myLabel.frame;
lblFrame.size.height = expecteingmyLabelSize.height;
myLabel.frame = lblFrame;
int addressLine = myLabel.frame.size.height/fontSize;
myLabel.numberOfLines = addressLine;
}
Using above code you can set dynamic height of UILabel.

Resources