Attribute for Multiple Text - ios

I have this code and it's working (answer from https://stackoverflow.com/a/3586943/1187014), but I want try modify it a little bit :
NSString *text = #"Forgot password?";
if ([_labelForgotPassword respondsToSelector:#selector(setAttributedText:)])
{
// iOS6 and above : Use NSAttributedStrings
const CGFloat fontSize = 13;
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
UIColor *foregroundColor = [UIColor whiteColor];
// Create the attributes
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
boldFont, NSFontAttributeName,
foregroundColor, NSForegroundColorAttributeName, nil];
NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
regularFont, NSFontAttributeName, nil];
const NSRange range = NSMakeRange(16,0);
// Create the attributed string (text + attributes)
NSMutableAttributedString *attributedText =
[[NSMutableAttributedString alloc] initWithString:text
attributes:attrs];
[attributedText setAttributes:subAttrs range:range];
// Set it in our UILabel and we are done!
[_labelForgotPassword setAttributedText:attributedText];
} else {
// iOS5 and below
// Here we have some options too. The first one is to do something
// less fancy and show it just as plain text without attributes.
// The second is to use CoreText and get similar results with a bit
// more of code. Interested people please look down the old answer.
// Now I am just being lazy so :p
[_labelForgotPassword setText:text];
}
What if I have multiple text, let's say :
NSString *text1 = #"Forgot password?"; // relates with UILabel _labelForgotPassword
NSString *text2 = #"I agree with terms and condition"; // relates with UILabel _labelTerms
NSString *text3 = #"Your country is not listed yet?"; // relates with UILabel _labelCountry
First method that came into my mind is by having nested IF, but nested IF will be very ugly when I have lots of text which need to be attributed, right?
So, how to create that code into a method so that I can just supply the string, name of _label, range, etc and return the result to specific UILabel. and it's all triggered under viewDidLoad. Not by button pressed, or something else.
thank you.

What i am understanding is you want the same attributed logic to be applied to all labels. You can create a category on UILabel if you want to use it for only UILabels.
Syntax of the category should be something like this:
+(NSMutableAttributedString *) convertToAttributedText: (NSString *) text withFont: (UIFont *) font
{
// write the above logic here
//return the attributed text;
}
You can pass the text1 / text2 / text3 whatever to this api and you will get the attributed text.
label.attributedtext = [NSString convertToAttributedText: text withFont:font];
You can configure this API parameters based on your need.
Hope this helps.

Related

How to make textLabel on UITableViewCell with multiple colours?

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;

WatchKit, AttributedString formatting not working

