开发者

iPhone:Add custom button frame in search string

Hello friends,

I want to add a custom button on my searching string like iPhone text.

Is there any way to develop this functionality or any idea?

Here is my code.

     - (void)viewDidLoad {
        [super viewDidLoad];

        self.title = @"Label Navigation Demo";

        [self dynamiclabel];
    }


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

        NSString *labe开发者_如何学JAVAlText = @"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.";
        NSLog(@"Label Text:--->%@",labelText);

        [dlabel getSize:labelText 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 findAllLocationsOfString:@"iPhone" sourceString:labelText];

        [self.view addSubview:dlabel];

        //Search String in Label Text
        //NSString *str = @"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.";
        //NSString *str = [NSString stringWithFormat:@"%@",labelText];
        NSInteger count = 0;
        NSArray *arr = [labelText componentsSeparatedByString:@" "];
        for(int i=0;i<[arr count];i++)
        {

            if([[arr objectAtIndex:i] isEqualToString:@"iPhone"])
            {   
                NSLog(@"%@ %d",[arr objectAtIndex:i],i);
                        CGRect buttonFrame = CGRectMake(i, 

                                             dlabel.frame.size.height + i, 

                                             dlabel.frame.size.height + i,

                                             i);

            //CGRect buttonFrame = dlabel.frame;
                       //buttonFrame.size.height = dlabel.frame.size.height-i;
                      //dlabel.frame = buttonFrame;

            NSLog(@"Button Frame:--->%@",NSStringFromCGRect(buttonFrame));
            [self buttonWithTitle:@"Hello" target:self frame:buttonFrame];
            }
            count++;
            NSLog(@"Count:-->%d",count);

        } 

    }

    - (UIButton *)buttonWithTitle:(NSString *)title target:(id)target frame:(CGRect)frame {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = frame;
    NSLog(@"Button Frame in Function:--->%@",NSStringFromCGRect(button.frame));
    [button setTitle:title forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
    [button addTarget:target action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundColor:[UIColor clearColor]];   // in case the parent view draws with a custom color or gradient, use a transparent color
    [self.view addSubview:button];

    return button;
}

Thanks in advance.


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 here or 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.


Am I correct to assume that you want to put a button on top of the label so the user is able to tap on the word "iPhone"? If so, you should think about using a web view instead, because it's rather complicated to calculate the exact position. Even if it's possible and reliable to determine the horizontal position of the individual occurrences of the word "iPhone" it becomes very hard if your label is using multiple lines.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