Replace the truncation ellipsis of UILabel in iOS 7 - ios

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

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

How can I show the text with different fonts in textView in ios?

Hi I am doing one application. In that I need to show text in different fonts. I mean if I show name in one font and I show city name in different font.
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 200, 100)];
textView.text = #"Vijay Apple Dev";
NSString *textViewText = textView.text;
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:textViewText];
[attributedText addAttribute:NSFontAttributeName
value:[UIFont fontWithName:#"Courier" size:20]
range:[textViewText rangeOfString:#"Apple"]];
[attributedText addAttribute:NSFontAttributeName
value:[UIFont fontWithName:#"Verdana" size:20]
range:[textViewText rangeOfString:#"Vijay"]];
textView.attributedText = attributedText;
[self.view addSubview:textView];
Try to do like this one... And customize according to your requirement like font name and font size
NSString *firstName = #"FirstName";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:firstName];
UIFont *font = [UIFont fontWithName:#"Helvetica" size:20];
[attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, firstName.length)];// Here define your range.
textView.attributedText = attributedString;
//Use attributed String
//Apply properties to both strings
//then merge and set it to textview
NSString *selectedString=#"Hey,";
UIFont *fontSelectedText = [UIFont boldSystemFontOfSize:25];
NSDictionary *dictBoldSelectedText = [NSDictionary dictionaryWithObjectsAndKeys:fontSelectedText, NSFontAttributeName, nil];
NSMutableAttributedString *mutAttrTextViewSelectedString = [[NSMutableAttributedString alloc] initWithString:selectedString];
[mutAttrTextViewSelectedString setAttributes:dictBoldSelectedText range:NSMakeRange(0,selectedString.length)];
[_tTextView setAttributedText:mutAttrTextViewSelectedString];
//Second String
NSString *restOfStrig=#"\nHello";
NSMutableAttributedString *mutAttrTextViewString = [[NSMutableAttributedString alloc] initWithString:restOfStrig];
UIFont *fontText = [UIFont boldSystemFontOfSize:16];
NSDictionary *dictBoldText = [NSDictionary dictionaryWithObjectsAndKeys:fontText, NSFontAttributeName, nil];
[mutAttrTextViewString setAttributes:dictBoldText range:NSMakeRange(0,restOfStrig.length)];
//Combining both the Attributed String
[mutAttrTextViewSelectedString appendAttributedString:mutAttrTextViewString];
NSMutableAttributedString * finalAttributedStr=[[NSMutableAttributedString alloc] initWithAttributedString:mutAttrTextViewSelectedString];
//setting it to the textView
[_tTextView setAttributedText:finalAttributedStr];
Use NSAttributedString
An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string. The cluster’s two public classes, NSAttributedString and NSMutableAttributedString, declare the programmatic interface for read-only attributed strings and modifiable attributed strings, respectively.
NSAttributedString
When you create your attributed string, then just set it to your textView:
self.textView.attributedText = myAttributedString;

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

Increasing the font size of first letter in UILabel.text

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

Setting custom font in UITextView with attributed string

I have a UITextView which contains an attributed string configured through storyboard. Now i want a custom font to be applied. I am doing it through code
textView.font = [UIFont fontWithName:kCustomFont size:textView.font.pointSize];
The problem is fonts get changed losing all the attributes. How do i fix it?
(Working on iOS 5 and above)
What's in your kCustomFont? If the FontName is wrong, even a single Character, the standard SystemFont will be used.
You should change it via NSMutableAttributedString's attributes.
I.e. use something like:
NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc] initWithAttributedString:textView.text];
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
[mutableSrting addAttributes:attrs range:NSMakeRange(0, [mutableString length])];
textView.attributedText = mutableString;
[mutableString release];

Resources