Why does comparing the same NSAttributedString not work?
I have the following:
- (void)someSetupClass {
NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:#"abcdefghijklmnopqrstuvwxyz"];
[aString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
[aString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Light" size:(16.0)] range:NSMakeRange(0, 20)];
[aString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];
_aTextView.attributedText = aString;
_aTextView.delegate = self;
[_scroll addSubview:_best];
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:#"abcdefghijklmnopqrstuvwxyz"];
[aString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
[aString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Light" size:(16.0)] range:NSMakeRange(0, 20)];
[aString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];
if ([textView.attributedText isEqualToAttributedString:aString]) {
// Never reaches here, but why?
textView.text = #"";
// textView.textColor = [UIColor blackColor];
}
[textView becomeFirstResponder];
}
Make that textview strongly typed variable and try. Eg.
#property(nonatomic, strong)UITextView * aTextView;
Check if your textView has default font set to Helvetica/system - size 12. May be because of this last part of your attributed string is taking default font of the textView after you setText. But part of aString (uvwxyz) does not have font assigned and hence it mismatches.
Hope this helps you :)
Related
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 want to make the string as
I used this code but it always show the '%' at the top right.
UIFont *PercentFont = [UIFont fontWithName:#"System" size:30];
NSString* percentString =[NSString stringWithFormat:#"%d%%",value ];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:percentString attributes:#{NSFontAttributeName: PercentFont}];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:240/255.0 green:133/255.0 blue:105/255.0 alpha:1.0] range:NSMakeRange(0, percentString.length -1)];
[attributedString setAttributes:#{NSFontAttributeName : [UIFont fontWithName:#"System" size:17] ,NSBaselineOffsetAttributeName : #17} range:NSMakeRange(percentString.length -1, 1)];
// set % digit color
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:240/255.0 green:133/255.0 blue:105/255.0 alpha:1.0] range:NSMakeRange(percentString.length -1, 1)];
Please help to correct it. Thanks in advance.
Change your code for this:
UIFont *PercentFont = [UIFont systemFontOfSize:30];
NSString* percentString =[NSString stringWithFormat:#"%d%%",9];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:percentString attributes:#{NSFontAttributeName: PercentFont}];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:240/255.0 green:133/255.0 blue:105/255.0 alpha:1.0] range:NSMakeRange(0, percentString.length -1)];
[attributedString setAttributes:#{NSFontAttributeName : [UIFont systemFontOfSize:17] ,NSBaselineOffsetAttributeName : #-2} range:NSMakeRange(percentString.length -1, 1)];
// set % digit color
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:240/255.0 green:133/255.0 blue:105/255.0 alpha:1.0] range:NSMakeRange(percentString.length -1, 1)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(120, 100, 200, 100)];
label.attributedText =attributedString;
It will be good idea change PercentFont by percentFont (lower case).
I'm new to iOS Development..I cannot get my strings to get BOLD.
Thanks in advance!
Here's my code
- (void)viewDidLoad {
[super viewDidLoad];
NSString *sentence = [[NSString alloc] initWithString:[NSString stringWithFormat:#"%#%#%#%#", self.name, self.adjective, self.noun, self.verb]];
NSMutableAttributedString *finalSentence = [[NSMutableAttributedString alloc] initWithString:sentence];
NSRange nameRange = NSMakeRange(0, self.name.length);
NSRange adjectiveRange = NSMakeRange(self.name.length, self.adjective.length);
NSRange nounRange = NSMakeRange((self.adjective.length + self.name.length), self.noun.length);
NSRange verbRange = NSMakeRange((self.adjective.length + self.name.length + self.noun.length), self.verb.length);
[finalSentence beginEditing];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Arial-BoldMT" size:17.0] range:nameRange];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Arial-BoldMT" size:17.0] range:adjectiveRange];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica-Bold" size:12.0] range:nounRange];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica-Bold" size:12.0] range:verbRange];
[finalSentence endEditing];
NSString *completeSentence = [finalSentence string];
self.resultTextView.text = completeSentence;
}
It's not an accident that there's a separate NSAttributedString class. The completeSentence, that you retrieve using your attributed string's string property, is a plain NSString, does not contain formatting info (because it cannot).
You have to assign the attributed string object itself to the attributedText property of your text view.
You are assigning the plain text version of your attributed text ([finalSentence string]) to the plain text property of your UITextView.
You should set the attributed text directly to the attributedText property of the UITextView -
self.resultTextView.attributedText=finalSentence
Just call
self.resultTextView.attributedText = finalSentence;
Here is code that you need
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *sentence = [[NSString alloc] initWithString:[NSString stringWithFormat:#"%#%#%#%#", self.name, self.adjective, self.noun, self.verb]];
NSMutableAttributedString *finalSentence = [[NSMutableAttributedString alloc] initWithString:sentence];
NSRange nameRange = NSMakeRange(0, self.name.length);
NSRange adjectiveRange = NSMakeRange(self.name.length, self.adjective.length);
NSRange nounRange = NSMakeRange((self.adjective.length + self.name.length), self.noun.length);
NSRange verbRange = NSMakeRange((self.adjective.length + self.name.length + self.noun.length), self.verb.length);
[finalSentence beginEditing];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Arial-BoldMT" size:17.0] range:nameRange];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Arial-BoldMT" size:17.0] range:adjectiveRange];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica-Bold" size:12.0] range:nounRange];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica-Bold" size:12.0] range:verbRange];
[finalSentence endEditing];
self.resultTextView.attributedText = finalSentence;
}
I am using the code below to try and set the color of a substring of text in my UITextView, but it is not working. It seems like it should be simple; what am I missing?
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:#"Welcome"];
UIFont *font = [UIFont fontWithName:#"Avenir-Light" size:10];
[title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)];
[title addAttribute:kCTForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, title.length)];
descriptionCommentLabel.attributedText = title;
You should replace kCTForegroundColorAttributeName with NSForegroundColorAttributeName. You can check a list of valid attributes in the constants section of this file: Apple docs: NSAttributedString UIKit additions. Your code would be now:
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:#"Welcome"];
UIFont *font = [UIFont fontWithName:#"Avenir-Light" size:10];
[title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)];
[title addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, title.length)];
The kCT... attributes were used in iOS 5 and older versions, when UIKit didn't support NSAttributedString and you had to mess with CoreText.
Change the below line in your code to:
[title addAttribute:kCTForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, title.length)];
to
[title addAttribute:NSForegroundColorAttributeNamevalue:[UIColor redColor] range:NSMakeRange(0, title.length)];
I need the text of a UILabel to be black, but have a light gray underline under the text. Is this possible with NSAttributedString or TTTAttributedLabel? Or is custom drawing using Core Graphics needed?
CLARIFICATION:
I need a specific color text on a different color underline. Example: blue text on red underline.
You can do with NSAttributedString as below.
NSMutableAttributedString* string = [[NSMutableAttributedString alloc]initWithString:#"you string"];
[string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, string.length)];//TextColor
[string addAttribute:NSUnderlineStyleAttributeName value:underlineNumber range:NSMakeRange(0, string.length)];//Underline color
[string addAttribute:NSUnderlineColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, string.length)];//TextColor
yourlabel. attributedText = string;
Note: You can also underline particular range of string as like in this post. Also note down, it works ios6+ only.
Instead of creating a local NSMutableAttributedString and adding attributes one by one, we can always create multiple attributes in one single line (using NSDictionary symbols - # { } ) to a specific UILabel including the actual text.
Objective C:
[someUILabel setAttributedText:
[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#", [someObject stringProperty]]
attributes:#{NSUnderlineStyleAttributeName:#(NSUnderlineStyleThick),
NSUnderlineColorAttributeName:[[UIColor alloc] initWithRed:0.953f green:0.424f blue:0.416f alpha:1.00f]}]];
In the above example we have set an underline which is also bold - total 2 attributes.
Swift:
self.someUIButton.setAttributedTitle(NSAttributedString(string: "UIButtonStringTitle", attributes: [NSUnderlineStyleAttributeName : 1]), forState: .Normal)
// Print `str` in black, and underline the word STRING in gray.
NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:#"This is my STRING"];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, str.length-7)];
[str addAttribute:NSUnderlineColorAttributeName value:[UIColor grayColor] range:NSMakeRange([str length]-6, 6)];
[str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange([str length]-6, 6)];
_label.attributedText = str; // assuming you have an iVar name `label`
NSAttributedString *title;
title = [[NSAttributedString alloc] initWithString:#"iphone app" for NSAttributedString" attributes:#{ NSFontAttributeName : [UIFont fontWithName:#"Noteworthy-Bold" size:36], NSUnderlineStyleAttributeName : #1 , NSStrokeColorAttributeName : [UIColor blackColor]}];
UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake( (self.view.bounds.size.width - title.size.width) / 2.0f, 40.0f, title.size.width, title.size.height)];
label.attributedText = title;
[self.view addSubview:label];
m-farhan.com farhan will be underlined
//-----------------------------
// Create attributed string
//-----------------------------
NSString *str = #"m-Farhan.com";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
// Add attribute NSUnderlineStyleAttributeName
[attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(2, 4)];
// Set background color for entire range
[attributedString addAttribute:NSBackgroundColorAttributeName
value:[UIColor colorWithRed:0.103 green:0.305 blue:0.492 alpha:1.000]
range:NSMakeRange(0, [attributedString length])];
// Define label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 280, 80)];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:UITextAlignmentLeft];
// Set label text to attributed string
[label setAttributedText:attributedString];
[[self view] addSubview:label];
In swift, use NSAttributedString.Key.underlineColor.