2014-01-15 44 views
0

我創建的UILabel的一個自定義子類GTLabel有合適的字體,大小,顏色等...正確施放一個UILabel

當我編程方式創建一個GTLabel,一切工作正常。當我在IB中創建一個UILabel時,我只需將它的類更改爲GTLabel即可。

現在,當我有一個UIButton,帶有一個titleLabel,我想將這個UILabel轉換成一個GTLabel。

我創建了一個類方法GTLabel:

+ (GTLabel*)labelFromLabel:(UILabel*)label 
{ 
    ... 

    return myGTLabel; 
} 

我實在不明白我是如何想的這個方法來進行。

我想做的是波紋管嗎?

GTLabel *myGTLabel = [[GTLabel alloc] init]; 
// Get all the properties of the original label 
myGTLabel.text = label.text; 
myGTLabel.frame = label.frame; 
// Do the modifications 
myGTLabel.font = [UIFont fontWithName:@"Gotham-Light" 
           size:label.font.pointSize]; 

的想法是做類似

myButton.titleLabel = [GTLabel labelFromLabel:myButton.titleLabel]; 

感謝您的幫助!

+0

我們需要你的迴應。 –

回答

3

您可以實現自定義的UIButton(如GTButton),並重新定義titleLabel屬性,設置用於您GTLabel

像一樣propertylabel:

#import "GTButton.h" 

@implementation GTButton 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 


-(UILabel *)titleLabel 
{ 
    UILabel* parentLabel = [super titleLabel]; 

    parentLabel.font = [UIFont fontWithName:@"Gotham-Light" 
            size:parentLabel.font.pointSize]; 
    // ... set all your attributes 

    return parentLabel; 
} 

@end 
2

titleLabel UIButton的方法是隻讀的。

雖然此屬性是隻讀的,但它自己的屬性是可讀/寫的。主要使用這些屬性來配置按鈕的文本。

例如:

UIButton *button     = [UIButton buttonWithType: UIButtonTypeSystem]; 
button.titleLabel.font   = [UIFont systemFontOfSize: 12]; 
button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation; 

創建UIButton的自定義類爲您創建標籤,並設置屬性在裏面。