UILabel是iOS开发中最常用的一种标签了,在开发过程中,会用到各种个样的标签属性,一段文字中变颜色或者字体的样式等(这些都是富文本中的内容,用于美化文字),下面就让我来给大家做一个详细的UILabel介绍吧!
@property (nonatomic, strong) UILabel *label;
self.label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, self.view.bounds.size.width-100, self.view.bounds.size.height- 200)];///第一种初始化方式
self.label = [UILabel alloc] init];///第二种初始化方式
self.label.text = @"我去。。。。这么短的么????还是不够长,文字到底要多长啊!!!!!!!!!!!!!!!~~~~~~~~~~";
self.label.textColor = [UIColor whiteColor];
self.label.font = [UIFont systemFontOfSize:25];
self.label.backgroundColor = [UIColor blackColor]
self.label.numberOfLines = 0;
self.label.textAlignment = NSTextAlignmentCenter;
self.label.enabled = YES;
self.label.highlighted = NO;
self.label.highlightedTextColor = [UIColor orangeColor];
self.label.lineBreakMode = NSLineBreakByTruncatingTail;
self.label.shadowColor = [UIColor orangeColor];
self.label.shadowOffset = CGSizeMake(0, 1.5);
self.label.baselineAdjustment = UIBaselineAdjustmentNone;
self.label.adjustsFontSizeToFitWidth = NO;
self.label.userInteractionEnabled = YES;
self.label.minimumScaleFactor = 0.5;
self.label.allowsDefaultTighteningForTruncation = NO;
self.label.preferredMaxLayoutWidth = 80;
self.label.enablesMarqueeWhenAncestorFocused = YES;
self.label.minimumFontSize = 14;
self.label.adjustsLetterSpacingToFitWidth = YES;
self.label.attributedText = string;//string为NSMutableAttributedString类型
富文本就是纯文本内容经过更高级的渲染,更改而成的文本(例如 一段文字中的某个字/某段字的颜色大小不一样,又或者有一些特殊字体例如 空心体等)一起来玩玩吧,富文本比较有意思
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.label.text];
[string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20.0f] range:NSMakeRange(0, 2)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 3)];
[string addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(5, 2)];
[string addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(9, 3)];
[string addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(11, 4)];
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Verdana-BoldItalic" size:13] range:NSMakeRange(15, 7)];
[string addAttribute:NSStrokeWidthAttributeName
value:@2 range:NSMakeRange(25, 3)];
[string addAttribute:NSStrokeColorAttributeName
value:[UIColor grayColor]
range:NSMakeRange(25, 3)];
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]};
[string addAttributes:attrsDic2 range:NSMakeRange(30, 3)];
NSShadow *shadow01 = [[NSShadow alloc]init];
shadow01.shadowBlurRadius = 5;//模糊度
shadow01.shadowColor = [UIColor magentaColor];
shadow01.shadowOffset = CGSizeMake(1, 3);
[string addAttributes:@{NSShadowAttributeName: shadow01,NSVerticalGlyphFormAttributeName: @(0),
NSExpansionAttributeName: @1} range:NSMakeRange(33, 4)];
NSShadow *shadow02 = [[NSShadow alloc]init]; shadow02.shadowBlurRadius = 5;//模糊度
shadow02.shadowColor = [UIColor blueColor];
shadow02.shadowOffset = CGSizeMake(1, 3);
[string addAttributes:@{NSShadowAttributeName: shadow02,
NSVerticalGlyphFormAttributeName: @(0),
NSObliquenessAttributeName: @1} range:NSMakeRange(37, 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;//对齐方式
/// 添加段落设置
[string addAttribute:NSParagraphStyleAttributeName
value:paragraph range:NSMakeRange(0, string.length)];
NSURL *url = [NSURL URLWithString:@"www.baidu.com"];
[string addAttribute:NSLinkAttributeName
value:url range:NSMakeRange(0, string.length)];
UILabel中虽然是我们用的最多,也是觉得的最简单的一个标签,但是当你仔细的去整理的时候,你会发现,label中有许多你不知道的东西。就像富文本一样,以前完全没有想过空心字是这样字设置出来的。UILabel在文本中真的非常大,特别是富文本,这也让你增加了很多的学习乐趣,好好玩玩富文本,是个好东西哦~