如何在ios中的特定textView文本上创建超链接

时间:2017-05-11 05:57:43

标签: ios objective-c

NSString *str = @"We’ll notify you before we make changes to these 
terms and give you the opportunity to review and comment on the revised 
terms before continuing to use. If you have any question just email 
me.";
NSMutableAttributedString *aStr = [[NSMutableAttributedString 
alloc]initWithString:str attributes:nil];
[str rangeOfString:@"email"]];
[self.textView setAttributedText:aStr];

这是我希望在电子邮件上创建链接的文字。

1 个答案:

答案 0 :(得分:2)

你可以做点什么,

NSString *str = @"We’ll notify you before we make changes to these terms and give you the opportunity to review and comment on the revised terms before continuing to use. If you have any question just email me.";
NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc]initWithString:str attributes:nil];
[aStr addAttribute:NSLinkAttributeName value:@"Your Link here" range:[str rangeOfString:@"email"]];
[self.textView setAttributedText:aStr];

<强>更新

您可以添加字体属性以使其变为粗体。例如,如果您想使We’ll notify加粗,那么

  [aStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14.0] range:[str rangeOfString:@"We’ll notify"]];

在将属性文字设置为textView之前添加此行!