2011-09-01 57 views
1
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. 
+0

請問您是否更具體,因爲我無法理解您的問題。你想在哪裏添加這個自定義按鈕?代碼段中沒有提供與按鈕相關的代碼,我們應該如何使用它? –

+0

我想要準確地添加自定義按鈕,無論我的iPhone文本顯示如此任何想法? –

回答

1

如果您想在標籤中的某些自定義文本上創建「鏈接」,而不是像@Fabian Kreiser建議的那樣使用WebView,那麼您應該使用我的OHAttributedLabel類(您可以找到它on GitHub here或在Stackoverflow上搜索術語,它也被其他帖子所提及),它能夠顯示NSAttributedString並向其添加超鏈接。

請參閱我的github存儲庫上提供的示例代碼:您可以使用我的addCustomLink:inRange:方法將一個鏈接(帶有自定義URL)添加到一定範圍的文本中(您可以通過迭代每次出現的單詞「 iPhone「很容易在你的文本中)。然後在OHAttributedLabel上的委託方法中,您可以捕獲何時點擊該鏈接並根據需要採取相應措施。

0

我是正確的假設,要換上標籤頂部的按鈕,以便用戶能夠挖掘單詞「iPhone」? 如果是這樣,您應該考慮使用Web視圖,因爲計算確切的位置相當複雜。即使確定單詞「iPhone」的水平位置是可能的也是可靠的,但如果您的標籤使用多行,則變得非常困難。