Cannot set underline for UILabel [duplicate] - ios

This question already has answers here:
How to make an underlined text in UILabel?
(7 answers)
Closed 4 years ago.
I have a UILabel which have text: "Something here and privacy policy", I just want to create underline for the text privacy policy but the code not affect. If I create underline for full text of my label then it worked, but it's not what I want.Here is my code:
NSString *str = self.myLabel.text;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
NSRange index = [str rangeOfString:#"privacy policy"];
if (index.location != NSNotFound){
[attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(index.location, index.length)];
//if I change index.location by 0 then it worked,
//with the underline start at the begin text of mylabel but that not what I expected
[self.myLabel setAttributedText:attributedString];
}
Is have something wrong with my code(my app support for iOS 7 and latter)?

Please try this piece of code.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 250, 40)];
label.font=[UIFont fontWithName:#"Helvetica" size:12];
label.textColor=[UIColor blackColor];
NSString *text = #"Something and Privacy Policy";
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc ]initWithString:text];
NSInteger location = [#"Something and " length]-1;
NSInteger length = [#"Privacy Policy" length];
[attrString addAttribute:NSUnderlineStyleAttributeName value:#(NSUnderlineStyleSingle) range:NSMakeRange(location, length)];
label.attributedText=attrString;
[self.view addSubview:label];

To temporarily fix your issue, you can split your String into 2 NSMutableAttributedString with latter string having text "privacy policy". You can then underline the whole text and concatenate the two strings.
NSString *str = self.myLabel.text;
NSRange index = [str rangeOfString:#"privacy policy"];
if (index.location != NSNotFound){
NSString *tempString = [str substringWithRange:NSMakeRange(0, index.location - 1)];
NSAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:tempString];
NSString *privacyString=[str substringFromIndex:index.location];
NSAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:privacyString];
[attributedString1 addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(0, attributedString1.length)];
//with the underline start at the begin text of mylabel but that not what I expected
NSMutableAttributedString *mutableAttString = [[NSMutableAttributedString alloc] init];
[mutableAttString appendAttributedString: attributedString];
[mutableAttString appendAttributedString:attributedString1];
[self.myLabel setAttributedText:mutableAttString];
}

The best way I've found to underline a portion of a text is to use Interface Builder (Designer first, coder second). Granted this might not be what you want but for a temporary solution I propose the following:
Instructions
1) Place a UILabel in your viewController of choice and add constraints as needed.
2) Click on your label, replace the text and go to Attributes Inspector. (Image 1)
3) Change your UILabel from 'Plain Text' to 'Attributed'
4) Highlight the portion you want underlined as shown in Image 2 below and press the 'T' in a square.
5) On the toolbar of the window that shows up, you'll see an underlined 'T'. Select 'single' (Image 3).
Images

Related

Find last line of multiline label and change colour [duplicate]

This question already has answers here:
UILabel Text with Multiple Font Colors
(8 answers)
Closed 4 years ago.
I m working on multi line text with different colour.The following code to change the first line colour But want to change the last line colour.
NSRange rangeOfNewLine = [label.text rangeOfString:#"\n"];
NSRange newRange = NSMakeRange(0, rangeOfNewLine.location);
[text addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:newRange];
NSArray* lines = [label.text componentsSeparatedByString:#"\n"];
NSString* lastLine = lines.lastObject;
NSRange rangeOfLastLine = [label.text rangeOfString:lastLine];
[text addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:rangeOfLastLine];
//NSString *myString = #"I have to replace text 'Dr Andrew Murphy, John Smith' ";
NSString *myString = #"Not a member?signin";
//Create mutable string from original one
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:myString];
//Fing range of the string you want to change colour
attributeText _label.attributedText = attString;
//If you need to change colour in more that one place just repeat it
NSRange range = [myString rangeOfString:#"signin"];
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:(63/255.0) green:(163/255.0) blue:(158/255.0) alpha:1.0] range:range];
//Add it to the label - notice its not text property but it's
attributeText _label.attributedText = attString;

Bolding an nsstring

