继承关系:UIView : UIResponder : NSObject布局
///UILabel 显示的文本只读,没法编辑,能够根据文字个数自动换行;字体
///UITextField 可编辑本文,可是没法换行,只能一行显示;当点击键盘上的return时,会触发事件。ui
////UITextView 可编辑文本,提供换行功能。spa
//建立uilabel UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 280, 80)];
//设置背景色 label1.backgroundColor = [UIColor grayColor]; //是否能与用户交互 label1.userInteractionEnabled = YES; //设置tag label1.tag = 91;
//设置标签文本 label1.text = @"Hello world!"; //设置标签文本字体和字体大小 label1.font = [UIFont fontWithName:@"Arial" size:30]; //字体、字号 //系统字体 label.font = [UIFont systemFontOfSize:30.0]; //加粗 label.font = [UIFont boldSystemFontOfSize:30.0]; //斜体 label.font = [UIFont italicSystemFontOfSize:30.0]; //拿到全部字体 NSArray* fonts = [UIFont familyNames]; //经过字体名字设置字体 label.font = [UIFont fontWithName:[fonts objectAtIndex:5] size:30.0]; //文本最多行数,为0时没有最大行数限制 自动换行 label1.numberOfLines = 2; //最小字体,行数为1时有效,默认为0.0 label1.minimumFontSize = 10.0; //段 行 字间距 label.numberOfLines = 0; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSKernAttributeName : @(1.5f)}];//字间距 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle setLineSpacing:5.0];//设置行间距 [paragraphStyle setParagraphSpacing:50.0];//设置段间距 [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])]; label.attributedText = attributedString;
//设置文本对其方式 label1.textAlignment = UITextAlignmentCenter; //文本对齐方式有如下三种 //typedef enum { // UITextAlignmentLeft = 0,左对齐 // UITextAlignmentCenter,居中对齐 // UITextAlignmentRight, 右对齐 //} UITextAlignment; //注:如今的版本已经弃用上述对齐方式,如下方为主 /* Values for NSTextAlignment */ typedef NS_ENUM(NSInteger, NSTextAlignment) { NSTextAlignmentLeft = 0, // Visually left aligned #if TARGET_OS_IPHONE NSTextAlignmentCenter = 1, // Visually centered NSTextAlignmentRight = 2, // Visually right aligned #else /* !TARGET_OS_IPHONE */ NSTextAlignmentRight = 1, // Visually right aligned NSTextAlignmentCenter = 2, // Visually centered #endif NSTextAlignmentJustified = 3, // Fully-justified. The last line in a paragraph is natural-aligned. NSTextAlignmentNatural = 4, // Indicates the default alignment for script } NS_ENUM_AVAILABLE_IOS(6_0);
//文本颜色 label1.textColor = [UIColor blueColor]; //颜色渐变 label.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@""]]; //文本高亮 label1.highlighted = YES; //高亮颜色 label1.highlightedTextColor = [UIColor blueColor]; //文本是否可变 :未激活,灰色,设置颜色无效 label1.enabled = YES; //去掉label背景色 label1.backgroundColor = [UIColor clearColor]; //文本阴影颜色 label1.shadowColor = [UIColor grayColor]; //阴影偏移量 label1.shadowOffset = CGSizeMake(1.0, 1.0);
//超出label边界文字的截取方式 label1.lineBreakMode = UILineBreakModeTailTruncation; //截取方式有如下6种 //typedef enum { // UILineBreakModeWordWrap = 0, 以空格为边界,保留整个单词 // UILineBreakModeCharacterWrap, 保留整个字符 // UILineBreakModeClip, 到边界为止 // UILineBreakModeHeadTruncation, 省略开始,以……代替 // UILineBreakModeTailTruncation, 省略结尾,以……代替 // UILineBreakModeMiddleTruncation,省略中间,以……代替,多行时做用于最后一行 //} UILineBreakMode;
//调整基线 需设置文本文字自适应大小 label1.adjustsFontSizeToFitWidth = YES; //baselineAdjustment这个值控制文本的基线位置,只有文本行数为1是有效 label1.baselineAdjustment = UIBaselineAdjustmentAlignCenters; //有三种方式 //typedef enum { // UIBaselineAdjustmentAlignBaselines = 0, 默认值文本最上端于label中线对齐 // UIBaselineAdjustmentAlignCenters,//文本中线于label中线对齐 // UIBaselineAdjustmentNone,//文本最低端与label中线对齐 //} UIBaselineAdjustment;
/* - (void)buttonClick:(UIButton*)button{ //父视图经过tag值获取子视图的指针对象 /* 子视图能够设置一个tag值,而后添加到父视图上,父视图就能够经过这个tag值拿到子视图的指针。 tag值也能够保存一些用户的信息。 */ UILabel* label = (UILabel*)[self.view viewWithTag:100]; label.text = @"我被修改了"; } */
//自动适应高度 CGSize size = CGSizeMake(280, 1000); UIFont *lableFont = [UIFont fontWithName:@"Arial" size:12.0]; CGSize labelSize = [label.text sizeWithFont:lableFont constrainedToSize:size];//7.0 ,现以下 //[label1.text boundingRectWithSize:(CGSize) options:(NSStringDrawingOptions) attributes:(NSDictionary *) context:(NSStringDrawingContext *)] label.frame = CGRectMake(0, 0, 280, labelSize.height);
//简单富文本 NSString *text = @"我是大坏蛋哦!"; NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text]; [attributeString setAttributes:@{NSForegroundColorAttributeName : [UIColor yellowColor], NSFontAttributeName : [UIFont systemFontOfSize:18]} range:NSMakeRange(2, 3)]; label1.attributedText = attributeString; //修改Range 所包含文本的 颜色、字体大小
//圆角 label.layer.backgroundColor =[UIColor yellowColor].CGColor; label.layer.cornerRadius =5.0; label.layer.frame = CGRectInset(label.layer.frame, 20, 20); ///边框 label.layer.borderColor = [[UIColor redColor] CGColor]; label.layer.borderWidth = 2;
[self.view addSubview:label1]; [label1 release];