gpt4 book ai didi

ios - 在 UITextView 中使文本超链接或可点击

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:53:00 31 4
gpt4 key购买 nike

我有 UITextView 并在其中显示来自三个标签(消息、titleUrl 和 url)的文本。我需要的是我想让“titleUrl”的文本可点击以在 WebView 中打开“url”的值。我设法直接从 url 打开链接,但我需要通过单击“titleUrl”来打开链接。我试图通过这段代码实现以下目标。

[self buildAgreeTextViewFromString:NSLocalizedString(@"I agree to the #<ts>terms of service# and #<pp>privacy policy#", 
@"PLEASE NOTE: please translate \"terms of service\" and \"privacy policy\" as well, and leave the #<ts># and #<pp># around your translations just as in the English version of this message.")];

但我不了解如何修改它以实现功能。我想输入字符串中的值,没有要输入的静态文本。谁能指导我处理这个问题?

更新:

NSString *message = [NSString stringWithFormat:@"%@\n ", tempStr1];
NSString *message1 = [NSString stringWithFormat:@"\n#<pp>%@#", titlStr1];
NSString *localizedString = NSLocalizedString(message1, nil);

NSRange ppRange = [localizedString rangeOfString:NSLocalizedString(message1, nil) options:NSCaseInsensitiveSearch];


NSURL *ppURL = [NSURL URLWithString:strUrl];



NSDictionary *attribute1 = @{NSForegroundColorAttributeName: [UIColor whiteColor],
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:15.0],
};
NSMutableAttributedString *newAttString = [[NSMutableAttributedString alloc] initWithString:message attributes:attribute1];

//

NSMutableAttributedString *finalMessage = [[NSMutableAttributedString alloc] initWithString:localizedString];
[finalMessage beginEditing];

[finalMessage addAttributes:attribute1 range:ppRange];
[finalMessage addAttribute:NSLinkAttributeName value:ppURL range:ppRange];
[finalMessage endEditing];

[newAttString appendAttributedString:finalMessage];

self.txtView.attributedText = newAttString;

最佳答案

这就像使用 NSMutableAttributedString 一样简单。注意:这不是唯一的方法,这可以通过搜索范围等来完成,这只是一个简单的实现,可以让您朝着正确的方向前进,因为您确实有静态消息,因为您的本地化所有这些,这意味着您拥有它的静态英语形式。

NSString *tosString = @"Terms of Service";
NSString *ppString = @"Privacy Policy";
NSString *message = [NSString stringWithFormat:@"I agree to the #<ts>%@# and #<pp>%@#", tosString, ppString];
NSString *localizedString = NSLocalizedString(message, nil);

NSRange tosRange = [localizedString rangeOfString:NSLocalizedString(tosString, nil) options:NSCaseInsensitiveSearch];
NSRange ppRange = [localizedString rangeOfString:NSLocalizedString(ppString, nil) options:NSCaseInsensitiveSearch];

NSURL *tosURL = [NSURL URLWithString:@"http://toslink.com"];
NSURL *ppURL = [NSURL URLWithString:@"http://pplink.com"];

NSMutableAttributedString *finalMessage = [[NSMutableAttributedString alloc] initWithString:localizedString];
[finalMessage beginEditing];
[finalMessage addAttribute:NSLinkAttributeName value:tosURL range:tosRange];
[finalMessage addAttribute:NSLinkAttributeName value:ppURL range:ppRange];
[finalMessage endEditing];

self.yourTextView.attributedText = finalMessage;

关于ios - 在 UITextView 中使文本超链接或可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32355563/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com