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;
Related
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
NSImage *image = [NSImage imageNamed:#"emotion"];;
attachment.image = image;
NSAttributedString *attributedString = [NSAttributedString attributedStringWithAttachment: attachment];
[[_feedbackContent textStorage] appendAttributedString:attributedString];
after add the image to NSTextAttachment, it is vertical flipped. Anybody know how to resolve this issue.
NSTextAttachment seems to use NSFileWrapper filename to get the UTI and has different behaviour based on the UTI.
I was able to fix it with using NSFileWrapper instead:
NSFileWrapper *fileWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:data];
// Without a filename (which is used to get the UTI by NSTextAttachment)
// the image is displayed flipped.
[fileWrapper setPreferredFilename:#"Attachment.png"];
NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithFileWrapper:fileWrapper];
You could also try to set the fileType property to kUTTypePNG or other image type to get it working.
radar://47170950
Assign the image to an NSTextAttachmentCell, not the NSTextAttachment.
id <NSTextAttachmentCell> cell = [[NSTextAttachmentCell alloc] initImageCell:image];
NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
[attachment setAttachmentCell:cell];
When drawing into a flipped view (which is why the image gets flipped), the only workaround I've found is to create a flipped image so it's then drawn the :
textAttachment.image = [NSImage imageWithSize:image.size flipped:YES drawingHandler:^BOOL(NSRect dstRect) {
[image drawInRect:dstRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
return YES;
}];
How to display html content in label.
Bellow link is how my label display html content.
https://i.stack.imgur.com/j1TKP.png
I also try using this code.
NSString * htmlString = #"<html><body> Some html string </body></html>";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:#{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}documentAttributes:nil error:nil];
UILabel * myLabel = [[UILabel alloc] init];
myLabel.attributedText = attrStr;
I tried your html string which was in image.I got the solution
ViewController.m
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString * htmlString = #"Description: The popular Mini Pashili holds your daily essentials. With signature side zip-pleats,this style is often worn fanned-open and cross body via detachable shoulder strap.Just pinch to open the push-lock padlock.<ul><li>100%Full Grain Leather</li><li>6.5”H x 8.5”W x 3”D</li><li>16.5”-22” Adjustable Shoulder Starp</li><li>Fully Lined</li><li>Dust Bag included</li><li>Imported</li><li>AE17-0226SKC</li></ul>";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:#{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
UILabel * myLabel = [[UILabel alloc] init];
myLabel.frame = CGRectMake(200, 200, 350, 300);
myLabel.numberOfLines = 4;
myLabel.attributedText = attrStr;
[self.view addSubview:myLabel];
}
Now the solution is
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 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
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.