博客> 富文本的使用
富文本的使用
2017-12-12 04:52 评论:0 阅读:93 进击的可乐SAMA

富文本可以使控件上的字体变换各种模式,下面我们来看下基本用法


UILabel * changeLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 200, 315, 50)];
changeLabel.textColor = [UIColor blackColor];
changeLabel.backgroundColor = [UIColor whiteColor];
changeLabel.text = @"注意了注意了我要改变了哈哈哈";
changeLabel.font = [UIFont systemFontOfSize:18];
changeLabel.textAlignment = NSTextAlignmentCenter;

//使用NSMutaleAttributedString类来实现富文本
NSMutableAttributedString * noteStr = [[NSMutableAttributedString alloc] initWithAttributedString:changeLabel.attributedText];
NSRange range = NSMakeRange([[noteStr string] rangeOfString:@"改变"].location, [[noteStr string] rangeOfString:@"改变"].length);

//基本类型
if ([self.type isEqualToString:@"wordColor"]) {//改变字体颜色

    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];

}else if ([self.type isEqualToString:@"wordFont"]){//改变字体大小

    [noteStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:25] range:range];

}else if ([self.type isEqualToString:@"backgroundColor"]){//改变背景颜色

    [noteStr addAttribute:NSBackgroundColorAttributeName value:[UIColor orangeColor] range:range];

}else if ([self.type isEqualToString:@"deleteLine"]){//删除线格式 && 删除线颜色

    //删除线格式
    [noteStr addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:range];
    //删除线颜色
    [noteStr addAttribute:NSStrikethroughColorAttributeName value:[UIColor darkGrayColor] range:range];

}else if ([self.type isEqualToString:@"underLine"]){//下划线格式 && 下划线颜色

    //下划线格式
    [noteStr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:range];
    //下划线颜色
    [noteStr addAttribute:NSUnderlineColorAttributeName value:[UIColor greenColor] range:range];

}else if ([self.type isEqualToString:@"shadow"]){//阴影效果

    NSShadow *shadow = [[NSShadow alloc]init];
    shadow.shadowOffset = CGSizeMake(2, 2);
    //阴影颜色
    shadow.shadowColor = [UIColor orangeColor];
    //模糊程度
    shadow.shadowBlurRadius = 2;
    [noteStr addAttribute:NSShadowAttributeName value:shadow range:range];

}else if ([self.type isEqualToString:@"wordWeight"]){//字体拉长

    [noteStr addAttribute:NSExpansionAttributeName value:@(0.5) range:range];

}else if ([self.type isEqualToString:@"wordSlope"]){//字体倾斜

    [noteStr addAttribute:NSObliquenessAttributeName value:@(0.5) range:range];

}else if ([self.type isEqualToString:@"wordGap"]){//字间距

    [noteStr addAttribute:NSKernAttributeName value:@(5.5) range:range];

}else if ([self.type isEqualToString:@"wordTrace"]){//字体描边

    [noteStr addAttribute:NSStrokeWidthAttributeName value:@(5) range:range];

}else if ([self.type isEqualToString:@"wordCombo"]){//连笔字

    [noteStr addAttribute:NSLigatureAttributeName value:@(YES) range:range];

}else if ([self.type isEqualToString:@"urlLink"]){//网址链接

    [noteStr addAttribute:NSLinkAttributeName value:@"改变" range:range];

}else if ([self.type isEqualToString:@"addImage"]){//添加图片

    NSTextAttachment * attachment=[[NSTextAttachment alloc] init];
    UIImage * img=[UIImage imageNamed:@"icon_selected"];
    attachment.image=img;
    attachment.bounds=CGRectMake(0, -3, 20, 20);
    NSAttributedString * noteStr1 = [NSAttributedString attributedStringWithAttachment:attachment];
    [noteStr insertAttributedString:noteStr1 atIndex:9];

}else if ([self.type isEqualToString:@"all"]){//多个属性

    NSDictionary * dict = @{NSForegroundColorAttributeName:[UIColor blueColor],
                                           NSExpansionAttributeName:@(0.5)};
    [noteStr addAttributes:dict range:range];

}

[changeLabel setAttributedText:noteStr];
[self.view addSubview:changeLabel];

//注:下划线和删除线的类型
/*
     NSUnderlineStyleNone                                      没有删除线
     NSUnderlineStyleSingle                                  = 0x01,
     NSUnderlineStyleThick NS_ENUM_AVAILABLE(10_0, 7_0)      = 0x02,
     NSUnderlineStyleDouble                                    双删除线

     NSUnderlinePatternSolid NS_ENUM_AVAILABLE(10_0, 7_0)      配合上面使用
     NSUnderlinePatternDot NS_ENUM_AVAILABLE(10_0, 7_0)        配合上面使用
     NSUnderlinePatternDash NS_ENUM_AVAILABLE(10_0, 7_0)       配合上面使用
     NSUnderlinePatternDashDot NS_ENUM_AVAILABLE(10_0, 7_0)    配合上面使用
     NSUnderlinePatternDashDotDot
 */
收藏
0
sina weixin mail 回到顶部