How can I underline text in iOS 6? - ios

I am trying to underline some text in a label. However, I do't know how to get the range of the entire text in the label. This is what I have so far:
NSMutableAttributedString *mat = [self.tableLabel.attributedText mutableCopy];
[mat addAttributes:#{NSUnderlineStyleAttributeName: #(NSUnderlineStyleSingle)} range://??];
self.tableLabel.attributedText = mat;
What should I put for the range?

For the range you may want to use:
NSMakeRange (0, mat.length);
Like this:
NSMutableAttributedString *mat = [self.tableLabel.attributedText mutableCopy];
[mat addAttributes:#{NSUnderlineStyleAttributeName: #(NSUnderlineStyleSingle)} range:NSMakeRange (0, mat.length)];
self.tableLabel.attributedText = mat;

NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:strComplete];
[attributedString addAttribute:NSUnderlineStyleAttributeName
value:#(NSUnderlineStyleSingle)
range:[strComplete rangeOfString:strFirst]];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:[strComplete rangeOfString:strSecond]];
cell.textLabel.attributedText = attributedString;

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.

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.

How to set multiple color text in table view in ios

I have a string ... Share Pictures .I want to show the Share in different color.I used NSMutableAttributedString to change the color of that part of the string.But when I am setting the cell.textLabel.text using the following string it doesn't work.Any other way to do this?
This way it doesn't work.
NSMutableAttributedString *string3 = [[NSMutableAttributedString alloc]initWithString:#"Share pictures "];
[string3 addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 4)];
NSString *tempStr3 = #"with your friends.";
NSString *finalString3 = [NSString stringWithFormat:#"%#%#" , string3, tempStr3];
[menuTextArray addObject:finalString3];
And in table view datasource method.
cell.textLabel.text = [menuTextArray objectAtIndex:indexPath.row];
You need to add only NSMutableAttributedString into your menuTextArray:
NSMutableAttributedString *yourString = [[NSMutableAttributedString alloc]initWithString:#"Share pictures with your friends"];
[yourString addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 4)];
[menuTextArray addObject:yourString];
then set attributedText
cell.textLabel.attributedText = [menuTextArray objectAtIndex:indexPath.row];
Use attributedText in place of text
cell.textLabel.attributedText = [menuTextArray objectAtIndex:indexPath.row];
Hope this code helps you.....
NSMutableAttributedString *string3 = [[NSMutableAttributedString alloc]initWithString:#"Share pictures with your friends"];
[string3 addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 5)];
[menuTextArray addObject:string3];
[cell.textLabel setAttributedText:string3];

How to append the following characters ‡, †, * as superscript to NSString in iOS

I need append the following characters ‡, †, * as superscript to NSString in iOS . Need your help. I use the following http://en.wikipedia.org/wiki/General_Punctuation_(Unicode_block) link but they are appending to NSString , But i want them as superscript
Try to use this one. And you need to #import <CoreText/CTStringAttributes.h>. This code works only in iOS6 or later version.
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 200, 40)];
NSString *infoString=#"X2 and H20 A‡ B† C*";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:infoString];
[attString addAttribute:(NSString *)kCTSuperscriptAttributeName value:#1 range:NSMakeRange(1, 1)];
[attString addAttribute:(NSString *)kCTSuperscriptAttributeName value:#-1 range:NSMakeRange(8, 1)];
[attString addAttribute:(NSString *)kCTSuperscriptAttributeName value:#1 range:NSMakeRange(12, 1)];
[attString addAttribute:(NSString *)kCTSuperscriptAttributeName value:#1 range:NSMakeRange(15, 1)];
[attString addAttribute:(NSString *)kCTSuperscriptAttributeName value:#1 range:NSMakeRange(18, 1)];
lbl.attributedText = attString;
[self.view addSubview:lbl];
Output
I hope this will help you
NSString does not allow you to format specific parts of the text. If you're planning to display your text in a UILabel, UITextField, or UITextView (and your app doesn't have to run on anything below iOS 6), you can use NSAttributedString and apply a kCTSuperscriptAttributeName attribute with the value #1. If you're using a UIWebView, use HTML's <sup> element.
This is how you could achieve that:
NSString *string = #"abcdefghi";
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSInteger num1 = 1;
CFNumberRef num2 = CFNumberCreate(NULL, kCFNumberNSIntegerType, &num1);
[attrString addAttribute:(id)kCTSuperscriptAttributeName value:(id)CFBridgingRelease(num2) range:NSMakeRange(6, 3)];
self.label.attributedText = attrString;
, where label is a UILabel property already added to the UI.
Make sure you add first the CoreText framework, also add this line on top of you .m file.
Hope it helps you somehow
Swift
Step 1.
import CoreText
Step 2.
let range1 = NSMakeRange(1, 1)
let range2 = NSMakeRange(5, 1)
let mutableStr : NSMutableAttributedString = NSMutableAttributedString(string: "h20 x2")
mutableStr.addAttribute(kCTSuperscriptAttributeName as String, value:-1, range: range1)
mutableStr.addAttribute(kCTSuperscriptAttributeName as String, value:1, range: range2)
self.lbl.attributedText = mutableStr
Output:

Resources