如何使 UILabel 文本中的两个或多个字符串指向不同的链接
How to make two or more strings in UILabel text point to different links
我有一个显示字符串的 UILabel。我需要更改 UILabel 中特定文本的颜色,当单击这些文本时,它应该在 web 视图中打开两个不同的链接。如何实现:
我编写的以下代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is Yahoo and Google" attributes:nil];
[attributedString addAttribute: NSForegroundColorAttributeName
value: [UIColor redColor]
range: NSMakeRange(8,5)];
[attributedString addAttribute: NSFontAttributeName
value: [UIFont fontWithName:@"Helvetica" size:15]
range: NSMakeRange(8,5)];
[attributedString addAttribute: NSFontAttributeName
value: [UIFont fontWithName:@"Didot" size:24]
range: NSMakeRange(18,6)];
self.linkLabel.attributedText = attributedString;
}
现在我希望当用户点击 google 时它应该打开 google.com 并且当用户点击 Yahoo 时它应该打开 yahoo.com。我怎么可能?
尝试更改为 UITextView,它应该使用以下内容
let verbiage = links.text! // UITextView text
let attributes = NSMutableAttributedString(string: verbiage)
let googleRange = (verbiage as NSString).range(of: "Google")
let yahooRange = (verbiage as NSString).range(of: "Yahoo")
attributes.addAttribute(NSLinkAttributeName, value: "https://www.google.com", range: googleRange)
attributes.addAttribute(NSLinkAttributeName, value: "https://www.yahoo.com", range: yahooRange)
let linkAttributes: [String : Any] = [
NSForegroundColorAttributeName: UIColor.red,
NSUnderlineColorAttributeName: UIColor.clear,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleNone.rawValue]
links.linkTextAttributes = linkAttributes
links.attributedText = attributes
不要忘记将 Scrolling Enabled 设置为 false 并将 Editable 设置为 false 以获得类似于 UILabel 的 UITextView
在分配 attributedString
之前使用下行将 link 添加到您的文本
[attributedString addAttribute:NSLinkAttributeName value:[NSUrl urlwithString@"https://www.google.com"] range:NSMakeRange(18,6)];
同样,您也可以对其他字符串执行此操作。
我有一个显示字符串的 UILabel。我需要更改 UILabel 中特定文本的颜色,当单击这些文本时,它应该在 web 视图中打开两个不同的链接。如何实现: 我编写的以下代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is Yahoo and Google" attributes:nil];
[attributedString addAttribute: NSForegroundColorAttributeName
value: [UIColor redColor]
range: NSMakeRange(8,5)];
[attributedString addAttribute: NSFontAttributeName
value: [UIFont fontWithName:@"Helvetica" size:15]
range: NSMakeRange(8,5)];
[attributedString addAttribute: NSFontAttributeName
value: [UIFont fontWithName:@"Didot" size:24]
range: NSMakeRange(18,6)];
self.linkLabel.attributedText = attributedString;
}
现在我希望当用户点击 google 时它应该打开 google.com 并且当用户点击 Yahoo 时它应该打开 yahoo.com。我怎么可能?
尝试更改为 UITextView,它应该使用以下内容
let verbiage = links.text! // UITextView text
let attributes = NSMutableAttributedString(string: verbiage)
let googleRange = (verbiage as NSString).range(of: "Google")
let yahooRange = (verbiage as NSString).range(of: "Yahoo")
attributes.addAttribute(NSLinkAttributeName, value: "https://www.google.com", range: googleRange)
attributes.addAttribute(NSLinkAttributeName, value: "https://www.yahoo.com", range: yahooRange)
let linkAttributes: [String : Any] = [
NSForegroundColorAttributeName: UIColor.red,
NSUnderlineColorAttributeName: UIColor.clear,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleNone.rawValue]
links.linkTextAttributes = linkAttributes
links.attributedText = attributes
不要忘记将 Scrolling Enabled 设置为 false 并将 Editable 设置为 false 以获得类似于 UILabel 的 UITextView
在分配 attributedString
之前使用下行将 link 添加到您的文本 [attributedString addAttribute:NSLinkAttributeName value:[NSUrl urlwithString@"https://www.google.com"] range:NSMakeRange(18,6)];
同样,您也可以对其他字符串执行此操作。