Underline in attributed text not working in iPhone 6+ - ios

I want to create a method that returns me attributed text.
here is the code that i am using
- (NSAttributedString *)getUnderlineAttributedStringForText:(NSString *)strWholeString andTextToHaveLink:(NSString *)strLink TextColor:(UIColor *)textColor LinkColor:(UIColor *)linkColor withFont:(UIFont*)font {
NSRange stringRange = NSMakeRange(0, strWholeString.length);
NSRange linkRange = [strWholeString rangeOfString:strLink];
NSLog(#"String Link :: %#",[strWholeString substringWithRange:linkRange]);
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:strWholeString];
// Paragraph Style
NSMutableParagraphStyle *paragrapStyle = NSMutableParagraphStyle.new;
paragrapStyle.alignment = NSTextAlignmentLeft;
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragrapStyle range:stringRange];
[attributedString addAttribute:NSForegroundColorAttributeName value:textColor range:stringRange];
[attributedString addAttribute:NSFontAttributeName value:font range:stringRange];
[attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger: NSUnderlineStyleSingle] range:linkRange];
[attributedString addAttribute:NSForegroundColorAttributeName value:linkColor range:linkRange];
return attributedString;
}
// This is how I call the method
[btnTermOfUse setAttributedTitle:[self getUnderlineAttributedStringForText:#"By signing up you agree to the Terms of Use" andTextToHaveLink:#"Terms of Use" TextColor:[UIColor darkGrayColor] LinkColor:[UIColor orangeColor] withFont:[UIFont systemFontOfSize:16]] forState:UIControlStateNormal];
This code works fine in all device except 6+.
Edits
- in 6+ just underline is not showing text colour change for the link text.
I can see the underline in iPhone 6 or 5S.
Any one have any idea regarding this?
I know there are already so many questions related to this but they didn't help me so I am writing here.

Found a hack !! :(
you should write this line before setting up the text,
[btnTermOfUse titleLabel].numberOfLines = 0;
This will show you the line on device too.

Related

Set NSStrikethroughStyleAttributeName for UILabel not work in iOS 10.3 [duplicate]

I have used this line of code before release of iOS 10.3 ,and worked fine.
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#\n%#",strMRP,strOffer]];
[attributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:NSMakeRange(0, strMRP.length)];
[attributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:15] range:NSMakeRange(strMRP.length, strOffer.length+1)];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:[NSNumber numberWithInteger: NSUnderlineStyleDouble]
range:NSMakeRange(0,strMRP.length)];
But now it is stopped working ,is there any alternate way to do the strike out ?
it is the bug in iOS 10.3 , NSStrikethroughStyleAttributeName (any NSUnderlineStyle cases) is not working any more on iOS SDK 10.3.
if anyone found the updated answer related to this , please inform here, I will update my answer.
Product Version: 10.3
Created: 14-Mar-2017
Originated: 14-Mar-2017
Open Radar Link: http://www.openradar.appspot.com/31034683
Radar status is Currently Open state
you can see the alternate sample also here may be it useful.
I found one workaround on developer forum, which works for me. Adding of NSBaselineOffsetAttributeName to string attributes fixed this problem :)
It work fine with
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#\n%#",strMRP,strOffer]];
[attributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:NSMakeRange(0, strMRP.length)];
[attributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:15] range:NSMakeRange(strMRP.length, strOffer.length+1)];
[attributeString addAttribute:NSBaselineOffsetAttributeName
value:[NSNumber numberWithInteger: NSUnderlineStyleNone]
range:NSMakeRange(0,strMRP.length)];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:[NSNumber numberWithInteger: NSUnderlineStyleDouble]
range:NSMakeRange(0,strMRP.length)];
iOS 10.3 onward you need to add NSBaselineOffsetAttributeName.
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#\n%#",strMRP,strOffer]];
[attributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:NSMakeRange(0, strMRP.length)];
[attributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:15] range:NSMakeRange(strMRP.length, strOffer.length+1)];
[attributeString addAttribute:NSBaselineOffsetAttributeName
value:[NSNumber numberWithInteger: NSUnderlineStyleNone]
range:NSMakeRange(0,strMRP.length)];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:[NSNumber numberWithInteger: NSUnderlineStyleDouble]
range:NSMakeRange(0,strMRP.length)];
Once you add NSBaselineOffsetAttributeName then it works for single line, double line ect.
As mentioned above, this is an iOS 10.3 bug.
We needed an immediate workaround, so just in case anyone is looking for hints:
Our Label had attributes set through both NSMutableAttributedString as well as NSMutableParagraphStyle. The bug did not occur when using no / an "empty" paragraph style (instance without any properties set).
So in this scenario, omitting the paragraph style and working around the then missing paragraph properties resolved the issue for us.
Just Use this :-
NSMutableAttributedString *costPrice = [[NSMutableAttributedString
alloc] initWithString:[NSString stringWithFormat:#"₹ %#",strDetails]];
[costPrice addAttribute:NSBaselineOffsetAttributeName
value:[NSNumber numberWithInteger: NSUnderlineStyleSingle] range:NSMakeRange(0,costPrice.length)];
This is temp solution . Hope it works
***You can pass it to function & Enjoy !!!
func customString(currentprice:String,oldPrice:String) -> NSMutableAttributedString{
// 1
let NewString = currentprice + " " + oldPrice
let string = NewString as NSString
let attributedString = NSMutableAttributedString(string: string as String)
// 2
let firstAttributes = [NSForegroundColorAttributeName: UIColor(red: 238/255, green: 140/255, blue: 84/255, alpha: 1),NSBaselineOffsetAttributeName:1]
let secondAttributes = [NSForegroundColorAttributeName: UIColor.lightGrayColor(), NSStrikethroughStyleAttributeName: 1]
// 3
attributedString.addAttributes(firstAttributes, range: string.rangeOfString(currentprice))
attributedString.addAttributes(secondAttributes, range: string.rangeOfString(oldPrice))
return attributedString
}
and use like:
YourUILabel.attributedText = customString("300", oldPrice: "400")

How to set background color to UILabel text only

I'm working the App where i need to set the label like attached image. Please let me know if anyone have any idea?
You can do like this
NSString *yourString1=#"What Does your friends really";
NSString *yourString2=#"Think of your spouce?";
NSString *str1=[NSString stringWithFormat:#" %#..\n",yourString1];
NSString *str2=[NSString stringWithFormat:#" %#,,",yourString2];
NSString *str3=[NSString stringWithFormat:#"%#%#",str1,str2];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[str1 stringByAppendingString:str2]];
NSRange range = [str1 rangeOfString:#".."];
NSRange range1 = [str3 rangeOfString:#",,"];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:range];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:range1];
[attributedString addAttribute: NSBackgroundColorAttributeName value: [UIColor orangeColor] range: NSMakeRange(0, str1.length)];
[attributedString addAttribute: NSBackgroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(str1.length, str2.length)];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:5];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [str2 length])];
lblTest.attributedText = attributedString;
Output of this Code:
If using an attributed string with a background color on the string doesn't work then you might need to create 2 separate labels with space between them and set the background color on each.
May be you need some space with that background color where text has ended. I have solved this problem in my own tricks. Add some comma or point to separate the string. Then apply this to the string. No extra label need to create.
NSArray *aArray = [#" Font Size .k" componentsSeparatedByString:#".k"];
NSMutableAttributedString *fulltext=[[NSMutableAttributedString alloc] initWithString:#""];
NSMutableAttributedString *title1=[[NSMutableAttributedString alloc] initWithString:[aArray objectAtIndex:0]];
NSMutableAttributedString *title2=[[NSMutableAttributedString alloc] initWithString:[aArray objectAtIndex:1]];
//[title1 addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:255.0/255.0 green:84.0/255.0 blue:49.0/255.0 alpha:1.0] range:NSMakeRange(0,title1.length)];
[title1 addAttribute:NSBackgroundColorAttributeName
value:[UIColor colorWithRed:255.0/255.0 green:84.0/255.0 blue:49.0/255.0 alpha:1.0]
range:NSMakeRange(0, title1.length)];
[title1 addAttribute:NSFontAttributeName
value:[UIFont boldSystemFontOfSize:(isIpad||isIPadPro)?19.0f:16.0f]
range:NSMakeRange(0,title1.length)];
[title2 addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(0,title2.length)];
[title2 addAttribute:NSBackgroundColorAttributeName
value:[UIColor clearColor]
range:NSMakeRange(0, title2.length)];
[title2 addAttribute:NSFontAttributeName
value:[UIFont boldSystemFontOfSize:(isIpad||isIPadPro)?19.0f:16.0f]
range:NSMakeRange(0,title2.length)];
[fulltext appendAttributedString:title1];
[fulltext appendAttributedString:title2];
self.textLabel.attributedText = fulltext;
My output is like:
Now make the K's background (title2) clear, so you can get the spaces after text end with your space!
//setting dummy text to label
self.lbLog.text=#"This is Simple Text With Red background Color";
//creating attributed string
NSMutableAttributedString *attribString =
[[NSMutableAttributedString alloc] initWithString:self.lbLog.text];
//setting background color to attributed text
[attribString addAttribute:NSBackgroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(0, attribString.length)];
//setting attributed text to label
self.lbLog.attributedText = attribString;
lblText.backgroundColor=UIColor.red
You need to use separate label where you set background colour of the label if background colour of all label text is same for all text in label otherwise for different background colour for some of the text you need to use NSMutableAttributedString.
label.backgroundColor = [UIColor colorWithRed:29.0/255.0 green:135.0/255.0 blue:145.0/255.0 alpha:1.0];
Use two custom Label (or instead label use uiview as your background), One is for your background,set backgroundcolor as clear color and another label is for ur text set backgroundcolor as your desire color.
We can do this with the help of UItextview. I did that, I will post the code very soon.

Is there an effective way to load a lot of text in a UITextView?

UITextView is causing my App to slow down because it holds a lot of text. I was wondering if there is some kind of third party library that can load more text effectively?
I am using attributedText to hold all of the text. If there is a better way please let me know. Any tips or suggestions are appreciated.
textView.attributedText = attributeStr;
Really? It worked fine on me. Please check this out on how I implemented my UITextView.The HELP_STATIC_CONTENT there is a constant variable that holds static texts for my Help screen which contains lot of texts (about 500 lines).
UITextView *lblHelp = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 20, self.view.frame.size.height - 20)];
[lblHelp setBackgroundColor:[UIColor clearColor]];
[lblHelp setEditable:NO];
[lblHelp setSelectable:NO];
lblHelp.showsVerticalScrollIndicator=NO;
lblHelp.showsHorizontalScrollIndicator=NO;
[self.view addSubview:lblHelp];
NSString *string = HELP_STATIC_CONTENT;
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.alignment=NSTextAlignmentJustified;
[paragraphStyle setLineSpacing:3];
[attrString beginEditing];
[attrString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, string.length)];
[attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];
[attrString endEditing];
lblHelp.attributedText = attrString;
If you want to change only a part of the text, you can directly change it with the textStorage property e.g:
[textView.textStorage addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:range];

Disabling UITextView Kerning

I'm trying to disable any kind of kerning in UITextView
Somehow in arabic letters the text view kern the letters to fit the line but it just ruin the whole word, here's an examples:
textview has white background, and the code is:
NSString *aya =[mainArray objectAtIndex:indexPath.row];
cell.tx.text = aya;
[cell.tx sizeToFit];
cell.tx.frame = CGRectMake(5, 5, 265, cell.tx.frame.size.height);
cell.tx.backgroundColor = [UIColor whiteColor];
also tried to set NSMutableAttributedString NSKernAttributeName value but didn't work,,
---------------------Edit: This is the code for Kern Attribute:
NSMutableAttributedString *attributedString;
attributedString = [[NSMutableAttributedString alloc] initWithString:aya];
[attributedString addAttribute:NSKernAttributeName
value:#0.0
range:NSMakeRange(0,[aya length])];
[attributedString addAttribute:NSFontAttributeName
value:font
range:NSMakeRange(0,[aya length])];
NSMutableParagraphStyle *paragrapStyle = [NSMutableParagraphStyle new];
paragrapStyle.alignment = NSTextAlignmentRight;
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragrapStyle
range:NSMakeRange(0,[aya length])];
[cell.tx setAttributedText:attributedString];
It's a hack and I'm not near a computer to try it, but give this a shot:
Set the tx's text property
[tx setText:aya];
Create an NSMutableAttributedString from the text view's text
NSMutableAttributedString *attrStr = [[tx attributedText] mutableCopy];
Set the kern attribute on the attributed string
[attrStr setAttributes:#{ NSKernAttributeName: #(0.f) }
range:NSMakeRange(0, [attrStr length])];
Set the attrStr back on tx
[tx setAttributedText:attrStr];
This will (hopefully) preserve the attributes UITextView infers from your NSString.

Two text colors in one UITableViewCell text label

I have a text label inside a UITableViewCell consisting of two words.
How can I change the color of the words; making the first word green, and the second word red?
NSString *twoWords = #"Green Red";
NSArray *components = [twoWords componentsSeparatedByString:#" "];
NSRange greenRange = [twoWords rangeOfString:[components objectAtIndex:0]];
NSRange redRange = [twoWords rangeOfString:[components objectAtIndex:1]];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:twoWords];
[attrString beginEditing];
[attrString addAttribute: NSForegroundColorAttributeName
value:[UIColor greenColor]
range:greenRange];
[attrString addAttribute: NSForegroundColorAttributeName
value:[UIColor redColor]
range:greenRange];
[attrString endEditing];
Then you can use attrString directly on a UILabel (> iOS 6, check Apple Documentation).
The simplest way to do this would be with an nsattributedstring in iOS 6.0 or later. You would allocate one of those and in the titleLabel (or any other object that holds text) of the UITableViewCell. If you're using the titleLabel you would do this:
[cell.titleLabel setAttributedText:yourAttributedString];
To setup the colors with an NSAttributedString, do this:
NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] initWithString:stringToManipulate];
[attributedString beginEditing];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, widthOfFisrtWord)];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(widthOfFisrtWord, widthOfSecondWord)];
[attributedString endEditing];
Note that the ranges provided above using NSMakeRange won't be the ranges you need. You'll have to change the range to fit your own needs depending if the two words have a space in between them or other characters.
Apple Documentation:
NSAttributedString
NSAttributedString UIKit Additions Reference
NSMutableAttributedString
This question addresses getting part of a string, which you would need to do. Instead of modifying the text with BOLD though, you can use this question to get an idea of how to change the color.
By using NSAttributedString string you can set two different colors.NSAttributedString
once check this one.

Resources