Increasing the font size of first letter in UILabel.text - ios

I would like the first letter of the UILabel to have a different font size from others, a few font size larger. Need some guidance on how to do it. The label has a lot of words.
This is what I have tried:
NSArray * words = [Label.text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *firstLetter = [[words objectAtIndex:0] substringToIndex:1];
But got stuck in increasing the size of the NSString. Is there a better way? I am welcome to suggestions and guidance.. Thanks..
EDIT:
[Label setFont:[UIFont fontWithName:#"Arial" size:12.f]];
NSArray * words = [Label.text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
UIFont *fontFirst=[UIFont fontWithName:#"Arial" size:15.f];
NSDictionary *attrsDictFirst=[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSAttributedString *finalString=[[NSAttributedString alloc] initWithString:[[words objectAtIndex:0] substringToIndex:1] attributes:attrsDictFirst];

To get, for example, this:
Say this:
NSString* s2 = #"Fourscore and seven years ago, our fathers brought forth "
#"upon this continent a new nation, conceived in liberty and dedicated "
#"to the proposition that all men are created equal.";
NSMutableAttributedString* content2 =
[[NSMutableAttributedString alloc]
initWithString:s2
attributes:
#{
NSFontAttributeName:
[UIFont fontWithName:#"HoeflerText-Black" size:16]
}];
[content2 setAttributes:
#{
NSFontAttributeName:[UIFont fontWithName:#"HoeflerText-Black" size:24]
} range:NSMakeRange(0,1)];
[content2 addAttributes:
#{
NSKernAttributeName:#-4
} range:NSMakeRange(0,1)];
NSMutableParagraphStyle* para2 = [NSMutableParagraphStyle new];
para2.headIndent = 10;
para2.firstLineHeadIndent = 10;
para2.tailIndent = -10;
para2.lineBreakMode = NSLineBreakByWordWrapping;
para2.alignment = NSTextAlignmentJustified;
para2.lineHeightMultiple = 1.2;
para2.hyphenationFactor = 1.0;
[content2 addAttribute:NSParagraphStyleAttributeName
value:para2 range:NSMakeRange(0,1)];
Then assign content2 to a label's attributedText.

Need to use NSAttributedString. iOS6.0 onwards
After getting the first letter make put it into attributed string, change its size.
take rest of the string set other size.
UIFont *font=[UIFont fontWithName:#"Arial" size:12.f];
NSDictionary *attrsDict=[NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
NSAttributedString *attribString=[[NSAttributedString alloc] initWithString:[words[0] substringFromIndex:1] attributes:attrsDict];
UIFont *fontFirst=[UIFont fontWithName:#"Arial" size:15.f];
NSDictionary *attrsDictFirst=[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSAttributedString *firstString=[[NSAttributedString alloc] initWithString:[attribString subStringToIndex:1] attributes:attrsDictFirst];
[attribString replaceCharactersInRange:NSMakeRange(0, 1) withString:firstString];

Related

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

Is is possible to format a specific part of an string within objective-c?

Is it possible to format a section of text in a string within the actual code of xcode to be displayed in a single label? For instance make a specific word bold while leaving the rest normal.
Or do I have to use separate strings with different labels with the specific formatting already determined?
Use attributed text. Example below bolds the word posted By but not the name
NSString *format = NSLocalizedStringFromTable(#"Posted By: %#", #"Details Screen", #"posted by %#");
NSString *text = [NSString stringWithFormat:format, name];
const CGFloat fontSize = 14.0;
UIFont *boldFont = [UIFont fontWithName:#"HelveticaNeue-Bold" size:fontSize];
UIFont *regularFont = [UIFont fontWithName:#"HelveticaNeue" size:fontSize];
UIColor *foregroundColor = [UIColor colorWithWhite:(51.0/255.0) alpha:1.0];
// Create the attributes
NSDictionary *attrs = #{NSFontAttributeName: boldFont,
NSForegroundColorAttributeName: foregroundColor};
NSDictionary *subAttrs = #{NSFontAttributeName: regularFont};
const NSRange range = NSMakeRange(text.length - name.length,name.length); // range of " Posted by: "
// Create the attributed string (text + attributes)
NSMutableAttributedString *attributedText =
[[NSMutableAttributedString alloc] initWithString:text
attributes:attrs];
[attributedText setAttributes:subAttrs range:range];
// Set it in our UILabel and we are done!
[_textLabel setAttributedText:attributedText];

Replace the truncation ellipsis of UILabel in iOS 7

How can I replace the truncation ellipsis ("…") of a UILabel in iOS 7 with another attributed character? For example, with a colored ">".
I was hoping Text Kit's NSLayoutManager would make this possible, but it appears UILabel doesn't make it public if it uses it.
Also, can I safely assume that an ellipsis is used as the truncation character in every localisation? Maybe different languages have different truncation characters.
I recommend you use TTTAttributedLabel, just set property "attributedTruncationToken" to your custom string.
I don't think it gives you access to this. I think you would have do handle it manually. For example, use TextKit to determine the size of your string, if it doesn't fit in the available area, truncate it yourself and append a ">" and then put your new string in the label.
NSAttributedString has methods for getting the size of the string.
Let me know if you need any more detail on this..?
I think you can do some customization in -replaceElipsesForLabel method provided by Fonix to get your desired result.
I have written a method to do it, and works in iOS7
-(void)setCustomEllipsis:(NSString*)customEllipsis inLabel:(UILabel*)label with:(NSString*)string{
//Replace the ellipsis
NSMutableString* result = [[NSMutableString alloc] initWithString:#""];
NSArray* strings = [string componentsSeparatedByString:#" "];
for (NSString* s in strings) {
CGRect newSize = [[NSString stringWithFormat:#"%#%#%#",result,s,customEllipsis] boundingRectWithSize:CGSizeMake(label.frame.size.width,0) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:label.font} context:nil];
if (newSize.size.height < label.frame.size.height) {
[result appendString:s];
[result appendString:#" "];
}else{
[result appendString:customEllipsis];
break;
}
}
[label setText:result];
//Set different font to the ellipsis
const CGFloat fontSize = 13;
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
UIColor *foregroundColor = [UIColor lightGrayColor];
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:regularFont, NSFontAttributeName,foregroundColor, NSForegroundColorAttributeName, nil];
NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:boldFont, NSFontAttributeName, nil];
const NSRange range = [label.text rangeOfString:customEllipsis];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:result
attributes:attrs];
[attributedText setAttributes:subAttrs range:range];
[label setAttributedText:attributedText];
}

iOS - UITableViewCell make text bold

I have a string:
NSString *userInfo = #"James Johnson #james";
What i want to do is bold James Johnson and keep #james normal font.
So what I have tried is using NSAttributedString but somewhere I'm doing something wrong in order to complete the process.
This is what I have tried:
NSString *user = #"James Johnson #james";
UIFont *fontLight = [UIFont fontWithName:#"HelveticaNeue-Light" size:14];
UIFont *fontBold = [UIFont fontWithName:#"HelveticaNeue-Bold" size:14];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:userInfo];
//TESTING WITH RANDOM PARTS OF THE STRIN
[string addAttribute:NSForegroundColorAttributeName value:fontLight range:NSMakeRange(0, 3)];
[string addAttribute:NSForegroundColorAttributeName value:fontBold range:NSMakeRange(3, 5)];
NSString *str = [string string];
cell.textLabel.text = str;
Is there a way I can make this work even if I'm on the wrong direction?
What's not working
For some reason, the characters from range 0 - 3 is not being a light font...instead the entire cell.textLabel.text is bold somehow and is not font size 14 which i had specified in the UIFont.
Your last part of it is wrong. You must create your NSAttributedString and finally trash the formatting by using
NSString *str = [string string];
As NSString doesn't know anything about formatting you have to use the NSAttributedString to assign it to the cell's textLabel:
cell.textLabel.attributedText = string;
You should set attributedString to attributed text
Comment these lines
//NSString *str = [string string];
//cell.textLabel.text = str;
And write this
cell.textLabel.attributedText = string;//Set NSMutableAttributedString only not NSString
UILabel has a property attributedText. You should be assigning this property with your attributed string and not use text property.
Make changes in you code as follows :
You will require to report all the characters with in the NSMutableAttributedString, so specify them with in the range, while adding an attribute.
Provide correct attribute name, else you will have an exception here, in this case you should use "NSFontAttribteName" in place of "NSForegroundColorAttributeName".
NSString *user = #"James Johnson #james";
UIFont *fontLight = [UIFont fontWithName:#"HelveticaNeue-Light" size:14];
UIFont *fontBold = [UIFont fontWithName:#"HelveticaNeue-Bold" size:14];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:user];
[string addAttribute:NSFontAttributeName value:fontLight range:NSMakeRange(0, 20)];
[string addAttribute:NSFontAttributeName value:fontBold range:NSMakeRange(0, 5)];
cell.textLabel.attributedText = string;

Change font of separate strings detailTextLabel

How can I change the font of one string in the detailTextLabel of my UITableViewCell?
NSString *detailStr = [NSString stringWithFormat:#"%# %#",post.score,post.domain];
cell.detailTextLabel.text = detailStr;
I basically want the post.score string to be one color and the post.domain string to be another.
Answer : NSAttributedString.
Try this :
int count1 = [post.score length];
int count2 = [post.domain length];
NSMutableAttributedString *textLabelStr = [[NSMutableAttributedString alloc] initWithString:#"%# %#",post.score,post.domain];
[textLabelStr setAttributes:#{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(0, count1)];
[textLabelStr setAttributes:#{NSForegroundColorAttributeName : [UIColor blueColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(count1 + 1, count2)];
cell.detailTextLabel.attributedText = textLabelStr;
Note : Not tested, but Write the Code to help you.
If you're happy to use iOS6+ APIs then check out NSAttributedString and use cell.detailTextLabel.attributedText = ...
I think your looking for the setAttributedText: method of UILabel. You can pass an NSAttributedStringto it in order to stylize different portions of the string in different ways.
I wrote a sample below that should do what you're asking for.
NSAttributedString *scoreAttributedString = [[NSAttributedString alloc] initWithString:[post score] attributes:#{NSForegroundColorAttributeName: [UIColor redColor]}];
NSAttributedString *domainAttributedString = [[NSAttributedString alloc] initWithString:[post domain] attributes:#{NSForegroundColorAttributeName: [UIColor greenColor]}];
NSMutableAttributedString *detailMutableAttributedString = [[NSMutableAttributedString alloc] init];
[detailMutableAttributedString appendAttributedString:scoreAttributedString];
[detailMutableAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:#" "]];
[detailMutableAttributedString appendAttributedString:domainAttributedString];
[[cell detailTextLabel] setAttributedText:detailMutableAttributedString];

Resources