2012-12-19 16 views

回答

10

你應該將2個UILabel作爲子視圖添加到UIButton中。

你可以不喜歡它:

UIButton *testButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
testButton.frame = CGRectMake(0, 0, 200, 40); 
[self.view addSubview:testButton]; 

UILabel *firstLineTestButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)]; 
firstLineTestButton.text = @"First line"; 
firstLineTestButton.font = [UIFont systemFontOfSize:12]; 
[testButton addSubview:firstLineTestButton]; 

UILabel *secondLineTestButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 200, 20)]; 
secondLineTestButton.text = @"Second line"; 
secondLineTestButton.font = [UIFont boldSystemFontOfSize:12]; 
[testButton addSubview:secondLineTestButton]; 


也作了對UILabels突出可能的,你需要做的按鈕自定義的突出顯示。

因此,將操作添加到按鈕,然後檢查標籤的按鈕子視圖並更改其顏色。

[testButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 
[testButton addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchDown]; 
[testButton addTarget:self action:@selector(touchCancel:) forControlEvents:UIControlEventTouchDragExit]; 

-(void)buttonAction:(UIButton*)sender 
{ 
    [self touchCancel:sender]; 
    /* DO SOME MORE ACTIONS */ 
} 

-(void)changeColor:(UIButton*)sender 
{ 
    sender.backgroundColor = [UIColor grayColor]; 

    for(UIView *subview in sender.subviews){ 
     if([subview isKindOfClass:[UILabel class]]){ 
      UILabel *subViewLabel = (UILabel*)subview; 
      subViewLabel.textColor = [UIColor blueColor]; 
     } 
    } 
} 

-(void)touchCancel:(UIButton*)sender 
{ 
    sender.backgroundColor = [UIColor clearColor]; 

    for(UIView *subview in sender.subviews){ 
     if([subview isKindOfClass:[UILabel class]]){ 
      UILabel *subViewLabel = (UILabel*)subview; 
      subViewLabel.textColor = [UIColor blackColor]; 
     } 
    } 
} 
+0

有點問題的話重繪。現在,當用戶選擇按鈕時,標籤保持不變,即沒有突出顯示等。在這種情況下做什麼?你能幫忙嗎? – user1903992

+0

只需將標籤的'backgroundColor'屬性設置爲'[UIColor clearColor]',你應該沒問題 –

+0

我已經更新了我的答案並突出顯示了按鈕 –

0

您可以添加兩個UILabels作爲按鈕的子視圖,然後可以設置標籤的文本

[lbl1 setFont:[UIFont fontWithName:@"Arial-Bold" size:18]]; 
[lbl2 setFont:[UIFont fontWithName:@"Arial" size:16]]; 
8

羅蘭的解決方案是好的,另一種方式來做到這一點是使用一個NSAttributedString。缺點是,它只適用於iOS 6及以上版本。

如果這是沒有問題的,這裏是代碼

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // We want 2 lines for our buttons' title label 
    [[self.button titleLabel] setNumberOfLines:2]; 

    // Setup the string 
    NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:@"This should be bold,\n and this should not."]; 

    // Set the font to bold from the beginning of the string to the "," 
    [titleText addAttributes:[NSDictionary dictionaryWithObject:[UIFont boldSystemFontOfSize:14] forKey:NSFontAttributeName] range:NSMakeRange(0, 20)]; 

    // Normal font for the rest of the text 
    [titleText addAttributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] range:NSMakeRange(20, 22)]; 

    // Set the attributed string as the buttons' title text 
    [self.button setAttributedTitle:titleText forState:UIControlStateNormal]; 
} 
+0

嗨,謝謝。這是一個問題,因爲我也需要支持iOS 4.3。但是,正如我上面評論的那樣,我遇到了羅蘭德方法的一個問題。也許你知道一個解決方法?謝謝。 – user1903992

0

對於正確的動畫,你應該繼承的UIButton和其狀態改變時

- (void)drawRect:(CGRect)rect 
{ 

    if (!self.firstLineTestButton) { 
     self.firstLineTestButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)]; 
     self.firstLineTestButton.text = @"First line"; 
     self.firstLineTestButton.font = [UIFont systemFontOfSize:12]; 
     [self addSubview:self.firstLineTestButton]; 
    } 

    if (!self.secondLineTestButton) { 
     self.secondLineTestButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 200, 20)]; 
     self.secondLineTestButton.text = @"Second line"; 
     self.secondLineTestButton.font = [UIFont boldSystemFontOfSize:12]; 
     [self addSubview:self.secondLineTestButton]; 
    } 

    if (!self.highlighted) { 
     // Do custom drawing such as changing text color 
    } else { 
     // Do custom drawing such as changing text color 
    } 
} 

- (void)setHighlighted:(BOOL)highlighted { 
    [super setHighlighted:highlighted]; 
    // Force redraw of button 
    [self setNeedsDisplay]; 
}