I worked UILabel. But setLineBreakMode is deprecated.
I have been using NSAttributedString.
but UILabel setLineBreakMode is
After that UILabel setNumberOfLines else does not work.
Before:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(42.0f, 10.0f, 275.0f, 50.0f)];
label.text = #"XXXXXX";
memoLabel.textAlignment = UITextAlignmentLeft;
memoLabel.numberOfLines = 2;
memoLabel.lineBreakMode = UILineBreakModeTailTruncation;
memoLabel.font = [UIFont systemFontOfSize:11];
memoLabel.backgroundColor = [UIColor clearColor];
IOS 6 after:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
NSAttributedString *string
= [[NSAttributedString alloc] initWithString:text
attributes:[NSDictionary
dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:11],
NSFontAttributeName,
paragraphStyle, NSParagraphStyleAttributeName,nil]];
[paragraphStyle release];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(42.0f, 10.0f, 275.0f, 50.0f)];
label.attributedText = string;
[string relase];
I want to be the same before and after the display.
How to display multiple lines?
The lineBreakMode property isn't deprecated in iOS 6. It has simply changed the names of the constants. The old constants are deprecated, but still available. You can use the new constants even if you are deploying to an older iOS, because the constants are just enum values. The old names and the new names have the same values. So, just set memoLabel.lineBreakMode = NSLineBreakByTruncatingTail.
Your example code doesn't appear to take advantage of any attributed string specific features. If you don't need an attributed string, just keep using a plain string. That still works in iOS 6.
Use NSLineBreakByTruncatingTail instead of UILineBreakModeTailTruncation
Related
I've a label in a table cell and I wish to add padding to top,bottom,left and right.
CGRect initialFrame = CGRectMake(10,10,100,20);
UIEdgeInsets contentInsets = UIEdgeInsetsMake(5, 0, 5, 0);
CGRect padd = UIEdgeInsetsInsetRect(initialFrame, contentInsets);
self.rewardLabel = [[UILabel alloc] initWithFrame:padd];
self.rewardLabel.backgroundColor =[UIColor colorWithRed:0.192 green:0.373 blue:0.561 alpha:1];
self.rewardLabel.layer.cornerRadius = 5.0f;
self.rewardLabel.layer.masksToBounds = YES;
self.rewardLabel.textColor = [UIColor whiteColor];
self.rewardLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.rewardLabel.numberOfLines = 1;
self.rewardLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
[self.contentView addSubview:self.rewardLabel];
But it seem like not working. Can anyone tell me how to do?
There are several ways on how to achieve this:
If you do not need a specific background color for your label you could just adjust the labels frame to add the padding (e.g. if your text should start 20px from the cell's left side, set the label's frame's x to 20).
To really add a padding, you could use a custom UILabel subclass and override its drawTextInRect: and intrinsicContentSize methods. (See this question for details)
If you just need a left and right padding you could use an NSAttributedString to add insets to UILabel:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.headIndent = 5.0;
paragraphStyle.firstLineHeadIndent = 5.0;
paragraphStyle.tailIndent = -5.0;
NSDictionary *attrsDictionary = #{NSParagraphStyleAttributeName: paragraphStyle};
label.attributedText = [[NSAttributedString alloc] initWithString:#"Your text" attributes:attrsDictionary];
In my project i have a requirement to show a UIButton that has two lines of text in its titleLabel With NSAttributedString. Its working fine in iOS8 but Where as in iOS7 its not working,i can see two lined text after selected button.
This is the code I'm using:
calendarBtn= [UIButton buttonWithType:UIButtonTypeCustom];
calendarBtn.frame=CGRectMake(50 ,10, 45, 45);
[calendarBtn addTarget:self action:#selector(calendarBtnclicked:)forControlEvents:UIControlEventTouchUpInside];
calendarBtn.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
calendarBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
calendarBtn.titleLabel.numberOfLines=2;
[calendarBtn setTitleColor:BLACK_COLOR forState:UIControlStateNormal];
NSString *dateString=#"27\nApr";
dateString=[dateString uppercaseString];
[calendarBtn setTitle:dateString forState:UIControlStateNormal];
// Font For 27
UIFont *ddFont = [UIFont fontWithName:ArkitechLight size:17.0f];
NSMutableDictionary *ddDict = [NSMutableDictionary dictionaryWithObject:ddFont forKey:NSFontAttributeName];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:3];
style.alignment=NSTextAlignmentCenter;
[style setLineBreakMode:NSLineBreakByWordWrapping];
[ddDict addEntriesFromDictionary:#{NSParagraphStyleAttributeName : style,}];
NSMutableAttributedString *ddStr = [[NSMutableAttributedString alloc] initWithString:[dateString substringToIndex:2] attributes:ddDict];
// Font For Apr
UIFont *mmmFont = [UIFont fontWithName:ArkitechLight size:11.5f];
NSDictionary *mmmDict = [NSDictionary dictionaryWithObject:mmmFont forKey:NSFontAttributeName];
NSMutableAttributedString *mmmString = [[NSMutableAttributedString alloc]initWithString:[dateString substringFromIndex:2] attributes:mmmDict];
[ddStr appendAttributedString:mmmString];
calendarBtn.titleLabel.attributedText=ddStr;
You need to use this line to set attributed title
[calendarBtn setAttributedTitle: ddStr forState:UIControlStateNormal];
instead of
calendarBtn.titleLabel.attributedText=ddStr;
Hope it helps :)
I'm setting up a UITextView, and it has links that you can tap that will open in Safari.
UITextView *tv = [[UITextView alloc] initWithFrame:CGRectMake(35, 100, 300, 400)];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[#"<a href='http://google.ca'>test</a>" dataUsingEncoding:NSUTF8StringEncoding] options:#{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: #(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
tv.attributedText = attributedString;
[self.view addSubview:tv];
It basically takes an HTML string in (with an <a href=... in it) and then using the HTML parsing methods in iOS 7 gives me my tappable UITextView links.
However, if I add the following line before I add the text view to my view:
tv.linkTextAttributes = #{
NSUnderlineStyleAttributeName: #(NSUnderlineStyleNone)
};
Nothing changes. I can set it to be NSUnderlineStyleThick and it gets thicker. I can change the NSForegroundColorAttributeName to [UIColor redColor] to make the text red, but I cannot figure out how to get the text to not have an underline.
What am I doing wrong?
You can use [NSMutableAttributedString alloc] initWithString:str instead of NSAttributedString alloc] initWithData:data. Links will automatic detected and not underline
Try to use code below:
NSString *str = #"http://google.ca";
NSAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:str];
tv.attributedText = attributedString;
I've been searching around to find an easy way to add shadows to the text of a UITextView, like you can do in a UILabel. I found this question where there was an answer that supposedly does this, however, it makes no sense why this should be the case.
Question: Adding shadows to the layer of the UITextView itself should not affect the text inside, rather it should shadow the entire object, right?
In my case, even adding the shadow to the layer of the textview is not having any effect (even after adding the QuartzCore headers).
i tried, and found that , you should set the UITextView's backgroundcolor to transparent,
so the shadow should work
UITextView *text = [[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 150, 100)] autorelease];
text.layer.shadowColor = [[UIColor whiteColor] CGColor];
text.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
text.layer.shadowOpacity = 1.0f;
text.layer.shadowRadius = 1.0f;
text.textColor = [UIColor blackColor];
//here is important!!!!
text.backgroundColor = [UIColor clearColor];
text.text = #"test\nok!";
text.font = [UIFont systemFontOfSize:50];
[self.view addSubview:text];
#adali's answer will work, but its wrong. You shouldn't add the shadow to the UITextView itself in order to effect the visible views inside. As you can see, by applying the shadow to the UITextView the cursor will also have the shadow.
The approach that should be used is with NSAttributedString.
NSMutableAttributedString* attString = [[NSMutableAttributedString alloc] initWithString:textView.text];
NSRange range = NSMakeRange(0, [attString length]);
[attString addAttribute:NSFontAttributeName value:textView.font range:range];
[attString addAttribute:NSForegroundColorAttributeName value:textView.textColor range:range];
NSShadow* shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor whiteColor];
shadow.shadowOffset = CGSizeMake(0.0f, 1.0f);
[attString addAttribute:NSShadowAttributeName value:shadow range:range];
textView.attributedText = attString;
However textView.attributedText is for iOS6. If you must support lower versions, you could use the following approach.
CALayer *textLayer = (CALayer *)[textView.layer.sublayers objectAtIndex:0];
textLayer.shadowColor = [UIColor whiteColor].CGColor;
textLayer.shadowOffset = CGSizeMake(0.0f, 1.0f);
textLayer.shadowOpacity = 1.0f;
textLayer.shadowRadius = 0.0f;
Can glow effects be applied on text inside UITextView? If so, how?
Here is some code to add a red glow to a UITextView. Requires iOS 6+. Adjust parameters to taste:
NSShadow *shadow = [NSShadow new];
shadow.shadowBlurRadius = 5;
shadow.shadowColor = [UIColor redColor];
shadow.shadowOffset = CGSizeMake(0, 0);
NSDictionary *attributes = #{NSShadowAttributeName:shadow};
textView.attributedText = [[NSAttributedString alloc]
initWithString:title
attributes:attributes];
Not really. You might be able to use a CSS-styled (using the text-shadow property) <textarea> in a UIWebView, though.