SO 上有很多答案向开发人员展示如何从 HTML 内容制作字符串或将 URL 放入字符串中,但我的问题是如何制作 HTML 字符串。
我正在尝试创建一个将以 HTML 格式返回或至少不显示 URL 的字符串。
因此,例如网络开发人员会这样做来隐藏 URL:
<a href="http://google.com">Visit Us at Google.com!</a>
我可以通过这样做轻松地将其转换为字符串:
NSString *urlLink = @"www.google.com";
NSString *string = [NSString stringWithFormat:@"Visit Us at %@", urlLink];
但这不会用我选择的超链接词替换链接。
我知道设备会根据您的显示方式决定它是否是超链接。即,文本字段、 TextView ,或者您可以强制打开它等。
我想做的是:
#define APPSTORELINK @"www.appstorelink.com"
@implementation Config
+(NSString *)appstorelink {
return APPSTORELINK;
}
+(NSString *)mmsmetadata {
NSString *string = [NSString stringWithFormat:@"I shared this publication with the <a href=\"http://%@\">[Name of my iPhone App] iPhone App</a>", APPSTORELINK];
return string;
}
所以我可以在这里或在应用程序范围内轻松地调用它:
NSArray *shareItems;
UIImage *snapshot = [self imageFromView:self.view];
shareItems = @[[Config mmsmetadata], snapshot];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:shareItems applicationActivities:nil];
activityController.excludedActivityTypes = @[UIActivityTypePostToFlickr, UIActivityTypeAssignToContact, UIActivityTypeMail, UIActivityTypePostToVimeo];
[activityController setCompletionWithItemsHandler:(UIActivityViewControllerCompletionWithItemsHandler)^(NSString *string, BOOL completed) {
简而言之,我怎样才能使字符串 HTML 格式开箱即用?我主要担心的是我想隐藏 URL 并将其替换为 HTML 标记,否则如果您有更好的解决方案。在 SO 上找不到任何内容。
有什么想法吗?我可能想得太多了。我相信有更简单的方法
编辑
甚至在发布之前我就已经知道 NSAttributedString,这是我尝试的第一件事。然而,问题不在于设置属性,那是简单的部分,定义我的问题的部分是如何设置它以便在 UIActivityViewController 中使用事件时显示作为属性
我是这样设置的,但结果和上面一样,所以我想使用 HTML 标签会更容易:
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"I shared this publication with the Army Pubs iPhone App!"];
NSRange selectedRange = NSMakeRange(0, [string length]);
NSURL *linkURL = [NSURL URLWithString:APPSTORELINK];
[string beginEditing];
[string addAttribute:NSLinkAttributeName
value:linkURL
range:selectedRange];
[string addAttribute:NSForegroundColorAttributeName
value:[UIColor blueColor]
range:selectedRange];
[string addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:NSUnderlineStyleSingle]
range:selectedRange];
[string endEditing];
return string;
但是,它仍然在消息或邮件编辑器中显示为纯文本。所以想想 MFMailComposeViewControllerDelegate 怎么会有 isHTML 的设置。如果设置为 yes,它会去除所有 HTML 标签并将文本显示为超链接。例如:
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:YES];
我想模拟从 UIActivityViewController 中调用事件时的情况
这是当前的输出,即使我使用我首先尝试的属性字符串进行输出,它也只是通过剥离 HTML 标记显示为纯文本,但不会使其成为链接
最佳答案
请参阅 attributed strings 中的链接选项.
The link attribute specifies an arbitrary object that is passed to the NSTextView method clickedOnLink:atIndex: when the user clicks in the text range associated with the NSLinkAttributeName attribute. The text view’s delegate object can implement textView:clickedOnLink:atIndex: or textView:clickedOnLink: to process the link object. Otherwise, the default implementation checks whether the link object is an NSURL object and, if so, opens it in the URL’s default application.
关于html - 为 UIActivityViewController 事件填充超链接属性字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27665006/