1、 UILabel 使用 NSString 顯示文字------代碼創建Label
{
//定義標籤的位置和尺寸,位置:x=0.0f, y=0.0f ,尺寸:x=320.0f, y=30.0f
UILabel *Label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 30.0f)];
//建立 NSString,定義其內容爲“abc”
NSString *sampleText = [NSString stringWithFormat:@"abc"];
//使用UILabel Label 獲取sampleText
Label.text = sampleText;
//輸出結果
NSLog(@“%@\n”,sampleText);
//在 View 的 subview 中加入標籤
[self.view addSubview:Label];
//使用後釋放Label
[Label release];
}
//設置顯示文字
scoreLabel.text = @"我是Andy--清風";
//設置字體:粗體,正常的是 SystemFontOfSize,調用系統的字體配置
scoreLabel.font = [UIFont boldSystemFontOfSize:20];
//設置文字顏色,可以有多種顏色可以選擇
scoreLabel.textColor = [UIColor orangeColor];
scoreLabel.textColor = [UIColor purpleColor];
//設置文字對齊位置,居左,居中,居右
scoreLabel.textAlignment = UITextAlignmentRight;
scoreLabel.textAlignment = UITextAlignmentCenter;
//設置字體大小是否適應label寬度
scoreLabel.adjustsFontSizeToFitWidth = YES;
//設置label的行數,這個可以根據上節的UITextView自適應高度
scoreLabel.numberOfLines = 2;
//設置文本是否高亮和高亮時的顏色
scoreLabel.highlighted = YES;
scoreLabel.highlightedTextColor = [UIColor orangeColor];
//設置陰影的顏色和陰影的偏移位置
scoreLabel.shadowColor = [UIColor redColor];
scoreLabel.shadowOffset = CGSizeMake(1.0,1.0);
//設置是否能與用戶進行交互
scoreLabel.userInteractionEnabled = YES;
//設置label中的文字是否可變,默認值是YES
scoreLabel.enabled = NO;
//設置文字過長時的顯示格式
scoreLabel.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中間
//設置背景色爲透明
scoreLabel.backgroudColor=[UIColor clearColor];
2、讓label自適應裏面的文字,自動調整寬度和高度。
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];這個frame是初設的,沒關係,後面還會重新設置其size。
[label setNumberOfLines:0];
NSString *s = @"string......";
UIFont *font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(320,2000);
CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake:(0,0, labelsize.width, labelsize.height)];
[self.view addSubView:label];