標籤:white mcu pat uilabel 背景 sre 互動 對齊 設定
//
// ViewController.m
// LabelAll
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];
//labele背景色
myLabel.backgroundColor = [UIColor brownColor];
[self.view addSubview:myLabel];
//label文字
myLabel.text = @"不易yi";
//label文字顏色
myLabel.textColor = [UIColor whiteColor];
//label文字大小
myLabel.font = [UIFont systemFontOfSize:14];//系統預設是17
//label文字對齊
myLabel.textAlignment = 0;
/*
NSTextAlignmentLeft = 0, 左
NSTextAlignmentCenter = 1, 中
NSTextAlignmentRight = 2, 右
*/
//label標記(tag)
myLabel.tag = 123;
//設定文本省略
myLabel.lineBreakMode =NSLineBreakByTruncatingHead;//其中lineBreakMode可選值為
/*
linBreakMode enum{
NSLineBreakByWordWrapping = 0,//保留整個單詞,以空格為邊界
NSLineBreakByCharWrapping,//保留整個字元
NSLineBreakByClipping,//以邊界為止
NSLineBreakByTruncatingHead,//省略開頭,以省略符號代替
NSLineBreakByTruncatingTail,//省略結尾,以省略符號代替
NSLineBreakByTruncatingMiddle//省略中間,以省略符號代替
}
*/
myLabel.numberOfLines = 1;//行數設定為1,不設定時系統會預設行數為1
//當需要設定的行數為不限數量的時候可以用numberOfLines=0實現
//當label大小使用sizeToFit方法,調整大小時會考慮到該屬性中儲存的值
// [myLabel sizeToFit];
//實現文本多行顯示
myLabel.lineBreakMode = NSLineBreakByCharWrapping;
myLabel.numberOfLines = 0;
//adjustFontSizeToFitWidth方法可實現文本自動根據label大小自動調整字型尺寸,直到文本的大小達到了自己設定的label文本尺寸最大、最小值與字串的最大最小值,要是用這個方法還有一個很大的限制就是只有在numberOfLines設定為1時才能用
//設定label的邊框和顏色
myLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;//邊框顏色,要為CGColor
myLabel.layer.borderWidth = 1;//邊框寬度
//設定label圓角
myLabel.layer.cornerRadius = 10;
//設定陰影
myLabel.shadowColor =[UIColor grayColor];
//設定陰影大小
myLabel.shadowOffset = CGSizeMake(2.0, 2.0);
//設定高亮
myLabel.highlighted =YES;
//設定是否能與使用者互動
myLabel.userInteractionEnabled = YES;
//設定label文字是否可變
myLabel.enabled = NO;//系統預設是yes
#pragma mark 自適應寬
UILabel *label3 = [[UILabel alloc] init];
label3.text = @"jfdkgiuhuhkjkllklmmlkgfhsresr";
CGSize size1 = CGSizeMake(20,20); //設定一個行高上限
NSDictionary *attribute = @{NSFontAttributeName: label3.font};
CGSize labelsize = [label3.text boundingRectWithSize:size1 options:NSStringDrawingUsesDeviceMetrics attributes:attribute context:nil].size;
label3.frame = CGRectMake(0, 400, labelsize.width + 3, labelsize.height);
label3.backgroundColor = [UIColor redColor];
[self.view addSubview:label3];
// 系統的這個方法計算不是特別精確,所以要加3-5(不加的話,字串長了以後label.frame.size.width會略小於字串長度,導致文字顯示不全)
#pragma mark 根據常值內容自動調整label高度
NSString *text =[[NSString alloc]init];
text = @"輸入常值內容輸入常值內容輸入常值內容輸入常值內容";
CGSize size = CGSizeMake(280, 180);
UIFont *fonts = [UIFont systemFontOfSize:14.0];
CGSize msgSie = [text sizeWithFont:fonts constrainedToSize:size lineBreakMode: NSLineBreakByCharWrapping];
UILabel *textLabel = [[UILabel alloc] init];
[textLabel setFont:[UIFont boldSystemFontOfSize:14]];
textLabel.frame = CGRectMake(20,300, 280,msgSie.height);
textLabel.text = text;
textLabel.backgroundColor = [UIColor whiteColor];
textLabel.lineBreakMode = NSLineBreakByCharWrapping;//實現文字多行顯示
textLabel.numberOfLines = 0;
[self.view addSubview:textLabel];
#pragma mark 設定label背景圖
CGSize sizeone= CGSizeMake(100, 200);
UIImage *image =[UIImage imageNamed:@"1.jpg"];
//調用修改背景圖的大小與label大小一致的方法
UIImage *laterImage =[self scaleImage:image ToSize:sizeone];
UIColor * color = [UIColor colorWithPatternImage:laterImage];
UILabel * label1 = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 200)];
[label1 setBackgroundColor:color];
[self.view addSubview:label1];
}
-(UIImage *)scaleImage:(UIImage *)img ToSize:(CGSize)itemSize{
UIImage *i;
// 建立一個bitmap的context,並把它設定成為當前正在使用的context
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect=CGRectMake(0, 0, itemSize.width, itemSize.height);
// 繪製改變大小的圖片
[img drawInRect:imageRect];
// 從當前context中建立一個改變大小後的圖片
i=UIGraphicsGetImageFromCurrentImageContext();
// 使當前的context出堆棧
UIGraphicsEndImageContext();
// 返回新的改變大小後的圖片
return i;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
iOS開發UILabel的常用屬性和方法