NSAttributedString Strikethrough makes my attributed text disappear - ios

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.

Related

UITextview font styling issue

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;

Segmented Control set attributed title in each segment

Actually i have a segmented control with 4 segment. I need to add attributed text in each segment like "Notification (2)". Here (2) will be in different color and Notification will be in different color.
I have search some third party library , But it doesn't work for me
Thanks & Regards
There is limitation to use attributed text as we use for label or button. But you may try below method to achieve your requirements.
-(void)SetSegmentValue:(NSString *)value forSegment:(int)index RangeOfBlueColor:(NSRange)bluecolorRange{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:value];
NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle defaultParagraphStyle]mutableCopy];
[paragrahStyle setAlignment:NSTextAlignmentCenter];
[paragrahStyle setLineBreakMode:NSLineBreakByWordWrapping];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, value.length)];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor blackColor]
range:NSMakeRange(0, value.length)];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor blueColor]
range:bluecolorRange];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13.0] range:NSMakeRange(0, value.length)];
int i =0 ;
for (UIView *v in [[[segment subviews] objectAtIndex:0] subviews]) {
if ([v isKindOfClass:[UILabel class]]&& i== index) {
UILabel *label=(UILabel *)v ;
[label setAttributedText:attributedString];
i++;
}
}
}
NOTE : You have to modified some part of code as it's just suggestion to solve your problem

Setting attributedText of UILabel causing issue with Lengthier content

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.

NSAttributedString change color at end of string

This must be an easy thing to do, but I cannot figure it out.
I have a NSMutableAttributedString that has, for example, "This is a test" in it. I want to color the word "test" blue, which I do with this:
NSMutableAttributedString *coloredText = [[NSMutableAttributedString alloc] initWithString:#"This is a test"];
[coloredText addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(10,4)];
That works just fine. But now I want to set the text color back to black for anything typed after "test".
If I do:
[coloredText addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(coloredText.string.length, 1)];
I get an objectAtIndex:effectiveRange: out of bounds error. Assumedly because the range extends beyond the length of the string.
If I do:
[coloredText addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(coloredText.string.length, 0)];
The error goes away but typing after the word "test" remains blue.
How do I set the current color at the insertion point when it is at the end of the string??
Cheers for any input.
In case someone else stumbles upon this, I wanted to post the code I used to solve the problem. I ended up using Kamil's suggestion and adding:
NSAttributedString *selectedString = [textView.attributedText attributedSubstringFromRange:NSMakeRange(textView.attributedText.string.length - 1, 1)];
__block BOOL isBlue = NO;
[selectedString enumerateAttributesInRange:NSMakeRange(0, [selectedString length]) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop) {
isBlue = [[attributes objectForKey:NSForegroundColorAttributeName] isEqual:[UIColor blueColor]];
}];
if (isBlue) {
NSMutableAttributedString *coloredText = [[NSMutableAttributedString alloc] initWithAttributedString:textView.attributedText];
[coloredText addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(textView.attributedText.string.length - 1, 1)];
textView.attributedText = coloredText;
}
to the text changed handler.
You need to recalculate the attributes if the text changes, because their effective range doesn't automatically change with the length of text.

Different colors in UITextView

I try to add string with different colors in my UITextView. I wrote this code
NSMutableAttributedString* attString =
[[NSMutableAttributedString alloc]initWithString:view.text]; //view is my UITextView
[attString addAttribute:(NSString*)kCTForegroundColorAttributeName
value:[UIColor greenColor]
range:(NSRange){attString.length-8, 8}];
view.attributedText = attString;
Maybe it is incorrect attribute, can you tell me what attribute change color for text?
Try using NSForegroundColorAttributeName instead of kCTForegroundColorAttributeName.
NSAttributedString attribute keys

Resources