iPhone:在搜索字符串中添加自定义按钮框

时间:2011-09-01 06:37:07

标签: objective-c ios

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 *labelText = @"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.

2 个答案:

答案 0 :(得分:1)

如果您想在标签中的某些自定义文字上创建“链接”,而不是使用@Fabian Kreiser建议的WebView,您可以使用我的OHAttributedLabel类(您可以找到它on GitHub here或搜索在Stackoverflow上的术语,也被其他帖子提及),它能够显示NSAttributedString并为其添加超链接。

请参阅我的github存储库中提供的示例代码:您可以使用我的addCustomLink:inRange:方法将链接(带有自定义URL)添加到一系列文本中(您可以通过迭代每次出现的文本来确定该范围)你的文字中的“iPhone”这个词很容易)。然后在OHAttributedLabel的委托方法中,您可以捕获链接何时被点击并相应地采取行动以执行您需要的任何操作。

答案 1 :(得分:0)

我是否正确地假设您想在标签顶部放置一个按钮,以便用户可以点击“iPhone”这个词? 如果是这样,您应该考虑使用Web视图,因为计算确切位置相当复杂。即使确定“iPhone”单词出现的水平位置是可能且可靠的,但如果您的标签使用多行,则会变得非常困难。