I have looked online for ways to bold a NSString and either it's not there or I can't seem to find it. I simply want to bold the word in the NSString but so far, what I have done is not woking:
NSString *player1Name = [[UIFont boldSystemFontOfSize:12] fontName];
I have relied on this to make "player1Name" bold but it doesn't work at all.
When I run it I get this:
Thanks in advance to anyone who helps me figure out the solution.
You can not bold a NSString... Try a NSAttributedString...
NSAttributedString *player1Name = [[NSAttributedString alloc] initWithString:#"name" attributes:#{NSFontAttributeName : [UIFont boldSystemFontOfSize:12]}];
https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/
fontName is a property on UIFont
[[UIFont boldSystemFontOfSize:12] fontName]
returns the font name HelveticaNeueInterface-MediumP4
Try this:
NSString *myString = #"My string!";
NSAttributedString *myBoldString = [[NSAttributedString alloc] initWithString:myString
attributes:#{ NSFontAttributeName: [UIFont boldSystemFontOfSize:35.0] }];
Also you can set range for particular text like:
NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSString *yourString = ...;
NSRange boldedRange = NSMakeRange(2, 4);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];
[attrString beginEditing];
[attrString addAttribute:kCTFontAttributeName
value:boldFontName
range:boldedRange];
[attrString endEditing];
You can't make NSString bold. Instead of that you can make UILabel text into bold. Because UILabels are for UI representation & NSStrings are just objects. So make your UILabel bold
[infoLabel setFont:[UIFont boldSystemFontOfSize:16]];

NSTextView with long NSMutableAttributedText low scrolling and loading speed

I have a uitextview that most of times should show long attributedtexts. I use below code for creating my attributed text:
//Add each line of doa to an element of array for future usage
NSArray *paragraphs = [self.Text componentsSeparatedByString:#"\r\n"];
MixedContent= [[NSMutableAttributedString alloc]init];
ArabicContent= [[NSMutableAttributedString alloc]init];
TranslatedContent= [[NSMutableAttributedString alloc]init];
NSString * temp=#"";
for (int i=0; i< paragraphs.count; i++) {
if([paragraphs[i] hasPrefix:#"$2."]){
temp= [paragraphs[i] stringByReplacingOccurrencesOfString:#"$2." withString:#"" options:1 range:NSMakeRange(0, 3)];
NSMutableAttributedString* tempMutable= [[NSMutableAttributedString alloc] initWithString:temp attributes:doaDescriptionAttributes];
//Go to next line
[tempMutable appendAttributedString:[[NSMutableAttributedString alloc] initWithString:#"\n\n"]];
[ArabicContent appendAttributedString: tempMutable];
[TranslatedContent appendAttributedString:tempMutable];
[MixedContent appendAttributedString:tempMutable];
}else if ([paragraphs[i] rangeOfString:#"$3."].location != NSNotFound){
temp= [paragraphs[i] stringByReplacingOccurrencesOfString:#"$3." withString:#"" options:1 range:NSMakeRange(0, 3)];
NSMutableAttributedString* tempMutable= [[NSMutableAttributedString alloc] initWithString:temp attributes:arabicDoaAttributes];
[MixedContent appendAttributedString:tempMutable];
[MixedContent appendAttributedString:[[NSMutableAttributedString alloc] initWithString:#"\n"]];
[ArabicContent appendAttributedString:tempMutable];
[ArabicContent appendAttributedString: [[NSMutableAttributedString alloc] initWithString:#" "]];
}else if ([paragraphs[i] rangeOfString:#"$4."].location != NSNotFound){
temp= [paragraphs[i] stringByReplacingOccurrencesOfString:#"$4." withString:#"" options:1 range:NSMakeRange(0, 3)];
NSMutableAttributedString* tempMutable= [[NSMutableAttributedString alloc] initWithString:temp attributes:arabicDoaAttributes];
[MixedContent appendAttributedString:tempMutable];
[MixedContent appendAttributedString:[[NSMutableAttributedString alloc] initWithString:#"\n"]];
[TranslatedContent appendAttributedString:tempMutable];
[TranslatedContent appendAttributedString: [[NSMutableAttributedString alloc] initWithString:#" "]];
}
temp=nil;
}
}
My text is contain 3 kind of text. Some description. Some Arabic text and their translation. Each of this part is showing with a specific color.
And I use this code for setting the text:
self.myTextView.attributedText= myAttributedText;
the problem is that when I set or change the text of this textview it takes about 2 seconds that app set or update the text. The waiting time is not good for my users.
Also when I scroll the textview it is not good at all. It scroll some paragraphs and then stop for and then scroll, stop and scroll,....
Actually it is slow on scrolling.
Is there any better ways for setting the text so that I have a better performance?
UPDATE
I ma thinking whethere it is possible to load the text by scrolling. Some thing like a lazy loader list but for uitextview. Is it possible?
UPDATE
When I add the text without any attributes. (I mean all lines with same color) The scroll works fine.
UPDATE
It is about the amount of texts. When I reduce the amount of texts. For example instead of setting 10000 character I set 500 character and now it is pretty fast. How can I fix it?

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