-1

I am trying fit an UIButton and an UILabel on the same line. If the UILabel is larger it will then wrap around the UIButton to the next line.

For example,

enter image description here

In this case, Kobe Bryant would be the UIButton which is clickable. and the rest of the text are UILabel.

The thing is that The UIButton is a variable and the width changes. I spent an entire day researching on google and stackoverflow and cannot come up with an answer.

If any of you can point me to the right direction, I'll be really appreciated

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
user3081516
  • 213
  • 2
  • 5
  • 14

3 Answers3

1

It is better to use a web view instead of UIButton and UILabel (disable the scrolling ). Load the text in the webview as ,

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];

Listen for the URL

- (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;

}

For performing the Segue action

  1. Remove the segue from UIButton
  2. ctrl+drag from the source UIViewController to destination UIViewController.
  3. select "push".
  4. Give an identifier to the segue.

And add the below line to go to the next view controller. (before return NO; in the above code)

[self performSegueWithIdentifier: @"segue_id" sender:self];

You can disable the scrolling of the web view as,

webView.scrollView.scrollEnabled = NO; 
webView.scrollView.bounces = NO;

And can calculate the dynamic height as,

CGSize maximumSize = CGSizeMake(320,568); //change as you wish

CGSize expectedLabelSize = [contentString sizeWithFont:fontUsed 
                        constrainedToSize: maximumSize 
                        lineBreakMode:NSLineBreakByCharWrapping]; 

Set the new height as,

[webview sizeThatFits: expectedLabelSize];

or

CGRect newFrame = webview.frame;
newFrame.size.height = expectedLabelSize.height;
webview.frame = newFrame;

Hope this may help you.

Neenu
  • 6,848
  • 2
  • 28
  • 54
  • Thanks for the answer. The UIButton suppose to opens up a segue to another view controller as oppose to another webview – user3081516 Nov 04 '14 at 06:17
  • It is working fine. But how do I disable the scrolling and use dynamic height. Because it may be 1, 2 or more lines depending on the length of the text – user3081516 Nov 04 '14 at 06:35
  • Hi Neenu, thanks alot. Everything is working except the dynamic height. I believe sizeWithFont is deprecated. – user3081516 Nov 04 '14 at 06:58
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];

In the delegate method you do this

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

After loading the text now fix the height of webview to the content

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

Disable scrolling

self.<WebView>.scrollView.scrollEnabled=FALSE;
Ramesh Muthe
  • 811
  • 7
  • 15
1

You can UITextView with attributed String setting to do this trick for you. Check out this link for how to do this.

Another Approach :

If you want to add a weblink to textview you can use following code snippet.

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

To detect if a link is tapped.

Combining the ideas above you can set a custom URL say http://linktapped. When you tap this you would get a call in App delegate. You can check if it your URL and do appropriate action needed(Instead of opening safari).

Community
  • 1
  • 1
Vignesh
  • 10,205
  • 2
  • 35
  • 73