首页 > 代码库 > iOS NSMutableAttributedString 简单使用
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;
iOS NSMutableAttributedString 简单使用