There are 3 UIlabel in my TableViewCell.xib.One of them need to set attributedText with NSMutableAttributedString,the code like this:
NSString * htmlString = [NSString stringWithFormat:#"将订单状态从 %# 改变为 %# ,原因:%#",lb.order_status,lb.changed_status,lb.remark];
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:#{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineBreakMode = NSLineBreakByTruncatingTail;
style.lineHeightMultiple = 0;
[attrStr addAttribute:NSParagraphStyleAttributeName
value:style
range:NSMakeRange(0, attrStr.length)];
[attrStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Palatino-Roman" size:13.0] range:NSMakeRange(0, attrStr.length)];
lbRemark.attributedText=attrStr;
Question 1:
lbRemark cannot display with multiple-lines, but if I do not use NSAttributedString to set lbRemark then it can display with multiple-lines.Is there something wrong with NSAttributedString?
Question 2: I need my Cell to calculate it's height with lbRemark' height.
Since iOS8, you don't need to calculate the height of the label. Auto-layout will do that for you.
Attach top and bottom constraints between the UILabel and the cell - do not set a height constraint for the UILabel.
In the viewDidLoad: method of your UIViewController, just do the following (replace the 50.0f with the default height of your rows):
self.tableView.estimatedRowHeight = 50.0f;
self.tableView.rowHeight = UITableViewAutomaticDimension;
1: Set this for multiple lines:
lbRemark.numberOfLines = 0;
2: This is how to calculate height of label according to text:
NSString * htmlString = [NSString stringWithFormat:#"将订单状态从 %# 改变为 %# ,原因:%#",#"test",#"test1",#"test2"];
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:#{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineBreakMode = NSLineBreakByTruncatingTail;
style.lineHeightMultiple = 0;
[attrStr addAttribute:NSParagraphStyleAttributeName
value:style
range:NSMakeRange(0, attrStr.length)];
[attrStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Palatino-Roman" size:13.0] range:NSMakeRange(0, attrStr.length)];
lbRemark.numberOfLines = 0;
CGRect paragraphRect = [attrStr boundingRectWithSize:CGSizeMake(625, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
context:nil]; //here 625 is width of label, use whatever your width
NSLog(#"height = %f", paragraphRect.size.height); //it should be height for label
lbRemark.attributedText=attrStr;
Hum...finally I got a solution for it.It's not anything wrong with NSAttributedString.I implement a function(my workmate who often helped me offer it ):
static UITableViewCell *sizingLogCell = nil;
static dispatch_once_t onceToken;
if ([indentifier isEqualToString:#"logCell"]) {
dispatch_once(&onceToken, ^{
sizingLogCell = [tableView dequeueReusableCellWithIdentifier:indentifier];
});
sizingLogCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(tableView.bounds));
[sizingLogCell setNeedsLayout];
[sizingLogCell layoutSubviews];
[self configureLogCell:sizingLogCell forIndexPath:indexPath];
CGSize size = [sizingLogCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height + 1;
}
The function's name is :
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath indentifierForCell:(NSString *)indentifier
The most important thing is 'size.height + 1',if without '+1' label still can not display with multi-lines.
Related
I added text in UITextView and to understand the boundaries I colored my UITextView with cyan color.
After google.com there is some extra space which I want to remove.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *desc = [self htmlAfterReplacingTagsAndAddingStyle];
_descriptionTextView.attributedText = [[NSAttributedString alloc] initWithData: [desc dataUsingEncoding:NSUTF8StringEncoding] options: #{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
_descriptionTextView.linkTextAttributes = #{ NSForegroundColorAttributeName: [UIColor blueColor], NSUnderlineStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle] };
_descriptionTextView.dataDetectorTypes = UIDataDetectorTypeLink;
_descriptionTextView.scrollEnabled = NO;
[self setTextViewHeight];
}
setTextViewHeight
- (void)setTextViewHeight {
UIFont *font = [UIFont fontWithName:#"OpenSans" size:14.0];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _descriptionContainer.frame.size.width, CGFLOAT_MAX)];
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.font = font;
NSString *desc = [self htmlAfterReplacingTagsAndAddingStyle];
label.attributedText = [[NSAttributedString alloc] initWithData: [desc dataUsingEncoding:NSUTF8StringEncoding] options: #{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
[label sizeToFit];
_descriptionTextView.backgroundColor = UIColor.cyanColor;
[_descriptionTextView setContentInset:UIEdgeInsetsMake(0, 0, -80, 0)];
_descriptionContainerHeight.constant = label.frame.size.height;
}
I read about setting content inset but that is not helpful. Changing the value of -80 doesn't have any impact.
I would appreciate some pointers which can help me remove the extra space present at the bottom of uitextView.
Use
[_descriptionTextView setTextContainerInset:UIEdgeInsetsMake(0, 0, 0, 0)];
as can be readed in UITextView Class reference
textContainerInset Property The inset of the text container's layout
area within the text view's content area.
Declaration
OBJECTIVE-C
#property(nonatomic, assign) UIEdgeInsets
textContainerInset
Discussion
This property provides text margins for
text laid out in the text view. By default the value of this property
is (8, 0, 8, 0).
Hope this helps
You should be able to get somewhere with textContainerInset (see #ReinierMelian answer). There's also opportunity to vastly simplify the text height measurement...
UIFont *font = [UIFont fontWithName:#"OpenSans" size:14.0];
CGFloat width = _descriptionTextView.bounds.size.width;
NSString *string = [self htmlAfterReplacingTagsAndAddingStyle];
CGRect rect = [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{ NSFontAttributeName:font } context:nil];
CGRect frame = _descriptionTextView.frame;
frame.size = CGSizeMake(width, rect.size.height);
_descriptionTextView.frame = frame;
// due to #ReinierMelian...
_descriptionTextView.extContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
To remove all padding:
[textView setTextContainerInset:UIEdgeInsetsZero];
textView.textContainer.lineFragmentPadding = 0; // to remove left padding
If you want to remove Multiple new lines into single one,
In textViewDidEndEditing:
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"\n+" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *trimmedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:#" "];
textView.text = trimmedString;
Hello I'm working on chat app in which user can send smiley (emoji) to other user. For text its working perfect but when it contains emoji then it is correct. I have used method for this
- (CGSize)getSizeForText:(NSString *)text maxWidth:(CGFloat)width font:(NSString *)fontName fontSize:(float)fontSize {
CGSize constraintSize;
constraintSize.height = MAXFLOAT;
constraintSize.width = width;
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:fontName size:fontSize], NSFontAttributeName,
nil];
CGRect frame = [text boundingRectWithSize:constraintSize
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:attributesDictionary
context:nil];
CGSize stringSize = frame.size;
return stringSize;
}
and the results are something like
As from images you can see what i want to say and what i need. I need some between two lines when there is emoji text in UILabel. Any help will be appreciated. Thanks in advance.
Edit: I have given answer on this. But if some have some quick hack for this then that will be great for me.
I have made a good work to do it manually and it looks good for me now. I have added linespacing for label text and it works good for me. I have set text like
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#",message_text]];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:4];
[attrString addAttribute:NSParagraphStyleAttributeName
value:style
range:NSMakeRange(0, [message_text length])];
lblChatMSG.attributedText = attrString;
and my method to get height of text is
- (CGSize)getSizeForText:(NSString *)text maxWidth:(CGFloat)width font:(NSString *)fontName fontSize:(float)fontSize {
CGSize constraintSize;
constraintSize.height = MAXFLOAT;
constraintSize.width = width;
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
paragraphStyle.lineSpacing = 4;
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:fontName size:fontSize], NSFontAttributeName,paragraphStyle,NSParagraphStyleAttributeName,
nil];
CGRect frame = [text boundingRectWithSize:constraintSize
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:attributesDictionary
context:nil];
CGSize stringSize = frame.size;
return stringSize;
}
And the result is
I just increased the required height of the label and adjusted its origin.
// Expand label to avoid cutoff emojis
label.frame.size.height += 8.0
label.frame.origin.y -= 4.0
I am creating a UIScrollView and setting the attributedText. For some reason the text was not appearing...until I actually scrolled down. It seems the text is being aligned to the bottom of the UIScrollView, and I have no idea why?!
NSString *description = NSLocalizedString(#"forgot.description", nil);
NSMutableAttributedString *paragraph = [[NSMutableAttributedString alloc] initWithString:description attributes:#{}];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:4];
[paragraph addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [paragraph length])];
[paragraph addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, [paragraph length])];
[paragraph addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:43/255.0f green:47/255.0f blue:61/255.0f alpha:1.0f] range:NSMakeRange(0, [paragraph length])];
_descriptionTextView = [[UITextView alloc] init];
_descriptionTextView.backgroundColor = [UIColor whiteColor];
_descriptionTextView.attributedText = paragraph;
_descriptionTextView.userInteractionEnabled = NO;
_descriptionTextView.scrollEnabled = NO;
_descriptionTextView.clipsToBounds = NO;
CGFloat const ComponentWidth = 280.0;
CGFloat const ComponentHeight = 40.0;
self.descriptionTextView.frame = CGRectIntegral(CGRectMake(x, y, ComponentWidth, ComponentHeight));
[self.view addSubview:self.descriptionTextView];
I think this has to do with the navigationBar when I set it to not hidden
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO animated:NO];
[super viewWillAppear:animated];
}
I know there are many links to get the height of the label within the cell. I have tried every thing .. nothing is working ..
My scenario is :
I have a cell , in this cell i have 6 labels , text within these labels are dynamic and coming from a web service.
I dont know what i am doing wrong , i have applied every thing to get the dynamic height right:
i have 2 issues:
1) when tableview loads first time heights of labels are not proper (its according to nib).
2) when i reload table view it gives me the dynamic height , but its not correct.
here is my code:
In controller where i am calculating to height of cell each time :
-(float)saltDetailsCellHeight : (HKDrugSaltInfoModel *)saltInfoModel
{
UIColor *_black=[UIColor blackColor];
UIColor *_lightGray = [UIColor lightGrayColor];
UIFont *font=[UIFont fontWithName:#"HelveticaNeue" size:13.0f];
// salt Heading
NSString * saltNameLabelString = [NSString stringWithFormat:#"%# %#",saltInfoModel.saltName,saltInfoModel.strength];
float hSaltNameLabelString = [HKGlobal heightOfGivenText:saltNameLabelString ofWidth:300.0 andFont:font];
// Salts string
NSString * saltsString = [NSString stringWithFormat:#"Pregnancy:%# Lactation:%# Lab:%# Food:%#",saltInfoModel.ci_pg,
saltInfoModel.ci_lc,saltInfoModel.ci_lb,saltInfoModel.ci_fd];
float hSaltsString = [HKGlobal heightOfGivenText:saltsString ofWidth:300.0 andFont:font];
// Usage Label
NSString *usageString = [NSString stringWithFormat:#"Typical Usage: %#",saltInfoModel.lc];
NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString:usageString];
NSInteger _stringLength=[usageString length];
[attrStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attrStr addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, 14)];
[attrStr addAttribute:NSForegroundColorAttributeName value:_lightGray range:NSMakeRange(15, _stringLength-15)];
float hUsageString = [HKGlobal heightOfGivenText:[NSString stringWithFormat:#"%#", (NSString *)attrStr] ofWidth:300.0 andFont:font];
// Side Effects label
NSString *sideEffectsString = [NSString stringWithFormat:#"Side Effects: %#",saltInfoModel.se];
NSMutableAttributedString* attrStrSide = [[NSMutableAttributedString alloc] initWithString:sideEffectsString];
NSInteger _stringLengthSE = [sideEffectsString length];
[attrStrSide addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLengthSE)];
[attrStrSide addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, 14)];
[attrStrSide addAttribute:NSForegroundColorAttributeName value:_lightGray range:NSMakeRange(15, _stringLengthSE-15)];
float hSideEffectsString = [HKGlobal heightOfGivenText:[NSString stringWithFormat:#"%#",(NSString *)attrStrSide] ofWidth:300.0 andFont:font];
// Drug Interaction Label
NSString *drugInteractionString = [NSString stringWithFormat:#"Drug Interaction: %#",saltInfoModel.di];
NSMutableAttributedString* attrdrugInteractionStr = [[NSMutableAttributedString alloc] initWithString:drugInteractionString];
NSInteger _strLenDrugInteraction = [drugInteractionString length];
[attrdrugInteractionStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _strLenDrugInteraction)];
[attrdrugInteractionStr addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, 18)];
[attrdrugInteractionStr addAttribute:NSForegroundColorAttributeName value:_lightGray range:NSMakeRange(18, _strLenDrugInteraction-18)];
float hAttrdrugInteractionStr = [HKGlobal heightOfGivenText:[NSString stringWithFormat:#"%#", (NSString*)attrdrugInteractionStr] ofWidth:300.0 andFont:font];
// MechanismOfAction Label
NSString *moaString = [NSString stringWithFormat:#"Mechanism Of Action: %#",saltInfoModel.moa];
NSMutableAttributedString* attrMoaStr = [[NSMutableAttributedString alloc] initWithString:moaString];
NSInteger _strLenMoa=[moaString length];
[attrMoaStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _strLenMoa)];
[attrMoaStr addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, 21)];
[attrMoaStr addAttribute:NSForegroundColorAttributeName value:_lightGray range:NSMakeRange(21, _strLenMoa-21)];
float hAttrMoaStr = [HKGlobal heightOfGivenText:[NSString stringWithFormat:#"%#", (NSString*)attrMoaStr] ofWidth:300.0 andFont:font];
arrayOFLabelsHeights = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithFloat:hSaltNameLabelString],
[NSNumber numberWithFloat:hSaltsString],
[NSNumber numberWithFloat:hUsageString],
[NSNumber numberWithFloat:hSideEffectsString],
[NSNumber numberWithFloat:hAttrdrugInteractionStr],
[NSNumber numberWithFloat:hAttrMoaStr],
nil];
float cellHeight = hSaltNameLabelString+10+hSaltsString+10+hUsageString+10+hSideEffectsString+10+hAttrdrugInteractionStr+10+hAttrMoaStr;
return cellHeight;
}
and i am using above method in tableview:heightForRowAtIndexpath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self saltDetailsCellHeight:[self.drugDetailModel.saltInfoArray objectAtIndex:indexPath.row-3]];
}
And in cell for row at index path i am PASSSing the arrayOFLabelsHeights array , which contains the height of each label :
HKMedicineDetailInfoCell *medicineDetailInfoCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
if(medicineDetailInfoCell == nil)
{
medicineDetailInfoCell = (HKMedicineDetailInfoCell *)[[[NSBundle mainBundle] loadNibNamed:#"MedicineDetailInfoCell" owner:self options:nil] objectAtIndex:0];
}
//Check whether array contains any object or not
if([self.drugDetailModel.saltInfoArray count] > 0)
{
[medicineDetailInfoCell customiseLabels:[self.drugDetailModel.saltInfoArray objectAtIndex:indexPath.row-3] WithHeights:arrayOFLabelsHeights]; // since 3 rows are already there
}
return medicineDetailInfoCell;
And the method customiseLabels: is declared in my custom cell class like this (i think there would be code duplicacy but i have to do this, not able to find any other way):
-(void)customiseLabels:(HKDrugSaltInfoModel *)saltInfoModel WithHeights:(NSArray *)heightsArray
{
UIColor *_black=[UIColor blackColor];
UIColor *_lightGray = [UIColor lightGrayColor];
//Helvetica Neue
UIFont *font=[UIFont fontWithName:#"HelveticaNeue" size:13.0f];
float h1 = [[heightsArray objectAtIndex:0] floatValue];
frameTemp = self.saltNameLabel.frame;
frameTemp.size.height = h1;
self.saltNameLabel.frame = frameTemp;
self.saltNameLabel.text = [NSString stringWithFormat:#"%# %#",saltInfoModel.saltName,saltInfoModel.strength];
frameTemp.origin.y += h1+10;
float h2 = [[heightsArray objectAtIndex:1] floatValue];
frameTemp.size.height = h2;
self.secondDescriptiveLabel.frame = frameTemp;
self.secondDescriptiveLabel.text = [NSString stringWithFormat:#"Pregnancy:%# Lactation:%# Lab:%# Food:%#",saltInfoModel.ci_pg,saltInfoModel.ci_lc,saltInfoModel.ci_lb,saltInfoModel.ci_fd];
frameTemp.origin.y += h2+10;
float h3 = [[heightsArray objectAtIndex:2] floatValue];
frameTemp.size.height = h3;
self.usageLabel.frame = frameTemp;
// Usage Label
NSString *str = [NSString stringWithFormat:#"Typical Usage: %#",saltInfoModel.lc];
NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString:str];
NSInteger _stringLength=[str length];
[attrStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attrStr addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, 14)];
[attrStr addAttribute:NSForegroundColorAttributeName value:_lightGray range:NSMakeRange(15, _stringLength-15)];
self.usageLabel.attributedText = attrStr;
frameTemp.origin.y += h3+10;
float h4 = [[heightsArray objectAtIndex:3] floatValue];
frameTemp.size.height = h4;
self.sideEffectsLabel.frame = frameTemp;
// Side Effects label
NSString *sideEffectsString = [NSString stringWithFormat:#"Side Effects: %#",saltInfoModel.se];
NSMutableAttributedString* attrSideEffectsStr = [[NSMutableAttributedString alloc] initWithString:sideEffectsString];
NSInteger _strLenSideEffects=[sideEffectsString length];
[attrSideEffectsStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _strLenSideEffects)];
[attrSideEffectsStr addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, 14)];
[attrSideEffectsStr addAttribute:NSForegroundColorAttributeName value:_lightGray range:NSMakeRange(14, _strLenSideEffects-14)];
self.sideEffectsLabel.attributedText = attrSideEffectsStr;
frameTemp.origin.y += h4+10;
float h5 = [[heightsArray objectAtIndex:4] floatValue];
frameTemp.size.height = h5;
self.drugInteractionLabel.frame = frameTemp;
// Drug Interaction Label
NSString *drugInteractionString = [NSString stringWithFormat:#"Drug Interaction: %#",saltInfoModel.di];
NSMutableAttributedString* attrdrugInteractionStr = [[NSMutableAttributedString alloc] initWithString:drugInteractionString];
NSInteger _strLenDrugInteraction=[drugInteractionString length];
[attrdrugInteractionStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _strLenDrugInteraction)];
[attrdrugInteractionStr addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, 18)];
[attrdrugInteractionStr addAttribute:NSForegroundColorAttributeName value:_lightGray range:NSMakeRange(18, _strLenDrugInteraction-18)];
self.drugInteractionLabel.attributedText = attrdrugInteractionStr;
frameTemp.origin.y += h5+10;
float h6 = [[heightsArray objectAtIndex:5] floatValue];
frameTemp.size.height = h6;
self.moaLabel.frame = frameTemp;
// MechanismOfAction Label
NSString *moaString = [NSString stringWithFormat:#"Mechanism Of Action: %#",saltInfoModel.moa];
NSMutableAttributedString* attrMoaStr = [[NSMutableAttributedString alloc] initWithString:moaString];
NSInteger _strLenMoa=[moaString length];
[attrMoaStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _strLenMoa)];
[attrMoaStr addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, 21)];
[attrMoaStr addAttribute:NSForegroundColorAttributeName value:_lightGray range:NSMakeRange(21, _strLenMoa-21)];
self.moaLabel.attributedText = attrMoaStr;
NSLog(#"height of cell : %f", frameTemp.origin.y + self.moaLabel.frame.size.height);
}
Still i have large spaces in between labels , here is screenshot:
I have tried every thing .. and struggling with this issue for many hours (a small issue i think :( )
I do not see where you are setting the height of the cell. You should not call some obscure function in heightForRowAtIndexPath but return the height of the cell.
Ask your datasource for the necessary strings and then calculate the height the usual way, i.e. using boundingRectWithSize:options:attributes:context:. The source of your problem is the convoluted code.
I have a multiple lines UILabel with attributed text.
All the lines in the text are of the same font, but each line is of a different font size.
I'm trying to achieve the exact same vertical space between each line.
However what is being displayed has variable spaces. It is as if something is adding a vertical margin to the font based on the font size.
CGFloat y = 0;
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:#""];
NSArray *linesArray = [NSArray arrayWithObjects:#"One I\n",
#"Two I\n",
#"Three I\n",
#"Four I\n",
#"Five I\n", nil];
CGFloat fontSize = 10.0;
for(NSString *line in linesArray) {
NSMutableAttributedString *attributedLine = [[NSMutableAttributedString alloc] initWithString:line];
NSInteger stringLength=[line length];
[attributedLine addAttribute:NSFontAttributeName
value:[UIFont fontWithName:#"TimesNewRomanPSMT" size:fontSize]
range:NSMakeRange(0, stringLength)];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 0.0f;
paragraphStyle.alignment = NSTextAlignmentRight;
[attributedLine addAttributes:#{ NSParagraphStyleAttributeName : paragraphStyle} range:NSMakeRange(0, stringLength)];
[attString appendAttributedString:attributedLine];
fontSize += 10.0;
}
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.numberOfLines = 0;
label.attributedText = attString;
[label sizeToFit];
CGRect newFrame = label.frame;
newFrame.size.width = self.view.frame.size.width - 40;
newFrame.origin.y = y;
newFrame.origin.x = 0;
label.frame = newFrame;
[self.view addSubview:label];
Any suggestions on the code I should use in order for it to display no space at all between each line of text?
I have been doing something similar, so maybe you could try something like this (typed in browser, watch out!):
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setAlignment: NSTextAlignmentRight];
[style setLineSpacing:0];
for(NSString *line in linesArray) {
NSMutableParagraphStyle *subStyle = [style mutableCopy];
[subStyle setMaximumLineHeight:10]; // play around with this value <-----
NSDictionary *attributes =
#{
NSFontAttributeName : [UIFont fontWithName:#"TimesNewRomanPSMT" size:fontSize],
NSParagraphStyleAttributeName : paragraphStyle,
};
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:line attributes: attributes]];
fontSize += 10.0;
}