I am building my first WatchKit App and am having troubles with NSAttributedString formatting as it does not seem to work as I'd expect ;)
This is my code:
UIFont *textFont = [UIFont fontWithName:#"Menlo" size:30];
UIFont *hlFont = [UIFont fontWithName:#"Menlo-Bold" size:30];
NSMutableAttributedString *information = [[NSMutableAttributedString alloc]initWithString:#"ADDED AN ENTRY OF " attributes:#{NSFontAttributeName : textFont}];
NSString *amountString = [NSString stringWithFormat:#"%.2f",(-1)*handler.amount.floatValue];
NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];
NSAttributedString *amount = [[NSAttributedString alloc]initWithString:amountString attributes:#{NSFontAttributeName : hlFont, NSUnderlineStyleAttributeName : underline }];
NSAttributedString *to = [[NSMutableAttributedString alloc]initWithString:#" TO " attributes:#{NSFontAttributeName : textFont}];
NSString *categoryString = handler.category;
NSAttributedString *category = [[NSAttributedString alloc]initWithString:categoryString attributes:#{NSFontAttributeName : hlFont, NSUnderlineStyleAttributeName : underline }];
[information appendAttributedString:amount];
[information appendAttributedString:to];
[information appendAttributedString:category];
[_informationLabel setAttributedText:information];
and this the result:
Expectation
10.00 and Stuff should be underlined and in boldface.
Is there something fundamentally different to how attributed strings work on the watch than on iOS? What am I missing?
read through this: https://developer.apple.com/library/ios/documentation/General/Conceptual/WatchKitProgrammingGuide/TextandLabels.html
Solved it, the problem were the fonts #"Menlo".
By using
UIFont *textFont = [UIFont systemFontOfSize:20];
UIFont *hlFont = [UIFont systemFontOfSize:20];
the formatting with underlines works fine.
I don't believe you can programmatically set a label to be bold, italic, underlined....this has to be done through the actual interface in the storyboard.
According to this link
The attributes you can configure are
text
text color
font
min scale
alignment
lines
a potential workaround is to incorporate multiple labels, and set the ones you need to the right format (bold, underlined)

Keep the colors of the textView text after appending NSSting

I'm using the next method to change the colors of few words in textView:
+(void)changeColorToTextObject : (UITextView *)textView ToColor : (UIColor *)color FromLocation : (int)location length : (int)length
{
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString: textView.attributedText];
[text addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(location, length)];
[textView setAttributedText: text];
}
and it looks like this:
but when i'm adding new value to this text the color are gone.
myTextView.text = [myTextView.text stringByAppendingString:newStr];
And it looks like this:
How can I keep the colors like before with the new string?
Instead of myTextView.text = [myTextView.text stringByAppendingString:newStr]; use:
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString:myTextView.attributedText];
NSAttributedString *newAttrString = [[NSAttributedString alloc] initWithString:newStr];
[text appendAttributedString:newAttrString];
myTextView.attributedText = text;
Your code does not work because you are assigning to a new string to the text property of myTextView. text is just just a NSString property and thus is not able to display colors or other things that can be displayed using an NSAttributedString.
Note that writing myTextView.attributedText = text; calls the setter of the property attributedText and thus is 100% equivalent to [myTextView setAttributedText:text];, just a different notation.

Apply rich text format on selected text of UITextView in iOS

I am creating an app in which i have to implement functionality like this:
1) Write into textview
2) Select text from textview
3) Allow user to apply bold,italic and underline functionality on selected text.
I have started implementing it using NSMutableAttributedString. It's working for bold and italic but replaces the textview text with only selected text.
-(void) textViewDidChangeSelection:(UITextView *)textView
{
rangeTxt = textView.selectedRange;
selectedTxt = [textView textInRange:textView.selectedTextRange];
NSLog(#"selectedText: %#", selectedTxt);
}
-(IBAction)btnBold:(id)sender
{
UIFont *boldFont = [UIFont boldSystemFontOfSize:self.txtNote.font.pointSize];
NSDictionary *boldAttr = [NSDictionary dictionaryWithObject:boldFont forKey:NSFontAttributeName];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]initWithString:selectedTxt attributes:boldAttr];
txtNote.attributedText = attributedText;
}
Can anybody please help me out to implement this functionality?
Thanks in advance.
You should not use didChangeSelection for this purpose. Use shouldChangeTextInRange instead.
This is because when you set the attributed string to new one you don't replace the text of certain location. You replace full text with your new text. You need range to locate the position where you want the text changed.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
NSMutableAttributedString *textViewText = [[NSMutableAttributedString alloc]initWithAttributedString:textView.attributedText];
NSRange selectedTextRange = [textView selectedRange];
NSString *selectedString = [textView textInRange:textView.selectedTextRange];
//lets say you always want to make selected text bold
UIFont *boldFont = [UIFont boldSystemFontOfSize:self.txtNote.font.pointSize];
NSDictionary *boldAttr = [NSDictionary dictionaryWithObject:boldFont forKey:NSFontAttributeName];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]initWithString:selectedString attributes:boldAttr];
// txtNote.attributedText = attributedText; //don't do this
[textViewText replaceCharactersInRange:range withAttributedString:attributedText]; // do this
textView.attributedText = textViewText;
return false;
}

