使用NSLinkAttributeName属性从NSMutableAttributedString获取。
使用html文件作为链接名称。如何增大文本区域。
-(NSMutableAttributedString *)attributedStringForLink:(NSString *)fullString
attributedString :(NSMutableAttributedString *)attributedString
link:(NSString *)linkString
path:(NSString *)pathString
font:(UIFont *)textFont
color:(UIColor *)textColor
alignment:(NSTextAlignment)textAlign
{
NSMutableAttributedString *attString;
if(attributedString) {
attString = attributedString;
} else {
attString = [[NSMutableAttributedString alloc]initWithString:fullString];
}
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
[paragraphStyle setAlignment:textAlign];
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
NSDictionary *attributes = @{NSParagraphStyleAttributeName:paragraphStyle,
NSFontAttributeName:textFont,
NSForegroundColorAttributeName:textColor};
NSRange range = [fullString rangeOfString:linkString];
[attString addAttributes:attributes range:range];
[attString addAttribute:NSLinkAttributeName value:pathString range:range];
[attString addAttribute:NSForegroundColorAttributeName value:textColor range:range];
return attString;
}
很抱歉,问题描述真的很糟糕。使用带有链接的NSAttributedString,它产生的结果很小。原来,这是我自己的带有小型UIWebView的视图控制器。创建一个新的UIViewController,其中包含一个包含UIWebView的UITextView。添加以下委托方法:
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL
inRange:(NSRange)characterRange
interaction:(UITextItemInteraction)interaction
{
if([[URL absoluteString] isEqualToString:@""]) {
return NO;
}
WebViewController *viewController = [[WebViewController alloc]init];
viewController.pathString = [URL absoluteString];
[self presentViewController:viewController animated:YES completion:nil];
return NO;
}