WhatsApp中的换行与电子邮件有什么不同?

4
在IOS 9.2中分享应用程序文本时,可以选择各种消息选项。问题在于,大多数选项(如邮件或短信)希望\n成为换行符,而WhatsApp则要求<BR>成为换行符。
据我所知,在应用程序中无法知道用户将选择什么,因此我发送了\n<BR>。虽然对于忽略\n的WhatsApp工作得很好,但对于显示<BR>的邮件效果不佳。
还尝试了%0A%0D,但WhatsAPP会忽略它。

1
因此,请为每个应用程序使用正确的相应换行符代码。 - max_
问题在于,“邮件”有许多标准。电子邮件可以使用文本模式或HTML模式,两者彼此排斥。 - Mr Lister
1个回答

4

编辑

从2017年7月19日起,iOS版WhatsApp不再将<br>解释为换行符,而是改用\n

这并不是向后兼容的更改,因此如果您使用<br>,则会得到Some<br>Text。下面的代码不应再使用。好消息是您无需做任何事情:WhatsApp将按预期处理\n


已弃用

我使用了自己的UIActivityItemProvider,根据所选的活动使用\n<br>

@interface ShareManager : UIActivityItemProvider <UIActivityItemSource>
@end

@implementation ShareManager

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
    id result = [self getShareText];

    if ([activityType containsIgnoringCase:@"WhatsApp"]) // You can also match against the exact id "net.whatsapp.WhatsApp.ShareExtension"
    {
        result = [result stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
    }

    return result;
}

@end

使用方法:

UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:@[[[ShareManager alloc] init]]];
[self presentViewController:activity animated:YES completion:nil];

我这样设置:NSString *strRemove = [textToShare stringByReplacingOccurrencesOfString:@"<br/>" withString:@"\n"]; 但是没有设置换行符,如何设置换行符? - ramesh bhuja
@rameshbhuja 这是相反的情况:你要分享的文本需要使用 <br> 而不是 \n,所以你应该用 <br> 替换所有的 \nNSString *strRemove = [textToShare stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"]; - gabrielmaldi
我的代码是当我下载带有文本的图像时,然后通过generalPasteboard预定义文本副本。然后我想应用\n换行符... - ramesh bhuja

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接