Nikunj Jadav
Nikunj Jadav

Reputation: 3402

iPhone:How to add custom button on Dynamic Label in particular text?

Hello friends,

This is below my code.

         @interface DynamicLabel : UILabel

      {

      }

         - (id) getSize:(NSString *)text FontName:(NSString *)f_name FontSize:(float)f_size label:  (UILabel *)templbl;

     @end


      #import "DynamicLabel.h"

      #define DEFAULT_COLOR [UIColor blackColor] 

      @implementation DynamicLabel


      - (id)initWithFrame:(CGRect)frame 
     {
        if ((self = [super initWithFrame:frame])) 
     {

         }
         return self;
    }

    //function for dynamically get size of label 
     - (id) getSize:(NSString *)text FontName:(NSString *)f_name FontSize:(float)f_size label:(UILabel *)templbl
     {
         templbl.numberOfLines = 0;
         [templbl setFont:[UIFont fontWithName:f_name size:f_size]];
         [templbl setLineBreakMode:UILineBreakModeWordWrap];

         CGSize maximumLabelSize = CGSizeMake(310,9999);

         CGSize expectedLabelSize = [text sizeWithFont:templbl.font
                                constrainedToSize:maximumLabelSize 
                                                 lineBreakMode:templbl.lineBreakMode];

         CGRect newFrame = templbl.frame;
         NSLog(@"New Frame:--->%@",NSStringFromCGRect(newFrame));
         newFrame.size.height = expectedLabelSize.height;
         NSLog(@"New Frame Height:--->%f",newFrame.size.height);
         templbl.frame = newFrame;
         NSLog(@"Temp Label Frame:--->%@",NSStringFromCGRect(templbl.frame));
         [templbl setText:text];
         NSLog(@"Temp Label Text:---%@",templbl.text);
         [templbl sizeToFit];

         return templbl;
       }

I used above code in RootViewController and success dynamically get height of label

      #import "DynamicLabel.h"

      @interface RootViewController : UIViewController 
     {
       DynamicLabel *dlabel;    
     }

     @property (nonatomic,retain) DynamicLabel *dlabel;

     -(void)dynamiclabel;

     @end

        #import "RootViewController.h"
        #define LABELS_FONT_NAME_BOLD @"Helvetica-Bold"

        #define FONT_GREEN_COLOR [UIColor colorWithRed:0.122f green:0.416f blue:0.20f alpha:1.0f]

        @implementation RootViewController
        @synthesize dlabel;

        #pragma mark -

        #pragma mark View lifecycle


        - (void)viewDidLoad {
           [super viewDidLoad];
       self.title = @"Label Navigation Demo";
        }

        -(void)dynamiclabel
       {
         dlabel = [[DynamicLabel alloc] initWithFrame:CGRectMake(30, 130, 300, 20)];

        [dlabel getSize:@"Choose a topic from the left to find answers for your questions about iPhone. For the latest downloads, manuals, and other resources for iPhone, choose an option below."  
FontName:LABELS_FONT_NAME_BOLD FontSize:12.5f label:dlabel];
        dlabel.textColor = FONT_GREEN_COLOR;
        dlabel.backgroundColor = [UIColor clearColor];
        NSLog(@"Dynamic Label Height:--->%f",dlabel.frame.size.height);
            [self.view addSubview:dlabel];
        }

My issue is that I want to add custom button dynamically wherever iPhone text is displayed in label text. Is there any way to develop this functionality?

Thanks in advance.

Upvotes: 1

Views: 969

Answers (4)

Jano
Jano

Reputation: 63667

You can create custom sized buttons for an arbitrary paragraph of text using stretchable images and sizeWithFont:constrainedToSize:, but a design that needs explanation is not a good design, and a paragraph inside a button is a good example of that.

enter image description here

Upvotes: 3

Hardik Gajjar
Hardik Gajjar

Reputation: 5058

To make just one word of a UILabel clickable as a hyperlink you would need to know exactly where the UILabel puts that word. That is difficult (not impossible) especially with a multiple line label and especially if you allow resizing of the font to make the text fit. Why not use a UIWebView and make it a hyperlink instead? Or separate the text that is clickable and put that text into the UIButton by itself, like:

label.text = @"Demo to set display view on label in ";
button.titleLabel.text = @"iPhone";

Upvotes: 1

Nikunj Jadav
Nikunj Jadav

Reputation: 3402

If you want to create a "link" on some custom text in your label, instead of using a WebView as @Fabian Kreiser suggested, you sould use my OHAttributedLabel class (you can find it on Github hereor search the term on Stackoverflow, it is mentionned by other posts too), which is able to display NSAttributedString and add hyperlinks to it.

See the sample code provided on my github repository: you can use my addCustomLink:inRange: method to add a link (with a customized URL) to a range of text (range that you could determine by iterating over every occurrences of the word "iPhone" in your text very easily). Then in the delegate method on OHAttributedLabel, you can catch when the link is tapped and act accordingly to do whatever you need.

Upvotes: 1

mayuur
mayuur

Reputation: 4746

Whenever you add your dynamic label, also add a UIButton to the same frame.

When you do this,

    dlabel = [[DynamicLabel alloc] initWithFrame:CGRectMake(30, 130, 300, 20)];

Also, do this after adding the label,

    UIButton* yourBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 130, 300, 20)];

And keep it UIButtonTypeCustom and add it to the view.

Upvotes: 0

Related Questions