• NSMutableAttributedString/NSAttributedString 富文本设置


      今天在做项目的过程中,我们的设计师想要一种字体四周都带阴影的效果,但是我们平时使用的setShadowColor 和setShadowOffset是达不到这种效果,setShadowOffset 只能让阴影在上下,左右 方向中都只能显示一个方向,达不到文字的四周都有阴影的这种效果。随后我们在查阅iOS SDK之后,发现可以通过attributedText的属性来设置字体的样式,经过测试是可以的.

    NSString *text = @"test";

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];

    NSShadow *shadow = [[NSShadow alloc] init];

    [shadow setShadowColor:[UIColorcolorWithRed:0green:0blue:0alpha:0.5]];

    [shadow setShadowBlurRadius:4.0];

    [shadow setShadowOffset:CGSizeMake(0, 0)];

    [attributedString addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, [attributedString length])];

    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColorlightGrayColor] range:NSMakeRange(0, [attributedString length])];

    [attributedString addAttribute:UIFontsystemFontOfSize value:18 range:NSMakeRange(0, [attributedString length])];

    userName.attributedText = textLabelStr;

    在上面这段代码中我们设置了字体大小,字体颜色,字体的阴影,字体阴影的大小。那么其实对字体样式进行设置的时候是可以限定样式应用在字符串中的位置。那么对于NSMutableAttributedString 我们可以添加哪些设置,SDK中描述如下:

    NSFontAttributeName NS_AVAILABLE_IOS(6_0);                // UIFont, default Helvetica(Neue) 12

    NSParagraphStyleAttributeName NS_AVAILABLE_IOS(6_0);      // NSParagraphStyle, default defaultParagraphStyle

    NSForegroundColorAttributeName NS_AVAILABLE_IOS(6_0);     // UIColor, default blackColor

    NSBackgroundColorAttributeName NS_AVAILABLE_IOS(6_0);     // UIColor, default nil: no background

    NSLigatureAttributeName NS_AVAILABLE_IOS(6_0);            // NSNumber containing integer, default 1: default ligatures, 0: no ligatures

    NSKernAttributeName NS_AVAILABLE_IOS(6_0);                // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled. (note: values other than nil and 0 are unsupported on iOS)

    NSStrikethroughStyleAttributeName NS_AVAILABLE_IOS(6_0);  // NSNumber containing integer, default 0: no strikethrough

    NSUnderlineStyleAttributeName NS_AVAILABLE_IOS(6_0);      // NSNumber containing integer, default 0: no underline

    NSStrokeColorAttributeName NS_AVAILABLE_IOS(6_0);         // UIColor, default nil: same as foreground color

    NSStrokeWidthAttributeName NS_AVAILABLE_IOS(6_0);         // NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)

    NSShadowAttributeName NS_AVAILABLE_IOS(6_0);              // NSShadow, default nil: no shadow

    NSTextEffectAttributeName NS_AVAILABLE_IOS(7_0);          // NSString, default nil: no text effect

    NSAttachmentAttributeName NS_AVAILABLE_IOS(7_0);          // NSTextAttachment, default nil

    NSLinkAttributeName NS_AVAILABLE_IOS(7_0);                // NSURL (preferred) or NSString

    NSBaselineOffsetAttributeName NS_AVAILABLE_IOS(7_0);      // NSNumber containing floating point value, in points; offset from baseline, default 0

    NSUnderlineColorAttributeName NS_AVAILABLE_IOS(7_0);      // UIColor, default nil: same as foreground color

    NSStrikethroughColorAttributeName NS_AVAILABLE_IOS(7_0);  // UIColor, default nil: same as foreground color

    NSObliquenessAttributeName NS_AVAILABLE_IOS(7_0);         // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew

    NSExpansionAttributeName NS_AVAILABLE_IOS(7_0);           // NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion

    NSWritingDirectionAttributeName NS_AVAILABLE_IOS(7_0);  

    NSVerticalGlyphFormAttributeName NS_AVAILABLE_IOS(6_0);  

    typedef NS_ENUM(NSInteger, NSUnderlineStyle) {

        NSUnderlineStyleNone                                = 0x00,

        NSUnderlineStyleSingle                              = 0x01,

        NSUnderlineStyleThick NS_ENUM_AVAILABLE_IOS(7_0)    = 0x02,

        NSUnderlineStyleDouble NS_ENUM_AVAILABLE_IOS(7_0)   = 0x09,

        NSUnderlinePatternSolid NS_ENUM_AVAILABLE_IOS(7_0)      = 0x0000,

        NSUnderlinePatternDot NS_ENUM_AVAILABLE_IOS(7_0)        = 0x0100,

        NSUnderlinePatternDash NS_ENUM_AVAILABLE_IOS(7_0)       = 0x0200,

        NSUnderlinePatternDashDot NS_ENUM_AVAILABLE_IOS(7_0)    = 0x0300,

        NSUnderlinePatternDashDotDot NS_ENUM_AVAILABLE_IOS(7_0) = 0x0400,

        NSUnderlineByWord NS_ENUM_AVAILABLE_IOS(7_0) = 0x8000

    } NS_ENUM_AVAILABLE_IOS(6_0);

    typedef NS_ENUM(NSInteger, NSTextWritingDirection) {

        NSTextWritingDirectionEmbedding     = (0 << 1),

        NSTextWritingDirectionOverride      = (1 << 1)

    } NS_ENUM_AVAILABLE_IOS(7_0);

    NSTextEffectLetterpressStyle NS_AVAILABLE_IOS(7_0);

     从官方文档中我们可以看到,对于字符串我们很多的样式都可以设置,针对我们的需求我们可以挑选相应的设置来进行设置。但是需要提醒的是这些属性的设置只有在ios6.0以后才有用,所以我们在兼容性方面应该做好抉择。另外有些样式的设置是需要在ios7之后才可以使用。另外对于UILable我们可以使用setBackgroundColor,setFont,setTextColor等,但是在我们使用了NSMutableAttributedString 对应的属性将会被覆盖,所以我们在使用这些样式的时候应该要明白这一点。

     

     

  • 相关阅读:
    通过欧拉计划学Rust编程(第500题)
    通过欧拉计划学Rust编程(第54题)
    刷完欧拉计划中难度系数为5%的所有63道题,我学会了Rust中的哪些知识点?
    用欧拉计划学Rust编程(第26题)
    通过欧拉计划学习Rust编程(第22~25题)
    用欧拉计划学Rust语言(第17~21题)
    用欧拉计划学习Rust编程(第13~16题)
    用欧拉计划学Rust语言(第7~12题)
    通过欧拉计划学Rust(第1~6题)
    《区块链生存训练2.0》PDF
  • 原文地址:https://www.cnblogs.com/xishui2011/p/alanchen.html
Copyright © 2020-2023  润新知