iOS开发富文本

时间:2024-01-12 10:06:14

iOS开发富文本

NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc] initWithString:@"犯我中华者,虽远必诛"];

[attributedStr setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:30]} range:NSMakeRange(0, 5)];

[attributedStr setAttributes:@{NSShadowAttributeName:[UIColor yellowColor],NSFontAttributeName:[UIFont systemFontOfSize:35]} range:NSMakeRange(2, 2)];

[attributedStr setAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:215/255.0 green:0 blue:18/255.0 alpha:0.9],NSFontAttributeName:[UIFont systemFontOfSize:30]} range:NSMakeRange(6, 4)];

self.attributedStr.attributedText = attributedStr;

//属性值

// Predefined character attributes for text. If the key is not in the dictionary, then use the default values as described below.

NSFontAttributeName                 // UIFont, default Helvetica(Neue) 12

NSParagraphStyleAttributeName       // NSParagraphStyle, default defaultParagraphStyle

NSForegroundColorAttributeName     // UIColor, default blackColor

NSBackgroundColorAttributeName      // UIColor, default nil: no background

NSLigatureAttributeName          // NSNumber containing integer, default 1: default ligatures, 0: no ligatures

NSKernAttributeName         // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled.

NSStrikethroughStyleAttributeName  // NSNumber containing integer, default 0: no strikethrough

NSUnderlineStyleAttributeName       // NSNumber containing integer, default 0: no underline

NSStrokeColorAttributeName         // UIColor, default nil: same as foreground color

NSStrokeWidthAttributeName        // 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           // NSShadow, default nil: no shadow

NSTextEffectAttributeName          // NSString, default nil: no text effect

NSAttachmentAttributeName           // NSTextAttachment, default nil

NSLinkAttributeName       // NSURL (preferred) or NSString

NSBaselineOffsetAttributeName      // NSNumber containing floating point value, in points; offset from baseline, default 0

NSUnderlineColorAttributeName      // UIColor, default nil: same as foreground color

NSStrikethroughColorAttributeName

NSObliquenessAttributeName

NSExpansionAttributeName

NSWritingDirectionAttributeName

NSVerticalGlyphFormAttributeName

/************************ Attribute values ************************/