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.
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];
I have a UITextView and there are certain words I'm casting with NSString stringWithFormat that I'd like to be bolded.
I have looked around Stack Overflow and tried to follow the the postings but I guess I'm not understanding it.
Here's what I've been playing around with:
NSRange boldedRange = NSMakeRange(0, 4);
NSString *boldFontName = [[UIFont fontWithName:#"Helvetica-Bold" size:100]fontName];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.name];
[attrString beginEditing];
[attrString addAttribute:NSFontAttributeName
value:boldFontName
range:boldedRange];
[attrString endEditing];
self.resultsTextView.attributedText = attrString;
self.resultsTextView.text = [NSString stringWithFormat:#"One day, %# was taking a walk and saw a %# boy. He was %# a %#.", attrString, self.adjective, self.adverb, self.noun];
You can also set it the following way if you want by setting a dictionary as a whole, as attribute
NSString *strTextView = #"This is some demo Text to set BOLD";
NSRange rangeBold = [strTextView rangeOfString:#"BOLD"];
UIFont *fontText = [UIFont boldSystemFontOfSize:10];
NSDictionary *dictBoldText = [NSDictionary dictionaryWithObjectsAndKeys:fontText, NSFontAttributeName, nil];
NSMutableAttributedString *mutAttrTextViewString = [[NSMutableAttributedString alloc] initWithString:strTextView];
[mutAttrTextViewString setAttributes:dictBoldText range:rangeBold];
[textViewTermsPolicy setAttributedText:mutAttrTextViewString];
Use the below code to set Attribute string in TextView.
NSString *infoString =#"I am Kirit Modi from Deesa.";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:infoString];
UIFont *font_regular=[UIFont fontWithName:#"Helvetica" size:20.0f];
UIFont *font_bold=[UIFont fontWithName:#"Helvetica-Bold" size:20.0f];
[attString addAttribute:NSFontAttributeName value:font_regular range:NSMakeRange(0, 4)];
[attString addAttribute:NSFontAttributeName value:font_bold range:NSMakeRange(5, 15)];
[attString addAttribute:NSFontAttributeName value:font_regular range:NSMakeRange(16, infoString.length - 15 - 1)];
[self.txtView setAttributedText:attString];
OutPut :
Check out #CrazyYoghurt improvement on #Bbrame and #BenoitJadinon on this previous SO question 3586871
I've been using it for over a year and it works great. One limitation: I don't think you can bold multiple times the same string if it appears more than once in your original string. But you can probably expend the code to make it do so if you wish.
If you also need to filter some word from the UITextView and make it underline/change color of that particular text only then you can use the below code.
Here, I'm getting all the doc text in the Content string and filter some particular text that is in Hebrew language.
NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc]initWithString:content attributes:nil];
[aStr addAttribute:NSLinkAttributeName value:#"http://www.apple.com" range:[content rangeOfString:#"מדיניות פרטיות"]];
[aStr addAttribute:NSLinkAttributeName value:#"http://www.google.com" range:[content rangeOfString:#"לינק"]];
textview.linkTextAttributes = #{NSUnderlineStyleAttributeName : #(NSUnderlineStyleSingle)};
textview.delegate = (id)self;
//...You can as per your custom color
[aStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[content rangeOfString:#"מדיניות פרטיות"]];
[aStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[content rangeOfString:#"לינק"]];
//Here You can also add the tap gesture on that text.
//Tap gesture
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tappedTextView:)];
[textview addGestureRecognizer:tapRecognizer];
[textview setAttributedText:aStr];
textview.textAlignment=NSTextAlignmentRight;
//For getting the text location in the tap gesture
-(void)tappedTextView:(UITapGestureRecognizer *)tapGesture
{
UITextView *textView = (UITextView *)tapGesture.view;
CGPoint tapLocation = [tapGesture locationInView:textView];
UITextPosition *textPosition = [textView closestPositionToPoint:tapLocation];
NSDictionary *attributes = [textView textStylingAtPosition:textPosition inDirection:UITextStorageDirectionForward];
NSString *urlStr = attributes[NSLinkAttributeName];
if (urlStr)
{
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",url]]];
PrivacyViewController *next = [PrivacyViewController new];
[self.navigationController pushViewController:next animated:YES];
}
}
I'm building an app with minimum version of iOS 7. In the method where i draw my cells, i have this code to draw a UILabel with different UIFonts types.
//Get Strings
NSString* author = [self getUserCreatedVideo:notf];
NSString* caption = [NSString stringWithFormat:#"\"%#\"", [notf objectForKey:#"title"]];
NSString* challenge = #"challenged you to reply to";
NSString* timeToReply = #"24h to reply";
NSString* myString = [NSString stringWithFormat:#"%# %# %# %#", author, challenge, caption, timeToReply];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:myString];
UIColor* greenColor = [UIColor colorWithRed:102.0/255.0 green:204.0/255.0 blue:153.0/255.0 alpha:1];
//Author
[str addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(0,author.length)];
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:#"Helvetica" size:17.0] range:NSMakeRange(0,author.length)];
//Challenge
[str addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(author.length+1,challenge.length)];
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:#"Helvetica" size:13.0] range:NSMakeRange(author.length+1,challenge.length)];
//Caption
[str addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(author.length+1+challenge.length+1,caption.length)];
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:#"Helvetica" size:14.0] range:NSMakeRange(author.length+1+challenge.length+1,caption.length)];
//Time To Reply
[str addAttribute:NSForegroundColorAttributeName value:greenColor range:NSMakeRange(author.length+1+challenge.length+1+caption.length+1,timeToReply.length)];
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:#"Helvetica" size:14.0] range:NSMakeRange(author.length+1+challenge.length+1+caption.length+1,4)];
cell.subtext.attributedText = str;
Now i want to calculate the UILabel text size, so that i can draw a UILabel 20px below cell.subtext. How can i achieve this?
So basically you want to calculate height of text when you know text width.
CGRect rect = [str boundingRectWithSize:CGSizeMake(cellWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
Use resulting rect.size the way you want.
I have read several method about bolding a part of string.
But I still can't get it work.
Here's my code
#define FONT_OPEN_BOLD(s) [UIFont fontWithName:#"OpenSans-Bold" size:s]
In viewDidLoad function
NSString *stringName = #"ShowTimes" ;
UIFont *font = FONT_OPEN_BOLD(15.0f);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:stringName];
[attrString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, 4)];
self.title = stringName;
Any suggestion?
Thank you in advance. ^^
NSString *stringName = #"ShowTimes" ;
UIFont *font = FONT_OPEN_BOLD(15.0f);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:stringName];
[attrString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, 4)];
//Initialize TTAttributedLabel with rect
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 150)];
//Set the attributedText property of TTAttributedLabel
label.attributedText = attrString;
//Set navigationItem.titleView to the label view we've created
self.navigationItem.titleView = label;
What you could do is use an NSAttributedString.
NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSString *yourString = ...;
NSRange boldedRange = NSMakeRange(22, 4);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];
[attrString beginEditing];
[attrString addAttribute:kCTFontAttributeName
value:boldFontName
range:boldedRange];
[attrString endEditing];
//draw attrString here...
Take a look at this handy dandy guide to drawing NSAttributedString objects with Core Text.
Is it possible to use multiple lines in the UIRefreshControl title? Whenever I add \n to the NSAttributedString only the first line will be displayed. I'm trying to set a title and on the next line some more text. So is there a workaround to use two lines of text in the UIRefreshControl?
This is the current code where only "Title Here" is displayed:
self.refreshControl = [[UIRefreshControl alloc] init];
NSString *title = #"Title Here";
NSString *subText = #"Subtext Here";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#\n%#",title,subText]];
[attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica" size:20.0f] range:NSMakeRange(0, [title length])];
[attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica" size:14.0f] range:NSMakeRange([title length],[subText length])];
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, [title length])];
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange([title length], [subText length])];
self.refreshControl.attributedTitle = attString;
here is a tricky method: find the UILabel in the UIRefreshControl and set numberOfLines = 0.
UILabel *titleLabel = [[[[self.refreshControl subviews] firstObject] subviews] lastObject];
if (titleLabel) {
titleLabel.numberOfLines = 0;
NSString title = #"Pull to Refresh.\nUpdated Time: 09:30"; // \n for new line.
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:title];
}
This piece of code will work
NSString *title = #"Title Here";
NSString *subText = #"Subtext Here";
NSMutableAttributedString *titleAttString = [[NSMutableAttributedString alloc] initWithString:title];
NSMutableAttributedString *subTitleAttString = [[NSMutableAttributedString alloc] initWithString:subText];
[titleAttString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica" size:20.0f] range:NSMakeRange(0, [title length])];
[subTitleAttString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica" size:14.0f] range:NSMakeRange(0,[subTitle length])];
[titleAttString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, [title length])];
[subTitleAttString addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, [subTitle length])];
[titleAttString appendAttributedString:subTitleAttString];
self.refreshControl.attributedTitle = titleAttString;
The trick is to change the UILable's property of Refresh Controller to Zero.
The hack is we have to find the label through the subviews and there child as shown below.
Here is a Swift version
if let refreshLabel = refreshControl?.subviews.first?.subviews.last as? UILabel {
refreshLabel.numberOfLines = 0
}
So here is the full code sample
private var marketRefreshController = UIRefreshControl()
private var lastUpdatedDate = Date()
override func viewDidLoad() {
super.viewDidLoad()
tableView.refreshControl = marketRefreshController
if let refreshLabel = refreshControl?.subviews.first?.subviews.last as? UILabel {
refreshLabel.numberOfLines = 0
}
marketRefreshController.addTarget(self, action: #selector(refreshMarketData), for: .valueChanged)
marketRefreshController.tintColor = UIColor.blue
let textAttributes = [NSAttributedString.Key.font: UIFont.appSubTitle,
NSAttributedString.Key.foregroundColor: UIColor.blue]
let timeSinceLastUpdate = lastUpdatedDate.timeAgoSinceNow // Date Sting Converter Helper Method
let displayString = "Fetching Market Data ... \n Last Updated: \(timeSinceLastUpdate)"
marketRefreshController.attributedTitle = NSAttributedString(string: displayString,
attributes: textAttributes)
tableView.addSubview(marketRefreshController)
}
The expected output should be like -