//建立uilabel UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 280, 80)]; //設置背景色 label1.backgroundColor = [UIColor grayColor]; //設置tag label1.tag = 91; //設置標籤文本 label1.text = @"Hello world!"; //設置標籤文本字體和字體大小 label1.font = [UIFont fontWithName:@"Arial" size:30]; //設置文本對其方式 label1.textAlignment = UITextAlignmentCenter; //文本對齊方式有如下三種 //typedef enum { // UITextAlignmentLeft = 0,左對齊 // UITextAlignmentCenter,居中對齊 // UITextAlignmentRight, 右對齊 //} UITextAlignment; //文本顏色 label1.textColor = [UIColor blueColor]; //超出label邊界文字的截取方式 label1.lineBreakMode = UILineBreakModeTailTruncation; //截取方式有如下6種 //typedef enum { // UILineBreakModeWordWrap = 0, 以空格爲邊界,保留整個單詞 // UILineBreakModeCharacterWrap, 保留整個字符 // UILineBreakModeClip, 到邊界爲止 // UILineBreakModeHeadTruncation, 省略開始,以……代替 // UILineBreakModeTailTruncation, 省略結尾,以……代替 // UILineBreakModeMiddleTruncation,省略中間,以……代替,多行時做用於最後一行 //} UILineBreakMode; //文本文字自適應大小 label1.adjustsFontSizeToFitWidth = YES; //當adjustsFontSizeToFitWidth=YES時候,若是文本font要縮小時 //baselineAdjustment這個值控制文本的基線位置,只有文本行數爲1是有效 label1.baselineAdjustment = UIBaselineAdjustmentAlignCenters; //有三種方式 //typedef enum { // UIBaselineAdjustmentAlignBaselines = 0, 默認值文本最上端於label中線對齊 // UIBaselineAdjustmentAlignCenters,//文本中線於label中線對齊 // UIBaselineAdjustmentNone,//文本最低端與label中線對齊 //} UIBaselineAdjustment; //文本最多行數,爲0時沒有最大行數限制 label1.numberOfLines = 2; //最小字體,行數爲1時有效,默認爲0.0 label1.minimumFontSize = 10.0; //文本高亮 label1.highlighted = YES; //文本是否可變 label1.enabled = YES; //去掉label背景色 //label1.backgroundColor = [UIColor clearColor]; //文本陰影顏色 label1.shadowColor = [UIColor grayColor]; //陰影大小 label1.shadowOffset = CGSizeMake(1.0, 1.0); //是否能與用戶交互 label1.userInteractionEnabled = YES; [self.view addSubview:label1]; [label1 release];