UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 200, 400)];
label.frame = CGRectMake(97, 47, 223, 19);
label.tag =101;
label.text =@"abcd"
NSString *labelText = @"abcd"; label.text = labelText;
label.font = [UIFont systemFontOfSize:12];//采用系统默认文字设置大小 label.font = [UIFont fontWithName:@"Arial" size:30];//设置文字类型与大小
label.textColor = [UIColor lightGrayColor];//其中textColor要用UIColor类型
label.textAlignment = NSTextAlignmentLeft;
label.lineBreakMode =NSLineBreakByCharWrapping;//其中lineBreakMode可选值为
linBreakMode enum{
NSLineBreakByWordWrapping = 0,//保留整个单词,以空格为边界
NSLineBreakByCharWrapping,//保留整个字符
NSLineBreakByClipping,//以边界为止
NSLineBreakByTruncatingHead,//省略开头,以省略号代替
NSLineBreakByTruncatingTail,//省略结尾,以省略号代替
NSLineBreakByTruncatingMiddle//省略中间,以省略号代替
}
label.numberOfLines = 1;//行数设置为1,不设置时系统会默认行数为1
[label sizeToFit];
commentTextLabel.lineBreakMode = NSLineBreakByCharWrapping; commentTextLabel.numberOfLines = 0;
label.numberOfLines =1; label.adjustsFontSizeToFitWidth =YES;
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(100, 180) lineBreakMode:NSLineBreakByCharWrapping];
float maxHeight =50;//设置最大高度
float minFontSize =9;
float height;
int fontSize = 31;//设置最大字号
NSString *text = @"输入文本内容";
do {
fontSize = fontSize - 1;
UIFont *font =[UIFont fontWithName:@"Arial" size:fontSize];
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(100, 180)/*宽度与label的宽度一样,高度应高于label高度*/ lineBreakMode:NSLineBreakByCharWrapping];
height = size.height;
NSLog(@"height=%f,fontSize=%d,text=%@",height,fontSize,text);
} while (height > maxHeight&&fontSize>minFontSize);
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];
label.text =text;
if (fontSize ==9) {//判断字体是否小于最小字号,小于最小字号时就使用系统默认的缩略显示
label.font = [UIFont fontWithName:@"Arial" size:15];
}
else{
label.font = [UIFont fontWithName:@"Arial" size:fontSize];
label.lineBreakMode = NSLineBreakByCharWrapping;//实现文字多行显示
label.numberOfLines = 0;
}
[self.view addSubview: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,70, 280,msgSie.height); textLabel.text = text; textLabel.lineBreakMode = NSLineBreakByCharWrapping;//实现文字多行显示 textLabel.numberOfLines = 0; [self.view addSubview:textLabel]; 设置label的边框粗细与颜色,设置前要在相应文件中加入#import<QuartzCore/QuartzCore.h> label.layer.borderColor = [UIColor lightGrayColor].CGColor;//边框颜色,要为CGColor label.layer.borderWidth = 1;//边框宽度
label.backgroundColor =[UIColor yellowColor];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 200, 400)]; UIImageView *imageView =[[UIImageView alloc]init]; imageView.frame =CGRectMake(50, 50, 200, 400); UIImage *image=[UIImage imageNamed:@"1.jpg"]; imageView.image =image;//imageView会根据自身大小改变添加的图片的大小所以不需要额外设置image label.backgroundColor = [UIColor clearColor]; label.text =@"hello world"; label.font = [UIFont systemFontOfSize:30]; label.textColor = [UIColor yellowColor]; [self.view addSubview:imageView];//添加的顺序不能错,否则图片会覆盖label [self.view addSubview:label];
UIColor * color = [UIColor colorWithPatternImage:image];//image为需要添加的背景图 UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 200)]; [label setBackgroundColor:color]; [self.view addSubview:label];
-(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;
}
CGSize size= CGSizeMake(100, 200); UIImage *image =[UIImage imageNamed:@"1.jpg"]; UIImage *laterImage =[self scaleImage:image ToSize:size]; UIColor * color = [UIColor colorWithPatternImage:laterImage]; UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 200)]; [label setBackgroundColor:color]; [self.view addSubview:label];
label.highLighted =YES;
label.shadowColor =[UIColor grayColor];
label.shadowOffset = CGSizeMake(2.0, 2.0);
label.layer.cornerRadius = 10;
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有