Multiple line with underline in UIButton title in iOS-5 - ios

I want to create underlined button link following image in iOS.
I already used UIUnderlineButton
so it gives me following output.
please help me to achieve underlined button like 1st image.

This should do the trick for iOS6 and above, using attributedString:
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:#"yourTextButton"];
[attrStr addAttribute:NSUnderlineStyleAttributeName value:#(NSUnderlineStyleSingle) range:NSMakeRange(0, [attrStr length])];
[yourButton setAttributedTitle:attrStr forState:UIControlStateNormal];
[[yourButton titleLabel] setNumberOfLines:0];//Multiple Lines

Why don't you use a UILabel and UITapGestureRecognizer when your text is this long and you don't need any additional classes.

Check this out. I hope it is what you want.
NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:#"Tap here...."];
[titleString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [titleString length])];
[titleString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, [titleString length])];
[_button setAttributedTitle: titleString forState:UIControlStateNormal];

Related

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.

Underline in attributed text not working in iPhone 6+

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.

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];

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.

Adding color to text in TextView

I have a TextView in which I need some text to change colors and fonts. Any method is available other than core text. Please help.
i need some text to change colors and fonts.
You certainly looking for NSAttributedString
From Source
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:#"Hello. That is a test attributed string."];
[str addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(3,5)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(10,7)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(20, 10)];
label.attributedText = str;

Resources