最好使用 web 视图而不是 UIButton 和 UILabel(禁用滚动)。
将 webview 中的文本加载为 ,
webview.delegate=self;
[webview loadHTMLString:@"<html><head></head><body><a href=\"name_identifier\"><b>Kobe Bryant</b></a> is a very good basketball player who is coming off an serious injury.</body></html>" baseURL:nil];
监听网址
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)aRequest navigationType:(UIWebViewNavigationType)navigationType{
if([[[aRequest URL] absoluteString] isEqual:@"name_identifier"]){
//TODO:Do the steps to call when press on the name
return NO;
}
return YES;
}
用于执行 Segue 操作
- 从 UIButton 中移除 segue
- ctrl+从源 UIViewController 拖动到目标 UIViewController。
- 选择“推送”。
- 给 segue 一个标识符。
并添加以下行以转到下一个视图控制器。
(在上述代码中return NO;之前)
[self performSegueWithIdentifier: @"segue_id" sender:self];
您可以禁用网页视图的滚动,
webView.scrollView.scrollEnabled = NO;
webView.scrollView.bounces = NO;
并且可以将动态高度计算为,
CGSize maximumSize = CGSizeMake(320,568); //change as you wish
CGSize expectedLabelSize = [contentString sizeWithFont:fontUsed
constrainedToSize: maximumSize
lineBreakMode:NSLineBreakByCharWrapping];
将新高度设置为,
[webview sizeThatFits: expectedLabelSize];
或
CGRect newFrame = webview.frame;
newFrame.size.height = expectedLabelSize.height;
webview.frame = newFrame;
希望对您有所帮助。