For some reason, when I display the word "Butterfly" (or anything else with an "fl"), the "f" and the "l" connect at the top (see image below). The font is Century Gothic Bold. This is the code I use to setup the UILabel. The string for the label is retrieved from a plist:
UILabel *label = [[UILabel alloc] init];
label.text = [self.flashcardDelegate.alphabetArr objectAtIndex:index];
label.textColor = [UIColor colorWithRed:.733 green:0 blue:.03137 alpha:1];
label.backgroundColor = [UIColor clearColor];
label.frame = CGRectMake(ipad ? 210.0f : 65.0f,
ipad ? 650.0f : 300.0f,
ipad ? 340.0f : 185.0f,
ipad ? 250.0f : 135.0f);
label.font = [UIFont fontWithName:#"CenturyGothic-Bold" size:ipad ? 200 : 100];
label.textAlignment = UITextAlignmentCenter;
Any idea why this would happen? Thanks.
It's called a ligature, and it's intentional. You can change how the OS renders ligatures by using the kCTLigatureAttributeName attribute if you're using Core Text, or you can use NSLigatureAttributeName if you're using NSAttributedString.
What you're seeing is known as a ligature - the idea was originally to make it easier to handle the kerning on metal blocks for printing presses (to use one glyph for commonly-joined character pairs), but now has persisted into the modern era as a largely stylistic decision.
I'm unsure how to disable it in the API, but hopefully this additional background information can help you find the answer.
Here is a short way of doing this. iOS 6.0+
NSMutableAttributedString *attributedString;
attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
[attributedString addAttribute:NSLigatureAttributeName value:#0 range:NSMakeRange(0, label.text.length)];
[label.text setAttributedText:attributedString];
[attributedString release];
Related
Got a iPhone 4 in the field and a strange problem, the UILabel does not show up any text. I tested it on iPhone 4S + iOS 7 simulator, it works fine.
Code:
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[colLabel.text copy]];
[attributeString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:1]
range:(NSRange){0,[attributeString length]}];
colLabel.text = nil;
colLabel.attributedText = [attributeString copy];
colLabel.font = [UIFont boldSystemFontOfSize:12];
I have checked. Its showing on iPhone 4, there may be something else. Clean build and delete from device and run again
I have been played with the attributed text for a while, and find out something new:
It seems like on iOS 7.0.x, NSUnderlineStyleAttributeName does not play well with other attributes like color or font, once they are bundled together, it just will not show up the text. Only having underline style actually could draw the text like below:
NSAttributedString* attrStr = [[NSAttributedString alloc] initWithString:colLabel.text
attributes:#{NSUnderlineStyleAttributeName:#(NSUnderlineStyleSingle)}];
colLabel.attributedText = attrStr;
But once you add something like
colLabel.backgroundColor = [UIColor greenColor];
or
colLabel.font = [UIFont boldSystemFontOfSize:12];
It just won't show up, unless you make two changes: appended a newline character to your original string, and set the label's numberOfLines to 2.
like:
NSAttributedString* attrStr =
[[NSAttributedString alloc] initWithString:#"TEST\n" // <---
attributes:
#{NSUnderlineStyleAttributeName:#(NSUnderlineStyleSingle),
NSParagraphStyleAttributeName:paragraph}];
UILabel* myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 0, 0)];
myLabel.backgroundColor = [UIColor greenColor];
myLabel.attributedText = attrStr;
[myLabel sizeToFit];
myLabel.numberOfLines = 2; // <---
This question already has answers here:
UILabel with two different color text
(5 answers)
Closed 9 years ago.
I am recently learning iOS development, i am wondering if that is possible to have one label with different size, different fonts and different colors, such as
user name(blue, bolder, and large font) is watching walking dead (red, bolder and medium font) on xxxx site (blue, bolder and large font)
Thanks!
There are 2 ways to do this
xib file or storyboard
go the label, and choose attribute instead of plain
then do whatever you want there
codes
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:#"user name is watching walking dead on xxx site"];
[attributedString addAttributes:[[NSDictionary alloc] initWithObjectsAndKeys:
NSFontAttributeName, [UIFont fontWithName:#"WHATEVER FONT" size:FONT_SIZE_HERE],
NSForegroundColorAttributeName, [UIColor blueColor],
nil]
range:NSMakeRange(0, 9)];//9 is the length of "user name"
[attributedString addAttributes:[[NSDictionary alloc] initWithObjectsAndKeys:
NSFontAttributeName, [UIFont fontWithName:#"WHATEVER FONT" size:FONT_SIZE_HERE],
NSForegroundColorAttributeName, [UIColor blueColor],
nil]
range:NSMakeRange(22, 12)];//22 is the start index of "Walking dead"
//and 12 is the length of "Walking dead"
//you got the idea, same way to do the xxx site.
//Check a file called "NSAttributedString.h"
//you will find even more options there
I personally prefer the second solution, since you have more options in the code, and it works for pretty much all the cases. But there is a learning curve.
Hope that helps
In your case you need to use NSMutableAttributedString.
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString: #"monkey goat"];
UILabel *testingLbl = [[UILabel alloc] init];
testingLbl.frame = CGRectMake(50, 100, 200, 30);
[attString addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(0,6)];
[attString addAttribute: NSFontAttributeName value: [UIFont fontWithName:#"Helvetica" size:15] range: NSMakeRange(0,6)];
[attString addAttribute: NSFontAttributeName value: [UIFont fontWithName:#"Didot" size:24] range: NSMakeRange(7,4)];
testingLbl.attributedText = attString;
[self.view addSubview:testingLbl];
And also this is the best site for find any custom controllers, Bye the way in your case fine UILabel with source code:
I'm trying to draw ellipsized text on multiple lines inside a rectangle.
For NSLineBreakByTruncatingTail, the documentation states
The line is displayed so that the beginning fits in the container and the missing text at the end of the line is indicated by an ellipsis glyph. Although this mode works for multiline text, it is more often used for single line text.
but with that mode I only get a single line:
However with NSLineBreakByWordWrapping, I don't get an ellipsis for overlong text:
Both pictures use the same code below (red background is the text drawing rectangle) and of course the same rectangle size, so 2 lines should definitely fit.
NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = <<see above>>;
paragraphStyle.alignment = NSTextAlignmentNatural;
NSDictionary* drawingAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"HelveticaNeue" size:36],
NSFontAttributeName,
paragraphStyle,
NSParagraphStyleAttributeName,
nil];
const CGRect rect = CGRectMake(...);
[#"I do not get ellipsized in any way" drawInRect:rect withAttributes:drawingAttributes];
Is there a way to combine ellipsizing with multiline rendering, as the documentation says? With a UILabel, I would only have to set the number of lines to something other than 1, but what about text rendering via code?
I don't understand why you are using NSMutableParagraphStyle and drawInRect, probably I'll need more information about the context you are trying to work.
But using this simple technique:
UILabel *label = [[UILabel alloc] initWithFrame:(CGRect){0,0,300,100}];
label.numberOfLines = 0;
label.backgroundColor = [UIColor redColor];
label.center = self.view.center;
label.font = [UIFont fontWithName:#"HelveticaNeue" size:36];
label.textAlignment = NSTextAlignmentNatural;
label.lineBreakMode = NSLineBreakByTruncatingTail;
label.text = #"I do not get ellipsized in any way";
[self.view addSubview:label];
You will see the following result:
My suggestion is to get rid of drawInRect in this case.
This example below is supposed to draw a stroke together with fill but it does not. What is wrong? I am using negative value to have stoke showed up according to Apple's documentation. If I make it a positive value, then text completely disappears.
UITextView *rte = [[UITextView alloc]initWithFrame:
CGRectMake(50,50,100,100)];
[self.view addSubview:rte];
NSDictionary *typingAttributes = #{
NSFontAttributeName: [UIFont fontWithName:#"Helvetica-Bold" size:20.0f],
NSForegroundColorAttributeName : [UIColor redColor],
NSStrokeColorAttributeName : [UIColor yellowColor],
NSStrokeWidthAttributeName : [NSNumber numberWithFloat:-2.0]
};
NSAttributedString *str = [[NSAttributedString alloc]
initWithString:#"Enter text here..."
attributes:typingAttributes];
rte.attributedText = str;
I have tried your code in iOS 7.0.3 simulator and got the result:
It is not working in iOS 6. I think it is a bug.
This approach is working
Sorry I don't have any explanation why in iOS 6 it doesn't work.
I need to underline a certain portion of a UILabel as the title suggests. For example: Please click ESPNSoccernet to read the latest Football News. I would like to underline the word ESPNSoccernet. This is because I want it to be clickable and it need to link to the website.
Need some guidance on doing this. If there is another way, do tell me...
for ios 6, you can use AttributedStrings
NSMutableAttributedString *yourString = [[NSMutableAttributedString alloc] initWithString:#"Please click ESPNSoccernet to read the latest Football News."];
[yourString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:1]
range:(NSRange){0,25}];
label.attributedText = [yourString copy];
you can also use a 3rd party UILable library TTTAttributedLabel.
In Xcode:
Select the label and choose identity inspector.
In text choose Attributed instead of plain.
Now Select the portion of text you want to underline.
Select font and choose underline in fonts style.
There you go.
Swift 2.0:
1) Make a nsmutablestring with underline attribute and add to sampleLabel's text.
2) Add a tap gesture to sampleLabel and associate a method for further action.
override func viewDidLoad() {
super.viewDidLoad()
let newsString: NSMutableAttributedString = NSMutableAttributedString(string: "Tap here to read the latest Football News.")
newsString.addAttributes([NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue], range: NSMakeRange(4, 4))
sampleLabel.attributedText = newsString.copy() as? NSAttributedString
let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapResponse:")
tapGesture.numberOfTapsRequired = 1
sampleLabel.userInteractionEnabled = true
sampleLabel.addGestureRecognizer(tapGesture)
}
func tapResponse(recognizer: UITapGestureRecognizer) {
print("tap")
}
UILabel is only capable of displaying plain text strings (in iOS 6 it can now also display NSAttributedStrings, but this will not work in older iOS versions, so it is best not to rely on this), so you will not be able to do this with a label.
You can look at TTTAttributedLabel for displaying attributed text (so you can add underlines and other formatting), but you will not be able to add hyperlinks with this class.
The options you have for a clickable segment of the string are basically:
Use a plain UILabel and overlay a UIButton over the part that you want to be clickable, or
Use TTTAttributedLabel to achieve the underline effect, and a UITapGestureRecognizer to detect and handle taps (note that this will capture taps on the entire label, not just the underlined part).
For iOS 6:
UILabel *label = [[UILabel alloc] init];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:#"Tap here to read the latest Football News."];
[string addAttribute:NSUnderlineStyleAttributeName value:#(1) range:NSMakeRange(4, 4)];
label.attributedText = [string copy];
For earlier iOS versions as well as iOS 6:
TTTAttributedLabel *label = [[TTTAttributedLabel alloc] init];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:#"Tap here to read the latest Football News."];
[string addAttribute:NSUnderlineStyleAttributeName value:#(1) range:NSMakeRange(4, 4)];
label.text = [string copy];
Then add a gesture recogniser and implement handleTap::
UITapGestureRecognizer *recogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[label addGestureRecognizer:recogniser];
- (void)handleTap:(UITapGestureRecognizer *)recogniser {
// Handle the tap here
}
Well, i have done the same thing like this:
Make a custom button with text: ESPNSoccernet, and background clearColor
Add a label as a subview with height 1 and some background color to this button, such that "ESPNSoccernet" looks underlined.
Put the remaining text in a label adjacent to this button, so that it looks like a whole text.
Hope it helps!
Note: if you r doing only >iOS 6.0, you might wanna check the other answers.
If this app for ios 6 or later version in that case you can use NSMutableAttributedString
NSMutableAttributedString *labelText = [[NSMutableAttributedString alloc] initWithString:#"Hello this is demmy label for testing"];
[labelText addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1]
range:(NSRange){10,10}];
label.attributedText = labelText;
for less version you can use like this..
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 116, 171, 20)];
label.text = #"Hello this is demmy label for testing";
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:16];
[self.view addSubview:label];
//underline code
CGSize expectedLabelSize = [[m_BCListArray objectAtIndex:tagcount] sizeWithFont:label.font constrainedToSize:label.frame.size lineBreakMode:UILineBreakModeWordWrap];
UIView *viewUnderline=[[UIView alloc] init];
viewUnderline.frame=CGRectMake((label.frame.size.width - expectedLabelSize.width)/2, expectedLabelSize.height + (label.frame.size.height - expectedLabelSize.height)/2, expectedLabelSize.width, 1);
viewUnderline.backgroundColor=[UIColor whiteColor];
[self.view addSubview:viewUnderline];
The following code snippet produces desired result :
NSDictionary *dictAttribute = #{NSForegroundColorAttributeName:[UIColor redColor],NSUnderlineStyleAttributeName:#1};
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:#"Please click ESPNSoccernet to read the latest Football News." attributes:dictAttribute];
lblText.attributedText = attrString;