As users search journals, I want to display a portion of the journal and the search term bolded in the detailTextLabel in UITableViewCell.
So like, SEARCH "today"
1/19/2014
Bright and sunnday, today is going to be a ...
How do you bold that word today, but not the rest of it?
cell.detailTextLabel.text = note.content;
detailTextLabel has the method setAttributedText so you can make an attributedText which you can customize dramatically, and then you can set it.
NSMutableAttributedString * attributedText = [[NSMutableAttributedString alloc] initWithString:note.content];
NSRange boldedRange = NSMakeRange(0, 5);// NSRange range = [displayingContent rangeOfString:self.notesSearchBar.text options:NSCaseInsensitiveSearch];
[attributedText addAttribute: NSFontAttributeName value:[UIFont fontWithName:#"Helvetica-Bold" size:16.0] range:boldedRange];
[cell.detailTextLabel setAttributedText: attributedText];
Related
I am setting font to UITextView . It works. I am also making the text entered in the UITextView as bold by selecting the text in UITextView. My problem is if I give
font size I cannot make the text bold.
try it .
self.txtView.text=#“Life is an opportunity , Don’t west your time !”;
NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:self.txtView.text];
NSString *word= #"Life is an opportunity";
NSRange range=[self..txtView.text rangeOfString:word];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
[self.txtView setAttributedText:string];
[self.txtView setFont:[UIFont fontWithName:#"Montserrat-Bold" size:15]];
self.txtView.alpha=1;
I am using UITableview for listing names of person and also implement UISearchbar for searching different names.I want to change textLabel color on UITableViewCell when user searching on specific item,for example when i am typing name “Ant” on searchbar,need to change color of “Ant” on UItableview. Please help me.
You need to look at using NSAttributedString to specify the colour of the text. You will need to process the visible cells on each change in the search criteria and check when configuring cells for display, find the search text in the name string and set the appropriate format attributes for the found range.
You can use below method to fulfil your requirement.
- (NSAttributedString *)highlightText:(NSString *)textToBeHighlighted inString:(NSString *)fullString {
NSDictionary *attributeForFullText = #{
NSForegroundColorAttributeName : [UIColor blackColor],
NSFontAttributeName : [UIFont systemFontOfSize:10.0f]
// And more......
};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:fullString attributes:attributeForFullText];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[fullString rangeOfString:textToBeHighlighted]];
/* you can use the above statement to do single style,
but if you want to do something more like size, font, alignment etc.
then you can do by below */
[attributedString addAttributes:dictionary_with_more_style range:[fullString rangeOfString:textToBeHighlighted]];
return attributedString;
}
And just call it from your cellForRowAtIndexPath
UILabel *titleLabel;
NSString *fullTitle;
NSString *searchString;
[titleLabel setAttributedText:[self highlightText:searchString inString:fullTitle];
You can make use of attributedText property of UILabel. This decorates the text on a label. Create a category for UITableViewCell and write a method and do the following:
#implementation UITableViewCell(asdf)
- (void)setText:(NSString *)text withMatchingText:(NSString *)matchingText;
{
// Set the default text color to the label text.
self.textLabel.textColor = // Default text color
if (matchingText.length > 0) {
// Create the color for the matching text.
UIColor *matchingColor = // Text color for the matching text, eg. "Ant"
//Find the range of the matching text from the whole text.
NSRange firstMatchingRange = [text rangeOfString:matchingText options:NSCaseInsensitiveSearch];
// Create an attributed string with matching color for your matching text on the whote text.
NSMutableAttributedString *mString = [[NSMutableAttributedString alloc] initWithString:text];
[mString addAttribute:NSForegroundColorAttributeName
value:matchingColor
range:firstMatchingRange];
self.textLabel.attributedText = mString;
}else {
self.textLabel.text = text;
}
}
In your viewController's tableView:cellForRowAtIndexPath: method do the following:
NSString *text = // the name property
cell setText:text withMatchingText:self.searchBar.text];
self.searchBar is the property of type UISearchBar you have on your ViewController connected with the search bar on your storyboard/xib.
The above code does like, If your search bar is having text then, It set the matchingColor to the matching text (eg. Ant) and the other letter(ony) will be the default colour. If no more matching text is there, then the whole text will be displayed with the default text colour.
This may help you.
Check this code. It may solve your problem.
Put this code in tableView:cellForRowAtIndexPath: method of your controller.
NSString *title = #"Test this answer";
NSString *searchText = #"ans";
NSMutableAttributedString *originalString = [[NSMutableAttributedString alloc] initWithString:title];
NSRange range = [title rangeOfString:searchText];
if (range.location == NSNotFound) {
NSLog(#"No match found");
}
else {
NSLog(#"Found the range of the substring at (%lu, %lu)", (unsigned long)range.location, range.location + range.length);
[originalString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
}
cell.lbl_Title.attributedText = originalString;
In my project I want to add an attributed text in UILabel placed on the xib.
It's working perfectly, but if large text appears it shows some issues.
My current implementation:
- (void)viewDidLoad
{
[super viewDidLoad];
_demoLabel.numberOfLines = 0;
_demoLabel.lineBreakMode = NSLineBreakByWordWrapping;
_demoLabel.attributedText = [self demoNameWithFontSize:21 andColor:[UIColor redColor]];
}
- (NSMutableAttributedString *)demoNameWithFontSize:(CGFloat)fontSize andColor:(UIColor *)color
{
NSMutableAttributedString *attributedText = nil;
NSString *demoName = #"Blah blah blah";
UIFont *demoFont = [UIFont fontWithName:#"Zapfino" size:fontSize];
attributedText = [[NSMutableAttributedString alloc] initWithString:demoName];
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.lineBreakMode = NSLineBreakByWordWrapping;
[attributedText addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, [demoName length])];
[attributedText addAttribute:NSFontAttributeName value:demoFont range:NSMakeRange(0, [demoName length])];
[attributedText addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, [demoName length])];
return attributedText;
}
Output:
Issue:
It is not displaying the whole text, even if I applied the NSMutableParagraphStyle.
How can I solve this ?
Alternative I found:
If I change
UIFont *demoFont = [UIFont fontWithName:#"Zapfino" size:fontSize];
to
UIFont *demoFont = [UIFont systemFontOfSize:fontSize];
It'll work and gives output like:
But the issue is I need to use custom font, can't use default font. Also cannot change the font size.
I checked UILabel class reference and googled, but couldn't find a solution. Please help me.
Is there anyway to span this text into multiple lines ?
You need to resize the UILabel to fit the text.
You can calculate the size with the boundingRectWithSize:options:context: NSAttributedString class method, which takes an attributed string and calculates the size within a set rect based on all the attributes of the string.
I am having issues with retaining an attributed NSMutableString. I have a UITableView who's each UITableViewCell has an attributed text. Setting the attributed text is no problem, but upon selection, the UITableViewCell's attributes is lost. This is my code in cellForRowAtIndexPath that sets the attribute:
NSMutableAttributedString *changesStyleString_h = [[NSMutableAttributedString alloc] initWithString:#"Attributes change!" attributes:#{NSFontAttributeName: [UIFont boldSystemFontOfSize:20], NSForegroundColorAttributeName:[UIColor yellowColor]}];
[changesStyleString_h addAttributes:#{ NSUnderlineStyleAttributeName:#(1)} range:NSMakeRange(11, 6)];
cell.mainLabel.attributedText = changesStyleString
might i point out that mainLabel is also a UILabel, no customization there. Any help in the right direction would be greatly appreciated!
I found that I needed to set attributes on the ENTIRE string, or it would do funky things.
NSString* string = #"1 - some string"
NSMutableAttributedString* string = [[NSMutableAttributedString alloc] initWithString:string];
[string setAttributes:#{NSForegroundColorAttributeName: accent, NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue-Bold" size:13.f]} range:NSMakeRange(0, 1)];
This would cause weird behavior when highlighting the cell.
However, when I did this:
[string setAttributes:#{NSForegroundColorAttributeName: [UIColor blackColor]} range:NSMakeRange(1, [labelTwo length] - 1)];
Everything seemed to work as expected.
Hope that helps!
I am trying to make an attributed string with a strikethrough. I can set other attributes such as foreground color and font size but when I try to set a strikethrough through part of the text, that part of the text disappears. Any Idea what might be causing it?
Below is the code. Thanks for looking!
// ...
//Price
NSLog(#"RESULT NUMBER %d", cell.result.resultId);
priceString = (priceString == nil) ? #"$150.00\n$100.00" : priceString;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:priceString];
NSRange linebreak = [priceString rangeOfString:#"\n"];
if (linebreak.location != NSNotFound) {
[attributedString beginEditing];
// RegPrice
NSRange firstLine = NSMakeRange(0, linebreak.location);
// [attributedString addAttribute:NSFontAttributeName
// value:[UIFont boldSystemFontOfSize:11]
// range:firstLine];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1]
range:firstLine];
#try {
[attributedString addAttribute:NSStrikethroughStyleAttributeName
value:#(NSUnderlineStyleSingle)
range:firstLine];
} #catch (NSException *e) {
NSLog(#"ATTRIBUTE EXCEPTION: %#", e);
}
// Sale Price
[attributedString addAttribute:NSFontAttributeName
value:[UIFont boldSystemFontOfSize:14]
range:NSMakeRange(linebreak.location + 1, priceString.length - (linebreak.location + 1))];
[attributedString endEditing];
}
cell.lblPrice.attributedText = attributedString;
return cell;
}
I had the same problem of string disappearing until I realised that the value for NSStrikethroughStyleAttributeName should be an NSNumber.
Therefore the code above should look like:
Objective-c
[attributedString addAttribute:NSStrikethroughStyleAttributeName
value: [NSNumber numberWithInt: NSUnderlineStyleSingle]
range:firstLine];
In Swift 4 attributes are passed using dictionary of type [NSAttributedStringKey: Any] and the strike trough style attribute would look like this:
[NSAttributedStringKey.strikethroughStyle: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue)]
For me boldSystemFontOfSize: returns a font object with the HelveticaNeueInterface-MediumP4 font. Are you sure this font has a strike-through? Not all fonts do. Maybe try using a different font, one that you know has a strike-through.
I had the problem with the strikethrough text disappearing in a UILabel in iOS 7.0 only. It works fine in iOS 6.1 and 7.1.
There seems to be a bug in iOS 7.0 that strikethrough text disappears if the label is too high for the text by a certain amount. It works when I set the label's frame to a small enough height or call sizeToFit after setting the attributedText.