UITextView text is aligned to the bottom - ios

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];
}

Related

ios - how to have Multiple lines of NSAttributedString

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.

sizeToFit clips the particular character for custom font

I'm not sure why? but using custom font with NSAttributedString when I sizeToFit the text it'll clip the particular character in string.
Here's the code sample:
NSString *appTitle = #"Hemang";
NSString *appQuote = #" My name last char is g";
NSString *mergedString = [NSString stringWithFormat:#"%#\n%#",appTitle,appQuote];
UILabel *label1 = [[UILabel alloc] init];
label1.frame = CGRectMake(0, 0, 200.f, 80.f);
NSMutableAttributedString *firstLabelAttributes = [[NSMutableAttributedString alloc] initWithString:mergedString];
NSMutableParagraphStyle *paragrapStyle = NSMutableParagraphStyle.new;
paragrapStyle.alignment = NSTextAlignmentLeft;
[firstLabelAttributes addAttribute:NSParagraphStyleAttributeName value:paragrapStyle range:NSMakeRange(0, [mergedString length])];
[firstLabelAttributes addAttribute:NSFontAttributeName
value:[UIFont fontWithName:#"NexaBold" size:50.f]
range:NSMakeRange(0, [appTitle length])];
[firstLabelAttributes addAttribute:NSFontAttributeName
value:[UIFont fontWithName:#"NexaLight" size:10.f]
range:NSMakeRange([appTitle length]+1, [appQuote length])];
[label1 setNumberOfLines:2];
[label1 setAttributedText:firstLabelAttributes];
[self.view addSubview:label1];
[label1 sizeToFit];
label1.center = self.view.center;
Update :
In my another test with normal
label1.text = #"Hemang"
[label1 sizeToFit];
g clips
Update 2:
Temporary work around is to increase some height after sizeToFit call.
CGRect frame = label1.frame
frame.size.height += 5;
label1.frame = frame;
It solved my problem but I really don't want this peace of patch.

UITextView can't be selectable with textContainer

I am using UITextView with textContainer property and attributed string...
UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame
textContainer:textContainer];
But this textview became unSelectable.
I tried to subClass UITextView and return YES to canBecomeFirstResponder method but it still unSelectable..
how to fix this problem ?
This is Part of my code
NSString * decodedHtmlString = [[contentString stringByDecodingHTMLEntities] stringWithNewLinesAsBRs];
NSString * plainText = [decodedHtmlString stringByConvertingHTMLToPlainText];
NSData * dataString = [decodedHtmlString dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:NO];
NSDictionary * attributedOptions = [NSDictionary dictionaryWithObjectsAndKeys:
NSHTMLTextDocumentType, NSDocumentTypeDocumentAttribute
, nil];
NSAttributedString * attributesString = [[NSAttributedString alloc] initWithData:dataString
options:attributedOptions
documentAttributes:nil
error:nil];
/////direction
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentRight;
NSMutableAttributedString * mutableAttrString = [[NSMutableAttributedString alloc] initWithAttributedString:attributesString];
[mutableAttrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Arial" size:29.0] range:NSMakeRange(0, plainText.length)];
[mutableAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, plainText.length)];
NSTextStorage * textStorage = [[NSTextStorage alloc] initWithAttributedString:mutableAttrString];
NSLayoutManager * layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];
NSUInteger lastRenderedGlyph = 0;
CGFloat currentXOffset = 0;
while (lastRenderedGlyph < layoutManager.numberOfGlyphs) {
CGRect textViewFrame = CGRectMake(currentXOffset, 0, 500, 700);
CGSize columnSize = textViewFrame.size;
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:columnSize];
[layoutManager addTextContainer:textContainer];
UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame
textContainer:textContainer];
[textView setSelectable:YES];
textView.scrollEnabled = NO;
currentXOffset += CGRectGetWidth(textViewFrame);
[self.scrollView addSubView:textView];
lastRenderedGlyph = NSMaxRange([layoutManager glyphRangeForTextContainer:textContainer]);
}
It seems that UITextView can't be selectable with using textContainer. So, as workaround we can retrieve attributedString from range of textContainer and then set it to the attributedText property of our UITextView.
NSAttributedString * subAttributedString = [mutableAttrString attributedSubstringFromRange:[layoutManager glyphRangeForTextContainer:textContainer]];
[textView setAttributedText: subAttributedString];
then the selection is working.
Try this
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:Size];
// create the UITextView for this column
UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame
textContainer:textContainer];

cant find height for my cell

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.

Equal spacing between lines of NSAttributedString with multiple font sizes for a UILabel in iOS 6

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;
}

Resources