UIlabel 继承与UIView 实现了 NSCoding 协议 主要的作用是用于在手机界面上展示一些内容 (标签作用)
m_label = [UILabel new];//创建一个label //设置label在手机坐标系中位置和大小 m_label.frame = CGRectMake(10, 40, WIDTH - 20, 40); //设置label中展示的内容 m_label.text = @"12345ytrewq"; //设置label中文字的对齐方式 m_label.textAlignment = NSTextAlignmentCenter; //设置文字的颜色 m_label.textColor = [UIColor redColor]; //设置label的背景颜色 m_label.backgroundColor = [UIColor lightGrayColor]; //设置文字 字体和字号 m_label.font = [UIFont systemFontOfSize:14]; //设置label圆角 m_label.layer.borderColor = [[UIColor greenColor] CGColor]; m_label.layer.cornerRadius = 8.0f; m_label.clipsToBounds = YES; m_label.layer.masksToBounds = YES; /* //设置高亮 label.highlighted = YES; label.highlightedTextColor = [UIColor orangeColor]; //设置阴影 label.shadowColor = [UIColor redColor]; label.shadowOffset = CGSizeMake(1.0,1.0); //设置是否能与用户进行交互 label.userInteractionEnabled = YES; //设置label中的文字是否可变,默认值是YES label.enabled = NO; //如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为 label4.baselineAdjustment = UIBaselineAdjustmentNone; */ [self.view addSubview:m_label];
UILabel 的一些高级用法 在一个label中展示出不同颜色不同字号不同字体的文字
/************** label的高级用法 *************/ //label中出现多种颜色的和不同字体的文字 NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"]; [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)]; [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)]; [str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)]; [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:14.0] range:NSMakeRange(0, 5)]; [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:24.0] range:NSMakeRange(6, 12)]; [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:14.0] range:NSMakeRange(19, 6)]; UILabel *label1 = [UILabel new]; label1.frame = CGRectMake(10, 100, WIDTH - 20, 40); label1.backgroundColor = [UIColor lightGrayColor]; label1.attributedText = str; label1.textAlignment = NSTextAlignmentCenter; [self.view addSubview:label1];
UILabel 的高度自动调整来适应其中的文字内容
UILabel *lab = [self.view viewWithTag:1000]; if (!lab) { lab = [UILabel new]; lab.text = _textfield.text; lab.font = [UIFont systemFontOfSize:14]; lab.textColor = [UIColor blackColor]; lab.backgroundColor = [UIColor orangeColor]; lab.numberOfLines = 0; lab.lineBreakMode = NSLineBreakByCharWrapping; [self.view addSubview:lab]; } //通过文字的多少来 让lab的高度自动调整 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init]; CGRect rect = [_textfield.text boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSParagraphStyleAttributeName:paragraphStyle} context:nil]; lab.frame = CGRectMake(50, self.textfield.frame.origin.y + 60, rect.size.width , rect.size.height);