iOS NSMutableAttributedString 简单使用

NSMutableAttributedString 部分属性介绍

/** NSFontAttributeName --- 设置字体大小 */
/** NSForegroundColorAttributeName --- 设置字体颜色 */
/** NSParagraphStyleAttributeName --- 设置段落格式 (暂无明显变化) */
/** NSBackgroundColorAttributeName --- 设置字体的背景颜色 */
/** NSLigatureAttributeName --- 设置连体属性 (暂无明显变化)*/
/** NSKernAttributeName ---  设置字符间的间距,整数加大,负数减小*/
/** NSStrikethroughStyleAttributeName ---  设置删除线*/
/** NSStrikethroughColorAttributeName --- 设置删除线的颜色 */
/** NSUnderlineStyleAttributeName --- 设置下划线 */
/** NSUnderlineColorAttributeName ---  设置下划线的颜色 */
/** NSStrokeWidthAttributeName --- 设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果 */
/** NSStrokeColorAttributeName --- 填充部分颜色,不是字体颜色,取值为 UIColor 对象 */
/** NSShadowAttributeName --- 设置字体的阴影,取值为 NSShadow 对象 */

 

NSString *str = @"哈哈哈,嘿嘿,啊哦,https://www.baidu.com,吼吼吼吼,嗯嗯嗯嗯,gakjfklj";
    
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc]initWithString:str];
    
    /** NSFontAttributeName --- 设置字体大小 */
    [attributeStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(0, 3)];
    
    /** NSForegroundColorAttributeName --- 设置字体颜色 */
    [attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 3)];
    
    [attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(10, 21)];
    
    /** NSParagraphStyleAttributeName --- 设置段落格式 (暂无明显变化) */
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
    style.headIndent = 16;
    [attributeStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, attributeStr.length)];
    
    /** NSBackgroundColorAttributeName --- 设置字体的背景颜色 */
    
    [attributeStr addAttribute:NSBackgroundColorAttributeName value:[UIColor cyanColor] range:NSMakeRange(4, 2)];
    
    /** NSLigatureAttributeName --- 设置连体属性 (暂无明显变化)*/
    
    [attributeStr addAttribute:NSLigatureAttributeName value:[NSNumber numberWithInteger:1] range:NSMakeRange(7, 2)];
    
    /** NSKernAttributeName ---  设置字符间的间距,整数加大,负数减小*/
    
    [attributeStr addAttribute:NSKernAttributeName value:[NSNumber numberWithInteger:0] range:NSMakeRange(7, 1)];
//    [attributeStr addAttribute:NSKernAttributeName value:[NSNumber numberWithInteger:-10] range:NSMakeRange(7, 1)];
    
    /** NSStrikethroughStyleAttributeName ---  设置删除线*/
    
    [attributeStr addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:1] range:NSMakeRange(attributeStr.length - 8, 8)];
    
    /** NSStrikethroughColorAttributeName --- 设置删除线的颜色 */
    [attributeStr addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(attributeStr.length - 8, 8)];
    
    /** NSUnderlineStyleAttributeName --- 设置下划线 */
    [attributeStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(10, 21)];
    
    
    [attributeStr addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"https://www.baidu.com/index.php?tn=monline_3_dg"] range:NSMakeRange(10, 21)];
    
    /** NSUnderlineColorAttributeName ---  设置下划线的颜色 */
    [attributeStr addAttribute:NSUnderlineColorAttributeName value:[UIColor redColor] range:NSMakeRange(10, 21)];
    
    /** NSStrokeWidthAttributeName --- 设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果 */
    [attributeStr addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithInteger:1] range:NSMakeRange(32, 4)];
    
    /** NSStrokeColorAttributeName --- 填充部分颜色,不是字体颜色,取值为 UIColor 对象 */
    [attributeStr addAttribute:NSStrokeColorAttributeName value:[UIColor magentaColor] range:NSMakeRange(32, 4)];
    
    /** NSShadowAttributeName --- 设置字体的阴影,取值为 NSShadow 对象 */
    NSShadow *shadow = [[NSShadow alloc]init];
    shadow.shadowColor = [UIColor orangeColor];
    shadow.shadowOffset = CGSizeMake(-2, -2);
    shadow.shadowBlurRadius = 2;
    [attributeStr addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(37, 4)];
    
    
    
    
    
    self.richLabel.attributedText = attributeStr;
View Code

 

 

转载于:https://www.cnblogs.com/yxtBlogs/p/5920818.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值