【问题标题】:Add hyperlinks to NSString iOS将超链接添加到 NSString iOS
【发布时间】:2015-10-14 18:32:08
【问题描述】:

我有一个简单的 NSString,例如:

NSString *text = @"Stackoverflow 太棒了!"

我想把 Stackoverflow 这个词变成一个指向https://stackoverflow.com/ 的超链接,并且仍然能够将字符串作为变量输出。这可能吗?

【问题讨论】:

标签: objective-c hyperlink nsstring


【解决方案1】:

字符串只包含字母 - 没有任何格式。

要将一个部分变成一个链接,你必须给第一个词分配一个带有 url 的 NSLink 属性:

NSURL *url = [NSURL URLWithString:@"http://www.google.de"];

NSAttributedString *my = [NSAttributedString attributedStringWithString:@"my"
                                                             attributes:@{NSForegroundColorAttributeName:[UIColor blackColor], NSFontAttributeName:[UIFont systemFontWithSize:16]}];
NSAttributedString *link = [NSAttributedString attributedStringWithString:@"Link"
                                                               attributes:@{NSForegroundColorAttributeName:[UIColor blue], NSFontAttributeName:[UIFont systemFontWithSize:16], NSLinkAttributeName:url}];
NSMutableAttributedString *attr = [my mutableCopy];
[attr appendAttributedString:link];

在文本视图中显示,UILabel 不支持点击 AFAIK

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 2017-10-12
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多