I have a UITextView and I want to replace some strings with specific images using NSTextAttachment. I'm able to insert an image attachment to the UITextView and I have a method that, from the text view's attributed text, returns a NSString replacing attachments with specific NSString:
-(NSString*)getStringFromAttributedString{
__block NSString *str = self.attributedText.string; // Trivial String representation
__block NSMutableString *string = [NSMutableString new]; // To store customized text
[self.attributedText enumerateAttributesInRange:NSMakeRange(0, self.attributedText.length) options:0 usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop){
// enumerate through the attributes
NSString *v;
NSObject *x = [attributes valueForKey:#"NSAttachment"];
if(x){ // YES= This is an attachment
v = [Fuctions getObjectTag:x];
if(v == nil){
v=#"";
}
}else{
v = [str substringWithRange:range]; // NO=This is a text block.
}
// append value to string
[string appendString:v];
}];
return string;
}
Now I need a method that returns a NSAttributedString from NSString, replacing some occurrences of string with image attachment, something like this but I'm not able to make it work.
-(NSMutableAttributedString*)getAttributedStringFromString:(NSString*)string{
// create NSMutableAttributedString from NSString in input
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
// create the attachment with image
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:#"myImageName.png"];
textAttachment.bounds = (CGRect) {0, -2, textAttachment.image.size};
// create NSMutableAttributedString with image attachment
NSMutableAttributedString *attrStringWithImage = [[NSAttributedString attributedStringWithAttachment:textAttachment] mutableCopy];
// here I'd like to replace all occurrences of a string (":D" this string for example) with my NSMutableAttributedString with image attachment).
return string;
}
How can I do?
Thanks, hope i explained myself.
This is my working solution:
-(NSMutableAttributedString*)getEmoticonStringFromString:(NSString*)string{
NSMutableAttributedString *final = [NSMutableAttributedString new]; //To store customized text
NSInteger len = [string length];
unichar buffer[len];
[string getCharacters:buffer range:NSMakeRange(0, len)];
for(int i = 1; i < len; i++) {
if(buffer[i-1] == ':' && buffer[i] == 'D'){
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [Functions scaleImage:[UIImage imageNamed:#"myImageName"] toSize:CGSizeMake(18.0f, 18.0f)];
textAttachment.bounds = (CGRect) {0, -2, textAttachment.image.size};
NSMutableAttributedString *attrStringWithImage = [[NSAttributedString attributedStringWithAttachment:textAttachment] mutableCopy];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init] ;
[paragraphStyle setAlignment:NSTextAlignmentLeft];
[paragraphStyle setLineSpacing:2.0f];
[attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attrString length])];
[final insertAttributedString:attrStringWithImage atIndex:final.length];
i++;
}else{
[final insertAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"%c", buffer[i-1]]] atIndex:final.length];
if(i == len-1){
[final insertAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"%c", buffer[i]]] atIndex:final.length];
}
}
}
return final;
}
Related
Im using the following code to bold a part of string. I want only the Shipment Ref. #: to be bolded. Following is my code
UIFont *boldFont = [UIFont boldSystemFontOfSize:12];
NSString *yourString = [NSString stringWithFormat:#"Shipment Ref. #: %# ",[[[NSUserDefaults alloc] init] valueForKey:#"shipRef"]];
NSRange boldedRange = NSMakeRange(10, 4);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];
[attrString beginEditing];
[attrString addAttribute:NSFontAttributeName
value:boldFont
range:boldedRange];
[attrString endEditing];
self.shipRefHeader.text = [attrString mutableString];
The problem is that it is not getting bolded
try this code
NSString * yourString = [NSString stringWithFormat:#"Shipment Ref. #: %# ",[[[NSUserDefaults alloc] init] valueForKey:#"shipRef"]];;
NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:yourString];
NSString *boldString = #"Shipment Ref. #:";
NSRange boldRange = [yourString rangeOfString:boldString];
[attrString addAttribute: NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:boldRange];
[self.shipRefHeader setAttributedText: attrString];
In my application I am displaying a text in my label like this
cell.lblUserDetails.text=[NSString stringWithFormat:#"%# and %# other likes your post", [dictionary valueForKey:#"username"], [dictionary valueForKey:#"CuriousCount"]];
Here I want to highlight Username and CuriousCount (in red color) and also username and CuriousCount should be clickable.
UIColor *color = [UIColor redColor];
NSDictionary *attrs = #{ NSForegroundColorAttributeName : color };
NSAttributedString *nameStr = [[NSAttributedString alloc] initWithString:[dictionary valueForKey:#"username"] attributes:attrs];
NSAttributedString *countStr = [[NSAttributedString alloc] initWithString:[dictionary valueForKey:#"CuriousCount"] attributes:attrs];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
[string appendAttributedString:nameStr];
[string appendAttributedString:[[NSAttributedString alloc] initWithString:#" and "]];
[string appendAttributedString:countStr];
[string appendAttributedString:[[NSAttributedString alloc] initWithString:#" other likes your post"]];
cell.lblUserDetails.attributedText = string;
Particular characters are to be highlighted in red color on the label so I wrote below function which works well, but I want to confirm, is there any other efficient way of doing this ? e.g.
-(NSMutableAttributedString*)getAttributeText:(NSString*)string forSubstring:(NSString*)searchstring {
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:_lblName.text];
NSRange searchRange = NSMakeRange(0,string.length);
for (NSInteger charIdx=0; charIdx<searchstring.length; charIdx++){
NSString *substring = [searchstring substringWithRange:NSMakeRange(charIdx, 1)];
NSRange foundRange;
searchRange.location = 0;
while (searchRange.location < string.length) {
searchRange.length = string.length-searchRange.location;
foundRange = [string rangeOfString:substring options:1 range:searchRange];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range:foundRange];
if (foundRange.location != NSNotFound) {
searchRange.location = foundRange.location+foundRange.length;
} else {
// no more substring to find
break;
}
}
}
return text;
}
Below is the code how I use it, and result as well
NSString *string = #"James Bond Always Rocks";
_lblName.text = string;
_lblAttributedName.attributedText = [self getAttributeText:string forSubstring:#"ao"];
Update
NSString *string = #"James Bond Always Rocks";
NSRange range = [string rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:#"J"] options:NSCaseInsensitiveSearch];
NSLog(#"range->%#",NSStringFromRange(range)); //This prints range->{0, 1}
NSString *string = #"James Bond Always Rocks";
NSRange range = [string rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:#"j"] options:NSCaseInsensitiveSearch];
NSLog(#"range->%#",NSStringFromRange(range)); //This prints range->{2147483647, 0}
You can simplify it by searching for a pattern ("[ao]+" in your example) to eliminate
the outer loop:
NSString *string = #"James Bond Always Rocks";
NSString *searchstring = #"ao";
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:string];
// A regular expression pattern that matches a sequence of the characters in "searchString":
NSString *pattern = [NSString stringWithFormat:#"[%#]+", [NSRegularExpression escapedPatternForString:searchstring]];
NSRange foundRange = [string rangeOfString:pattern options:NSRegularExpressionSearch|NSCaseInsensitiveSearch];
while (foundRange.location != NSNotFound) {
[text addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range:foundRange];
NSRange nextRange = NSMakeRange(foundRange.location + foundRange.length, string.length - foundRange.location - foundRange.length);
foundRange = [string rangeOfString:pattern options:NSRegularExpressionSearch|NSCaseInsensitiveSearch range:nextRange];
}
Here is my version, this is very basic approach using simple loops.
Why I posted it because I tracked the efficiency and please see the time taken by each of the implementations.
2014-03-14 15:48:42.792 TimeEfficiency[1166:303] My: 0.000073 seconds
2014-03-14 15:48:45.319 TimeEfficiency[1166:303] martin: 0.000278 seconds
2014-03-14 15:48:48.263 TimeEfficiency[1166:303] avt: 0.000029 seconds
2014-03-14 15:48:51.152 TimeEfficiency[1166:303] janak: 0.000092 seconds
Hence Avt's is best in time-performance.
NSString *string = #"James Bond Always Rocks";
NSString *searchstring = #"ao";
NSMutableArray *characters = [NSMutableArray new];
for (NSInteger i=0; i<searchstring.length; i++) {
[characters addObject:[searchstring substringWithRange:NSMakeRange(i, 1)]]; //ao
}
//store all the location of each of the char
NSMutableArray *locations = [NSMutableArray new];
for (NSInteger i=0; i<string.length; i++) {
if ([characters containsObject: [string substringWithRange:NSMakeRange(i, 1)]] ){
[locations addObject:#(i)];
}
}
//loop for string and for each location change the color
NSMutableAttributedString *text = [[NSMutableAttributedString alloc]initWithString:string];
for (NSInteger i=0; i<locations.count; i++) {
NSRange range=NSMakeRange([locations[i] intValue], 1);
[text addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
}
My variant with NSCharacterSet
- (NSMutableAttributedString*)getAttributeText:(NSString*)string forSubstring:(NSString*)searchstring {
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:string];
NSRange searchRange = NSMakeRange(0,string.length);
NSString *allCaseString = [[searchstring uppercaseString] stringByAppendingString:[searchstring lowercaseString]];
NSCharacterSet *chSet = [NSCharacterSet characterSetWithCharactersInString:allCaseString];
NSRange foundRange;
while (foundRange = [string rangeOfCharacterFromSet:chSet options:NSCaseInsensitiveSearch range:searchRange],
foundRange.location != NSNotFound) {
[text addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range:foundRange];
NSUInteger newStart = foundRange.location + foundRange.length;
searchRange = (NSRange){newStart, string.length - newStart};
}
return text;
}
The problem is to create two text columns in PDF like this:
For generating PDF I use Apple's guide "Drawing and Printing Guide for iOS", two columns I create with tabs:
NSArray *stringList =
#[
#[string1, string2],
#[#"", string4],
#[string5, string6],
#[string7, string8]
];
resultString = [[NSMutableAttributedString alloc] init];
for (NSArray *row in stringList) {
int i = 0;
NSMutableAttributedString *subResult = [[NSMutableAttributedString alloc] init];
NSMutableArray *tabs = #[].mutableCopy;
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
for (NSString *tempString in row) {
NSTextTab *tab = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentLeft location:200 options:nil];
if ([tempString isKindOfClass:[NSAttributedString class]]) {
[subResult appendAttributedString:(NSAttributedString *)tempString];
[subResult appendAttributedString:[[NSAttributedString alloc] initWithString:#"\t"]];
} else {
[subResult appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#\t", tempString]]];
}
[tabs addObject:tab];
i++;
}
[subResult appendAttributedString:[[NSAttributedString alloc] initWithString:#"\n"]];
[style setTabStops:tabs];
[subResult addAttribute:NSParagraphStyleAttributeName
value:style
range:NSMakeRange(0, subResult.length)];
[resultString appendAttributedString:subResult];
}
As output I get this:
So, the lines of my string marked with red arrows I want see entirely in the second column, like in the picture №1.
The idea is to split the text into blocks and single lines and to use for each part
- (void)drawInRect:(CGRect)rect;
or
void CTLineDraw(CTLineRef line, CGContextRef context);
Solution:
#import CoreText;
<...>
- (IBAction)createPDF:(id)sender {
PDFFileName = <yourString>;
PDFPath = <yourPath>;
[self generatePdfWithFilePath:PDFPath];
}
- (void)generatePdfWithFilePath:(NSString *)thefilePath {
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
BOOL done = NO;
do {
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 712), nil);
[self drawText];
done = YES;
}
while (!done);
UIGraphicsEndPDFContext();
}
- (void)drawText {
CGContextRef currentContext = UIGraphicsGetCurrentContext();
//Here you can set up attributes for your text
NSDictionary *attributesForText = #{
NSFontAttributeName:[UIFont fontWithName:#"Georgia" size:12]
};
//Drawing text in line
NSMutableAttributedString *stringOneAttributed = [[NSMutableAttributedString alloc] initWithString:stringOne attributes:attributesForText];
CTLineRef stringOneLine = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)(stringOneAttributed));
CGContextSetTextPosition(currentContext, x, y);
CTLineDraw(stringOneLine, currentContext);
CFRelease(stringOneLine);
//Drawing block of text in rectangle
NSMutableAttributedString *stringTwoAttributed = [[NSMutableAttributedString alloc] initWithString:stringTwo attributes:attributesForText];
[stringTwoAttributed drawInRect:CGRectMake(x, y, width, height)];
}
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.