Formatting UITableView Cells in iphone

I am developing an iphone app. In the image below I want the first name to be in plain font and the last name to be bold.. How can I do that? Please suggest me.. Please check this image:
Another questio..Now I think the reverse way but the problem here is the first line and second line you see are part of the same string. I want the first line to be bold and the second line to be in plain font. I am storing this in a dictionary. So my dictionary has a key and the value is a string of names and departments. I am unable to set the font. I tried to create two labels and tried to split the string according to the index and assign it to the labels I created. But, in this case the index keeps on changing as there might be a first name for a contact or there might not be any name.
In this case Prinicipal should be in plain font and name should be in bold
Please see the below image:
Starting OS 6.0 there is a property on the UILabel called attributedText which has NSAttributedString type (Available in iOS 3.2 and later.).
Here it is how I am using it:
NSDictionary *firstNameAttributes = #{ NSFontAttributeName : [UIFont fontWithName:#"Helvetica" size:18.0],
NSStrokeColorAttributeName : [UIColor blackColor]};
NSDictionary *lastNameAttributes = #{NSFontAttributeName : [UIFont fontWithName:#"Helvetica-Bold" size:20.0],
NSStrokeColorAttributeName : [UIColor blackColor]};
NSString* first = ... first name ..;
NSString* last = [NSString stringWithFormat:#" %#",... last name ....];
NSMutableAttributedString * name =
[[NSMutableAttributedString alloc] initWithString:first attributes:firstNameAttributes];
NSMutableAttributedString * lastName =
[[NSMutableAttributedString alloc] initWithString:last attributes:lastNameAttributes];
[name appendAttributedString:lastName];
[[cell textLabel] setAttributedText:name];
See also Introduction to Attributed String Programming Guide.
Since the label that you're showing the name string in defines the formatting and style, if you want to have different styles you need to have a different uilabel for each each style you want. Specifically, you will need a uilabel for the firstname: firstNameLabel.font = [UIFont systemFontOfSize:12]; and one for the lastname: lastNameLabel.font = [UIFont boldSystemFontOfSize:12];.
First place the first name string is the firstNameLabel then call [firstNameLabel sizeToFit] to fit the label text within it. Then use the frame of the firstNameLabel to place the lastNameLabel directly after it.
UILabel * firstNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,10,100,25)];
firstNameLabel.tag = firstNameLabelTag //This should be a constant probably
firstNameLabel.font = [UIFont systemFontOfSize:12];
firstNameLabel.text = theStringRepresentingTheFirstName;
[firstNameLabel sizeToFit];
UILabel * lastNameLabel = [[UILabel alloc] initWithFrame:
CGRectMake(10+firstNameLabel.frame.size.width+2, 10, 100, 25)];
lastNameLabel.tag = lastNameLabelTag //This should be a constant probably
lastNameLabel.font = [UIFont boldSystemFontOfSize:12];
lastNameLabel.text = theLastNameString;.
[cell.contentView addSubview:firstNameLabel];
[cell.contentView addSubview:lastNameLabel];
And as for splitting the name string, you're probably pretty limited there. I would split on the first space and assume the first string is the last name (as in your first picture).
The principle case is similar, you need a label for each style that you want to present.
- (IBAction)buttonPressed:(UIButton *)sender {
NSString *title = [sender titleForState:UIControlStateNormal];
NSString *plainText = [NSString stringWithFormat:#"%# button pressed.", title];
NSMutableAttributedString *styledText = [[NSMutableAttributedString alloc] initWithString:plainText];
NSDictionary *attributes = #{ NSFontAttributeName : [UIFont boldSystemFontOfSize:self.statusLabel.font.pointSize]};
NSRange nameRange = [plainText rangeOfString:title];
[styledText setAttributes:attributes range:nameRange];
self.statusLabel.attributedText = styledText;
}
You have to create a custom UITableViewCell. http://icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/

Resources