#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UILabel * label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.label = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, 300, 260)];
self.label.text = @"Label Text Content,This is text label things attribute";
//self.label.font = [UIFont systemFontOfSize:17];//使用17号
self.label.font = [UIFont fontWithName:@"Arial-BoldItalicMT"size:17];//设置字体和字号
self.label.textColor = [UIColor blackColor];//使用文本黑色
self.label.shadowColor = [UIColor blackColor];//设置阴影
self.label.shadowOffset = CGSizeMake(0, 1);//一个向上的阴影
self.label.textAlignment = NSTextAlignmentCenter;//设置文字居中
self.label.lineBreakMode = NSLineBreakByTruncatingTail;//段落样式,最后截断尾巴,用……代替
//富文本的基本数据类型-属性字符串。
NSString * string = self.label.text;
const CGFloat fontSize = 16.0;
NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc]initWithString:string];
NSInteger length = [string length];
//设置字体
UIFont * baseFont = [UIFont systemFontOfSize:fontSize];
//设置所有的字体
[attrString addAttribute:NSFontAttributeName value:baseFontrange:NSMakeRange(0, length)];
UIFont * boldFont = [UIFont boldSystemFontOfSize:fontSize];
//设置Text这四个字母的字体为粗体
[attrString addAttribute:NSFontAttributeName value:boldFontrange:[string rangeOfString:@"Text"]];
//设置颜色
UIColor * color = [UIColor redColor];
[attrString addAttribute:NSForegroundColorAttributeName value:color range:[string rangeOfString:@"Content"]];
[attrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Verdana-BoldItalic" size:18] range:[stringrangeOfString:@"Label"]];
self.label.numberOfLines = 2;
NSMutableParagraphStyle * style = [[NSParagraphStyle defaultParagraphStyle]mutableCopy];
style.lineSpacing = 10;//增加行高
style.headIndent = 10;//头部缩进
style.tailIndent = -10;//尾部缩进
style.lineHeightMultiple = 1.5;//行间距是多少倍
style.alignment = NSTextAlignmentLeft;//左对齐
style.firstLineHeadIndent = 20;//首行头缩进
style.paragraphSpacing = 10;//段落后面的间距;
style.paragraphSpacingBefore = 20;//段落之前的间距
[attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, length)];
[attrString addAttribute:NSKernAttributeName value:@2range:NSMakeRange(0, length)];//字符间距 2pt
[attrString addAttribute:NSStrokeColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"is"]];//设置文字描边颜色,需要和NSStrokeWidthAttributeName设置描边宽度,这样就能使文字空心
[attrString addAttribute:NSStrokeWidthAttributeName value:@2range:[string rangeOfString:@"is"]];//空心字,文字边框描述
[attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:[stringrangeOfString:@"text"]];//下划线
[attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleThick) range:[stringrangeOfString:@"label"]];//厚的下划线
[attrString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle)range:[string rangeOfString:@"things"]];//删除线
[attrString addAttribute:NSStrikethroughColorAttributeName value:[UIColor blueColor] range:[stringrangeOfString:@"things"]];//删除线蓝色
self.label.attributedText = attrString;
self.label.highlightedTextColor = [UIColor redColor];//设置文本高亮显示颜色,与highlighted一起使用。
self.label.highlighted = NO; //高亮状态是否打开
self.label.enabled = YES;//设置文字内容是否可变
self.label.userInteractionEnabled = YES;//设置标签是否忽略或移除用户交互。默认为NO
self.label.baselineAdjustment = UIBaselineAdjustmentNone;//如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为。
// UIBaselineAdjustmentAlignBaselines=0,默认,文本最上端与中线对齐。
// UIBaselineAdjustmentAlignCenters, 文本中线与label中线对齐。
// UIBaselineAdjustmentNone, 文本最低端与label中线对齐。;
[self.view addSubview:self.label];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
转载于:https://www.cnblogs.com/ylg-----/p/4753015.html
相关资源:ios-UILabel的简单封装.zip