【问题标题】:IOS Multiple line Label floating around a UIButtonIOS多行标签浮动在UIButton周围
【发布时间】:2014-12-30 23:02:13
【问题描述】:

我正在尝试将UIButton 和UILabel 放在同一行。如果UILabel 更大,它将环绕UIButton 到下一行。

例如,

在这种情况下,Kobe Bryant 将是UIButton,这是可点击。其余文字为UILabel。

问题是UIButton 是一个变量,宽度会发生变化。我花了一整天时间研究 google 和 stackoverflow 却找不到答案。

如果有人能指出正确的方向,我将不胜感激

【问题讨论】:

  • 看看这个答案,您可以将其更改为使用按钮的框架:stackoverflow.com/a/20033752/2043580
  • 您应该使用不可编辑的 TextView,因为 UILabel 不支持排除路径
  • 如何计算只有在运行时才知道的 UIButton 的宽度
  • 运行时可以使用button.frame来获取frame。
  • 你将文本保留在 webview 中,并将“kobe bryant”作为链接。我遇到了同样的问题,我花了和你一样的时间

标签: ios objective-c uilabel core-text


【解决方案1】:
NSString    *link = @"<a href=\"movetonextscreen\" style=\"text-decoration:none; color : #000000;\"><b>Kobe Bryant</b></a>";
NSString    *html = [NSString stringWithFormat:@"%@ your text", link];
UIWebView   *webView = [[UIWebView alloc] initWithFrame:cell.contentView.bounds];
webView.delegate = self;
[webView loadHTMLString:html baseURL:nil];

在委托方法中你这样做

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeLinkClicked)
    {
}
}

加载文本后,现在将 webview 的高度固定为内容

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    CGRect newBounds = <webView>.bounds;
    newBounds.size.height = <webView>.scrollView.contentSize.height;
    <webView>.bounds = newBounds;
}

禁用滚动

self.<WebView>.scrollView.scrollEnabled=FALSE;

【讨论】:

  • 当我将 webView 设置为 3 行时,单击 Button 后它缩小为 2 行
【解决方案2】:

您可以使用带有属性字符串设置的 UITextView 来为您完成此技巧。查看此link 了解如何执行此操作。

另一种方法:

如果你想给 textview 添加一个网络链接,你可以使用下面的代码 sn-p。

NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:@"Test"];
[str addAttribute: NSLinkAttributeName value: @"http://your link" range: NSMakeRange(0, str.length)];
yourTextView.attributedText = str;

检测link是否被点击。

结合上述想法,您可以设置一个自定义 URL,例如 http://linktapped。当您点击此按钮时,您会在 App 委托中接到电话。您可以检查它是否是您的 URL 并执行所需的适当操作(而不是打开 safari)。

【讨论】:

  • Opps 错过了。现已编辑。
【解决方案3】:

最好使用 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 操作

  1. 从 UIButton 中移除 segue
  2. ctrl+从源 UIViewController 拖动到目标 UIViewController。
  3. 选择“推送”。
  4. 给 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;

希望对您有所帮助。

【讨论】:

  • 感谢您的回答。 UIButton 假设打开一个到另一个视图控制器的 segue,而不是另一个 webview
  • 它工作正常。但是如何禁用滚动并使用动态高度。因为它可能是 1 行、2 行或更多行,具体取决于文本的长度
  • 嗨 Neenu,非常感谢。除了动态高度之外,一切都在工作。我相信 sizeWithFont 已被弃用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-25
  • 2014-12-08
  • 1970-01-01
  • 2013-10-30
  • 1970-01-01
  • 1970-01-01
  • 2023-02-21
相关资源
最近更新 更多