富文本

在实际的应用开发中经常需要使用到富文本,这里总结经常使用到的富文本属性方便学习与记忆

  • NSFontAttributeName:指定字体大小,默认是12point;UIFont
    代码实例:
    NSMutableAttributedString *originString = [[NSMutableAttributedString alloc] initWithString:@"默认字体样式\n" attributes:nil]; //增加NSFontAttributeName属性设置字体大小为18 NSAttributedString *fontAttributeStr = [[NSAttributedString alloc] initWithString:@"带有NSFontAttributeName样式的字体\n" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]}]; [originString appendAttributedString:fontAttributeStr];

  • NSForegroundColorAttributeName:指定字体颜色,默认是黑色;UIColor
    代码实例:
    //增加NSForegroundColorAttributeName属性设置字体颜色为红色 NSAttributedString *textColorAttributeStr = [[NSAttributedString alloc] initWithString:@"带有NSForegroundColorAttributeName样式的字体\n" attributes:@{NSForegroundColorAttributeName:[UIColor redColor]}]; [originString appendAttributedString:textColorAttributeStr];

  • NSBackgroundColorAttributeName:指定字体背景颜色;UIColor
    代码实例:
    NSAttributedString *backColorAttributeStr = [[NSAttributedString alloc] initWithString:@"带有NSBackgroundColorAttributeName样式的字体\n" attributes:@{NSBackgroundColorAttributeName:[UIColor redColor]}]; [originString appendAttributedString:backColorAttributeStr];

  • NSLigatureAttributeName:连体字符设置,没用过暂时;NSNumber

  • NSKernAttributeName:设置字体间距;NSNumber
    代码实例:
    NSAttributedString *textSpaceAttributeStr = [[NSAttributedString alloc] initWithString:@"带有NSKernAttributeName样式的字体\n" attributes:@{NSKernAttributeName:@3}]; [originString appendAttributedString:textSpaceAttributeStr];

  • NSStrikethroughStyleAttributeName:设置删除线样式,其取值如下:NSNumber

取值 对应样式
NSUnderlineStyleNone 不设置删除线
NSUnderlineStyleSingle 一条较细的删除线
NSUnderlineStyleThick 一条较粗的删除线
NSUnderlineStyleDouble 两条较细的删除线
NSUnderlineStyleDouble 两条较细的删除线

代码实例:
NSAttributedString *deleteAttributeStr = [[NSAttributedString alloc] initWithString:@"带有NSStrikethroughStyleAttributeName样式的字体\n" attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleThick),NSStrikethroughColorAttributeName:[UIColor redColor]}]; [originString appendAttributedString:deleteAttributeStr];

  • NSStrikethroughColorAttributeName:设置删除线的颜色;UIColor
  • NSUnderlineStyleAttributeName ** 和 NSUnderlineColorAttributeName:设置下划线和下划线的颜色;NSNumber** && UIColor下划线样式取值与NSStrikethroughStyleAttributeName使用相同的枚举;
  • NSStrokeWidthAttributeNameNSStrokeColorAttributeName :字体描边以及描边颜色的设置;NSNumber && UIColor
    代码实例:
    //设置字体描边以及描边颜色 NSAttributedString *strokeAttributeStr = [[NSAttributedString alloc] initWithString:@"带有NSStrokeWidthAttributeName样式的字体\n" attributes:@{NSStrokeWidthAttributeName:@1,NSStrokeColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:18 weight:UIFontWeightBold]}]; [originString appendAttributedString:strokeAttributeStr];
  • NSShadowAttributeName:设置文字阴影; NSShadow
    代码实例:
    //设置文字阴影,使用NSShadow NSShadow *shadow = [[NSShadow alloc] init]; shadow.shadowBlurRadius = 3.0f; //模糊 shadow.shadowColor = [UIColor redColor];//颜色 shadow.shadowOffset = CGSizeMake(1, 5); NSAttributedString *shadowAttributeStr = [[NSAttributedString alloc] initWithString:@"带有NSShadowAttributeName样式的字体\n" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSShadowAttributeName:shadow}]; [originString appendAttributedString:shadowAttributeStr];
  • NSBaselineOffsetAttributeName:设置文字基于准线上下偏移,其值为正数时上移,反之下移 NSNumber
    代码实例:
    //设置文字偏移 NSAttributedString *baselineOffsetAttributeStr = [[NSAttributedString alloc] initWithString:@"带有NSBaselineOffsetAttributeName样式的字体\n" attributes:@{NSBaselineOffsetAttributeName:@5}]; //方便对比加入正常的字符串 [originString appendAttributedString:[[NSAttributedString alloc] initWithString:@"没有设置偏移的" attributes:nil]]; [originString appendAttributedString:baselineOffsetAttributeStr];
  • NSWritingDirectionAttributeName:设置文字书写方向 NSArray of NSNumber
    代码实例:
    //设置文字书写方向:NSWritingDirectionAttributeName NSAttributedString *directionAttributeStr = [[NSAttributedString alloc] initWithString:@"带有NSWritingDirectionAttributeName样式的字体\n" attributes:@{NSWritingDirectionAttributeName:@[@3]}]; [originString appendAttributedString:directionAttributeStr];
  • NSLinkAttributeName:设置点击文字的跳转链接,需要使用UITextView并实现其代理方法shouldInteractWithURL NSURL or NSString
    代码实例:
    //设置点击跳转链接:NSLinkAttributeName NSAttributedString *linkAttributeStr = [[NSAttributedString alloc] initWithString:@"带有NSLinkAttributeName样式的字体\n" attributes:@{NSLinkAttributeName:@"https://www.baidu.com"}]; [originString appendAttributedString:linkAttributeStr];
  • NSTextAttachment:文本附件,常被用来实现图文混排 NSTextAttachment
    代码实例:
    //图文混排 NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; attachment.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://upload.jianshu.io/users/upload_avatars/2115199/148387f72be4.jpg?imageMogr/thumbnail/240x240/quality/100"]]]; attachment.bounds = CGRectMake(0, 0, 30, 30); NSAttributedString *attachmentImg = [NSAttributedString attributedStringWithAttachment:attachment]; NSAttributedString *attachmentAttributeStr = [[NSAttributedString alloc] initWithString:@"带有NSTextAttachment样式的字体\n" attributes:nil]; [originString appendAttributedString:attachmentImg]; [originString appendAttributedString:attachmentAttributeStr];

以上所有样式的效果图如下:

富文本_第1张图片
Normal.png

你可能感兴趣的:(富文本)