UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 300, 200)];//初始化标签
label.text = @"标签属性";//默认为空
label.font = [UIFont systemFontOfSize:17];//默认使用系统的17
label.textColor = [UIColor redColor];//默认使用文本黑色
label.shadowColor = [UIColor yellowColor];//默认没有阴影
label.shadowOffset = CGSizeMake(2,3);//默认是一个向上的阴影(0,-1),设置阴影的偏移量
label.textAlignment = NSTextAlignmentCenter;//默认是左对齐
label.lineBreakMode = NSLineBreakByTruncatingTail;//设置文字过长时的显示格式,默认是最后截断尾巴,用...代替
label.numberOfLines = 1;//标签最多显示行数,如果为0则表示多行。
label.enabled = NO;//默认是YES,只是决定了Label的绘制方式,将它设置为NO将会使文本变暗,表示它没有激活,这时向它设置颜色值是无效的。
label.highlighted = YES;//是否高亮显示
label.highlightedTextColor = [UIColor orangeColor]; //高亮显示时的文本颜色
label.adjustsFontSizeToFitWidth = NO;//默认NO,改变字母之间的间距来适应Label大小
label.baselineAdjustment = UIBaselineAdjustmentNone;//如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为。
label.layer.borderColor = [UIColor lightGrayColor].CGColor;//边框颜色设置
label.layer.borderWidth = 2.0;//边框宽度
[self.view addSubview:label];//添加到父视图上
拓展
一:字体类型
/* UIFont */
systemFontOfSize:系统的默认方法
boldSystemFontOfSize:字体加粗
italicSystemFontOfSize:字体为斜体
二:文本对齐方式
/* Values for NSTextAlignment */
NSTextAlignmentLeft 左对齐
NSTextAlignmentCenter 剧中对齐
NSTextAlignmentRight 右对齐
NSTextAlignmentJustified 两端对齐
NSTextAlignmentNatural 根据显示的文字特性对齐
三:段落样式
/* Values for NSLineBreakMode */
NSLineBreakByWordWrapping = 0 //在字边界处换行,默认
NSLineBreakByCharWrapping //在字符边界处换行
NSLineBreakByClipping //简单剪辑
NSLineBreakByTruncatingHead //截断在行首:“... wxyz”
NSLineBreakByTruncatingTail //截尾在行尾:“abcd ...”
NSLineBreakByTruncatingMiddle //截断行中间:“ab ... yz”
四:基线的行为
/* Values for baselineAdjustment */
UIBaselineAdjustmentAlignBaselines = 0,默认,文本最上端与中线对齐。
UIBaselineAdjustmentAlignCenters, 文本中线与label中线对齐。
UIBaselineAdjustmentNone, 文本最低端与label中线对齐。
//富文本的基本数据类型,属性字符串。
NSString *str = @"人生若只如初见,何事秋风悲画扇。\n等闲变却故人心,却道故人心易变。\n骊山语罢清宵半,泪雨霖铃终不怨。\n何如薄幸锦衣郎,比翼连枝当日愿。";
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
//attrStr添加字体和设置字体的范围
[attrStr addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:20.0f]
range:NSMakeRange(0, 3)];
//attrStr添加文字颜色
[attrStr addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(3, 4)];
//attrStr设置背景颜色
[attrStr addAttribute:NSBackgroundColorAttributeName
value:[UIColor yellowColor]
range:NSMakeRange(5, 2)];
//attrStr添加下划线
[attrStr addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInteger:NSUnderlineStyleSingle]
range:NSMakeRange(8, 3)];
//attrStr删除线
[attrStr addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(11, 1)];
//attrStr添加厚的下划线
[attrStr addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleThick)
range:NSMakeRange(12, 3)];
//attrStr字体格式
[attrStr addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"Verdana-BoldItalic" size:13]
range:NSMakeRange(17, 7)];
//NSShadowAttributeName 设置阴影,单独设置不好使,必须和其他属性搭配才好使,和以下这三个任一个搭配都好使,NSVerticalGlyphFormAttributeName,NSObliquenessAttributeName,NSExpansionAttributeName
//attrStr空心字,文字边框描述(组合)
[attrStr addAttribute:NSStrokeWidthAttributeName
value:@3 range:NSMakeRange(25, 3)];
[attrStr addAttribute:NSStrokeColorAttributeName
value:[UIColor blueColor]
range:NSMakeRange(25, 3)];
//attrStr设置删除线红线(组合)
[attrStr addAttribute:NSStrikethroughStyleAttributeName
value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle)
range:NSMakeRange(34, 3)];
[attrStr addAttribute:NSStrikethroughColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(34, 3)];
//attrStr设置文字描边颜色(组合)
[attrStr addAttribute:NSStrokeWidthAttributeName
value:@-2 range:NSMakeRange(29, 3)];
[attrStr addAttribute:NSStrokeColorAttributeName
value:[UIColor magentaColor]
range:NSMakeRange(29, 3)];
[attrStr addAttribute:NSForegroundColorAttributeName
value:[UIColor blueColor]
range:NSMakeRange(29, 3)];
//attrStr设置文字描边颜色(复合)
NSDictionary *attrsDic1 = @{NSStrokeWidthAttributeName: @-2,
NSStrokeColorAttributeName: [UIColor magentaColor],
NSForegroundColorAttributeName: [UIColor blueColor]};
[attrStr addAttributes:attrsDic1 range:NSMakeRange(29, 3)];
//attrStr设置阴影(以下开始复合使用)
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowBlurRadius = 5;//模糊度
shadow.shadowColor = [UIColor yellowColor];
shadow.shadowOffset = CGSizeMake(1, 3);
NSDictionary *attrsDic2 = @{NSShadowAttributeName: shadow,
NSVerticalGlyphFormAttributeName: @(0),
NSForegroundColorAttributeName: [UIColor magentaColor]};
[attrStr addAttributes:attrsDic2 range:NSMakeRange(51, 4)];
//NSVerticalGlyphFormAttributeName该属性所对应的值是一个 NSNumber 对象(整数)。0 表示横排文本。1 表示竖排文本。在 iOS 中,总是使用横排文本,0 以外的值都未定义。
//attrStr设置文本扁平化
NSShadow *shadow1 = [[NSShadow alloc]init];
shadow1.shadowBlurRadius = 5;//模糊度
shadow1.shadowColor = [UIColor magentaColor];
shadow1.shadowOffset = CGSizeMake(1, 3);
NSDictionary *attrsDic3 = @{NSShadowAttributeName: shadow1,
NSVerticalGlyphFormAttributeName: @(0),
NSExpansionAttributeName: @1};
[attrStr addAttributes:attrsDic3 range:NSMakeRange(59, 7)];
//attrStr设置字体倾斜
NSShadow *shadow2 = [[NSShadow alloc]init];
shadow2.shadowBlurRadius = 5;//模糊度
shadow2.shadowColor = [UIColor blueColor];
shadow2.shadowOffset = CGSizeMake(1, 3);
NSDictionary *attrsDic4 = @{NSShadowAttributeName: shadow2,
NSVerticalGlyphFormAttributeName: @(0),
NSObliquenessAttributeName: @1};
[attrStr addAttributes:attrsDic4 range:NSMakeRange(55, 3)];
//段落格式
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.lineSpacing = 2;//行间距
paragraph.headIndent = 10;//头部缩进,相当于左padding
paragraph.tailIndent = -10;//相当于右padding
paragraph.lineHeightMultiple = 1.5;//行间距是多少倍
paragraph.firstLineHeadIndent = 5;//指定段落开始的缩进,首行头缩进
paragraph.headIndent = 10;//调整全部文字的缩进
paragraph.paragraphSpacingBefore = 20;//段落之前的间距
paragraph.paragraphSpacing = 20;//段落后面的间距
paragraph.alignment = NSTextAlignmentLeft;//对齐方式
//attrStr添加段落设置
[attrStr addAttribute:NSParagraphStyleAttributeName
value:paragraph
range:NSMakeRange(0, [str length])];
//label添加超链接
NSURL *url = [NSURL URLWithString:@"www.baidu.com"];
[attrStr addAttribute:NSLinkAttributeName
value:url
range:NSMakeRange(42, 7)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 100, 200, 0)];
label.backgroundColor = [UIColor lightGrayColor];
//自动换行
label.numberOfLines = 0;
//设置label的富文本
label.attributedText = attrStr;
//label高度自适应
[label sizeToFit];
//添加父视图
[self.view addSubview:label];
拓展如下
字体名如下:
Font Family: American Typewriter
Font: AmericanTypewriter
Font: AmericanTypewriter-Bold
Font Family: AppleGothic
Font: AppleGothic
Font Family: Arial
Font: ArialMT
Font: Arial-BoldMT
Font: Arial-BoldItalicMT
Font: Arial-ItalicMT
Font Family: Arial Rounded MT Bold
Font: ArialRoundedMTBold
Font Family: Arial Unicode MS
Font: ArialUnicodeMS
Font Family: Courier
Font: Courier
Font: Courier-BoldOblique
Font: Courier-Oblique
Font: Courier-Bold
Font Family: Courier New
Font: CourierNewPS-BoldMT
Font: CourierNewPS-ItalicMT
Font: CourierNewPS-BoldItalicMT
Font: CourierNewPSMT
Font Family: DB LCD Temp
Font: DBLCDTempBlack
Font Family: Georgia
Font: Georgia-Bold
Font: Georgia
Font: Georgia-BoldItalic
Font: Georgia-Italic
Font Family: Helvetica
Font: Helvetica-Oblique
Font: Helvetica-BoldOblique
Font: Helvetica
Font: Helvetica-Bold
Font Family: Helvetica Neue
Font: HelveticaNeue
Font: HelveticaNeue-Bold
Font Family: Hiragino Kaku Gothic **** W3
Font: HiraKakuProN-W3
Font Family: Hiragino Kaku Gothic **** W6
Font: HiraKakuProN-W6
Font Family: Marker Felt
Font: MarkerFelt-Thin
Font Family: STHeiti J
Font: STHeitiJ-Medium
Font: STHeitiJ-Light
Font Family: STHeiti K
Font: STHeitiK-Medium
Font: STHeitiK-Light
Font Family: STHeiti SC
Font: STHeitiSC-Medium
Font: STHeitiSC-Light
Font Family: STHeiti TC
Font: STHeitiTC-Light
Font: STHeitiTC-Medium
Font Family: Times New Roman
Font: TimesNewRomanPSMT
Font: TimesNewRomanPS-BoldMT
Font: TimesNewRomanPS-BoldItalicMT
Font: TimesNewRomanPS-ItalicMT
Font Family: Trebuchet MS
Font: TrebuchetMS-Italic
Font: TrebuchetMS
Font: Trebuchet-BoldItalic
Font: TrebuchetMS-Bold
Font Family: Verdana
Font: Verdana-Bold
Font: Verdana-BoldItalic
Font: Verdana
Font: Verdana-Italic
Font Family: Zapfino
Font: Zapfino
//段落样式
lineSpacing; 来增加行距
paragraphSpacing;
alignment; 对齐
firstLineHeadIndent; 段落开始的缩排像素
headIndent; 可调整全部文字的缩排距离,可当作左边 padding 使用
tailIndent; 可调整文字尾端的缩排距离。需要注意的是,这里指定的值可以当作文字显示的宽、而也可当作右边 padding 使用,依据输入的正负值而定:
lineBreakMode;
minimumLineHeight;
maximumLineHeight; 而针对不同的字型与字号,我们可以透过指定最大与最小行距(maximumLineHeight 与 minimumLineHeight)来避免过高或过窄的状况发生。
baseWritingDirection;
lineHeightMultiple; 想要调整行距,可以透过搭配使用 lineHeightMultiple 更改行距倍数
paragraphSpacingBefore; 而若是文章内容有分段落的话,也可以透过指定段落结尾距离(paragraphSpacing)以及段落开头距离(paragraphSpacingBefore):
hyphenationFactor;
@property(readwrite,copy,NS_NONATOMIC_IOSONLY) NSArray *tabStops NS_AVAILABLE_IOS(7_0);
@property(readwrite,NS_NONATOMIC_IOSONLY) CGFloat defaultTabInterval NS_AVAILABLE_IOS(7_0);
在拓展: 用label即可加载html字符的,用富文本转一下html字符串即可
//str是要显示的字符串
NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
[attrString addAttributes:@{NSBaselineOffsetAttributeName: @(5),//设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏,可使UILabel文本垂直居中
NSFontAttributeName:[UIFont systemFontOfSize:14]} range:NSMakeRange(0, attrString.length)];
self.descLabel.attributedText = attrString;
//计算html字符串高度
NSMutableAttributedString *htmlString =[[NSMutableAttributedString alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:NULL error:nil];
[htmlString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} range:NSMakeRange(0, htmlString.length)];
CGSize textSize = [htmlString boundingRectWithSize:(CGSize){ScreenWidth - 20, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
return textSize.height ;