2012-07-05 52 views
0

我是iphone.i的新手,發現UILabel實例方法很難實現,我可以使用它嗎我可以自定義UIlabel的文本的外觀,進一步通過子類化UILabel。我需要plz有點幫助發起。例如我在我的viewController有一個標籤,我怎麼能把它的文本和鋤頭歸類爲 。提前感謝。自定義UILabel的行爲

+0

lbltext.text = @「newtext」 –

+1

在xcode中,當你創建一個對象時,比如說UILabel * label;嘗試輸入「標籤」,然後按Esc鍵,xcode將顯示您的對象擁有的方法,然後「標記」。 (點)Xcode將呈現您的對象擁有的屬性。可以引導你。 – janusbalatbat

回答

3

可以使用UILabel這麼多的屬性,如:

UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 40)]; 
lbl.font = [UIFont fontWithName:@"Helvetica" size:12.0]; // For setting font style with size 
lbl.textColor = [UIColor whiteColor];  //For setting text color 
lbl.backgroundColor = [UIColor clearColor]; // For setting background color 
lbl.textAlignment = UITextAlignmentCenter; // For setting the horizontal text alignment 
lbl.numberOfLines = 2;      // For setting allowed number of lines in a label 
lbl.lineBreakMode = UILineBreakModeWordWrap; // For setting line break mode 
lbl.text = @"TitleText";       // For setting the text inside the label 

我們如果其他任何事情,你想知道的我知道!

這兩種方法

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines 
{ 
    return CGRectInset(bounds, MARGIN, MARGIN); 
} 

- (void)drawTextInRect:(CGRect)rect 
{ 
    [super drawTextInRect: CGRectInset(self.bounds, MARGIN, MARGIN)]; 
} 

我們正在使用CGRectInset創建一個矩形,它是除了現有的矩形(bounds)更大或更小。

對於較小的長方形,用作MARGIN 正值對於更大的矩形,用積極的價值觀作爲MARGIN

所有最優秀的

+0

告訴我如何實現這兩個方法。 - textRectForBounds:limitedToNumberOfLines: - drawTextInRect: –

+0

想到了兩種方法嗎? –

+0

如果你可以標記我的答案是正確的,那麼這對其他人也有幫助。 –