2011-08-30 57 views
1

我有關於設置UIButton框架的問題。將UIButton的框架設置爲UILabel中的特定單詞

 NSString *sample = @"Demo to set display view on label in iPhone"; 

    CGSize maximumLabelSize = CGSizeMake(55,9999); 
    expectedLabelSize = [sample sizeWithFont:[UIFont systemFontOfSize:20] 
               constrainedToSize:maximumLabelSize 
               lineBreakMode:UILineBreakModeWordWrap]; 

    NSLog(@"%f",expectedLabelSize.height); 


    self.label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, expectedLabelSize.width, expectedLabelSize.height)]; 
    self.label.backgroundColor = [UIColor clearColor]; 
    self.label.numberOfLines = 0; 
    self.label.text = sample; 
    [self.label sizeToFit]; 

所以在下面的UIButton代碼如何設置文本框的位置,這樣只有「iPhone」這個詞是可點擊?

self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
self.button.frame = CGRectMake(self.label.frame.origin.x, 
           self.label.frame.origin.y+50, 
           expectedLabelSize.width, 
           expectedLabelSize.height); 
+0

你想讓這個按鈕自動顯示在你的標籤上嗎? –

+0

我想單擊uilabel的iPhone文本,所以任何想法 –

+0

啊,我想你想UIButton是文本「iPhone」在UILabel?多線標籤很難實現。最好只將文本「iPhone」放在UIButton中,而不要放在UILabel中。 – progrmr

回答

0

要使只是一個一個UILabel可點擊的話,你就需要確切地知道其中的UILabel把這個詞的超鏈接。這很難(不是不可能),尤其是對於多行標籤,特別是如果允許調整字體大小以使文本適合。

爲什麼不使用UIWebView並改爲超鏈接?或分離即點擊文本,並把文本到UIButton的本身,如:

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

注意:如果您的問題沒有解決,你不應該勾選這個答案。

+0

沒有它的不工作,不能得到框架的按鈕請告訴我任何想法? –

+0

什麼部分不起作用?它如何失敗?添加NSLog,看看它說什麼(並告訴我們)。 – progrmr

+0

其實我想要得到y的按鈕框架,我用uabel和CGSizeMake(55,9999)中的多行文本self.label.numberOfLines = 0來設置uilabel的高度,所以請給我想法設置y框架按鈕 –