属性字符串NSAttributedString简易教程

原创 2015年11月19日 14:20:38
</pre><p><span style="font-size:18px"><strong>简单步骤</strong></span></p><p></p><pre name="code" class="objc"><span style="font-size:14px;"><pre name="code" class="objc">NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc]initWithString:normalString];
    [attriString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:titleSize] range:NSMakeRange(0 , a)];
    [attriString addAttribute:NSForegroundColorAttributeName value:textColor range:NSMakeRange(a, normalString.length - a)];
   
   [attriLabel setAttributedTitle:attriString forState:UIControlStateNormal]; </span>


属性一览

/************************ Attributes ************************/

// Predefined character attributes for text. If the key is not in the dictionary, then use the default values as described below.
UIKIT_EXTERN NSString * const NSFontAttributeName NS_AVAILABLE(10_0, 6_0);                // UIFont, default Helvetica(Neue) 12
UIKIT_EXTERN NSString * const NSParagraphStyleAttributeName NS_AVAILABLE(10_0, 6_0);      // NSParagraphStyle, default defaultParagraphStyle
UIKIT_EXTERN NSString * const NSForegroundColorAttributeName NS_AVAILABLE(10_0, 6_0);     // UIColor, default blackColor
UIKIT_EXTERN NSString * const NSBackgroundColorAttributeName NS_AVAILABLE(10_0, 6_0);     // UIColor, default nil: no background
UIKIT_EXTERN NSString * const NSLigatureAttributeName NS_AVAILABLE(10_0, 6_0);            // NSNumber containing integer, default 1: default ligatures, 0: no ligatures
UIKIT_EXTERN NSString * const NSKernAttributeName NS_AVAILABLE(10_0, 6_0);                // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled.
UIKIT_EXTERN NSString * const NSStrikethroughStyleAttributeName NS_AVAILABLE(10_0, 6_0);  // NSNumber containing integer, default 0: no strikethrough
UIKIT_EXTERN NSString * const NSUnderlineStyleAttributeName NS_AVAILABLE(10_0, 6_0);      // NSNumber containing integer, default 0: no underline
UIKIT_EXTERN NSString * const NSStrokeColorAttributeName NS_AVAILABLE(10_0, 6_0);         // UIColor, default nil: same as foreground color
UIKIT_EXTERN NSString * const NSStrokeWidthAttributeName NS_AVAILABLE(10_0, 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)
UIKIT_EXTERN NSString * const NSShadowAttributeName NS_AVAILABLE(10_0, 6_0);              // NSShadow, default nil: no shadow
UIKIT_EXTERN NSString *const NSTextEffectAttributeName NS_AVAILABLE(10_10, 7_0);          // NSString, default nil: no text effect

UIKIT_EXTERN NSString * const NSAttachmentAttributeName NS_AVAILABLE(10_0, 7_0);          // NSTextAttachment, default nil
UIKIT_EXTERN NSString * const NSLinkAttributeName NS_AVAILABLE(10_0, 7_0);                // NSURL (preferred) or NSString
UIKIT_EXTERN NSString * const NSBaselineOffsetAttributeName NS_AVAILABLE(10_0, 7_0);      // NSNumber containing floating point value, in points; offset from baseline, default 0
UIKIT_EXTERN NSString * const NSUnderlineColorAttributeName NS_AVAILABLE(10_0, 7_0);      // UIColor, default nil: same as foreground color
UIKIT_EXTERN NSString * const NSStrikethroughColorAttributeName NS_AVAILABLE(10_0, 7_0);  // UIColor, default nil: same as foreground color
UIKIT_EXTERN NSString * const NSObliquenessAttributeName NS_AVAILABLE(10_0, 7_0);         // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew
UIKIT_EXTERN NSString * const NSExpansionAttributeName NS_AVAILABLE(10_0, 7_0);           // NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion

UIKIT_EXTERN NSString * const NSWritingDirectionAttributeName NS_AVAILABLE(10_6, 7_0);    // NSArray of NSNumbers representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters.  The control characters can be obtained by masking NSWritingDirection and NSTextWritingDirection values.  LRE: NSWritingDirectionLeftToRight|NSWritingDirectionEmbedding, RLE: NSWritingDirectionRightToLeft|NSWritingDirectionEmbedding, LRO: NSWritingDirectionLeftToRight|NSWritingDirectionOverride, RLO: NSWritingDirectionRightToLeft|NSWritingDirectionOverride,

UIKIT_EXTERN NSString * const NSVerticalGlyphFormAttributeName NS_AVAILABLE(10_7, 6_0);   // An NSNumber containing an integer value.  0 means horizontal text.  1 indicates vertical text.  If not specified, it could follow higher-level vertical orientation settings.  Currently on iOS, it's always horizontal.  The behavior for any other value is undefined.


参考资料

[1]: NSAttributedString 详解

[2]: iOS中属性化字符串


版权声明:本文为博主原创文章,未经博主允许不得转载。

Objective-C属性字符串NSAttributedString

NSAttributedString是Objective-C中的属性字符串类,GitHub上也有很多第三方,用得较多的是TTTAttributedLabel,这里给大家介绍一下系统NSAttribut...

iOS NSAttributedString(属性字符串)

ui
  • slkai
  • slkai
  • 2014年11月12日 14:28
  • 5441

iOS开发富文本NSAttributedString属性

// // NSAttributedString.h // UIKit // // Copyright (c) 2011-2015 Apple Inc. All rights r...

NSAttributedString的21种属性详细介绍(图文混排)

说明: NSAttributedString 可以非常方便的实现文字排版和图文混排功能. 共有21中效果(API), 本文将较详细的介绍21种的属性的使用 注: 本博客由@凡俊编写, @...

iOS swift 字符属性NSAttributedString描述

/*  字符属性    字符属性可以应用于 attributed string 的文本中。    NSString *const NSFontAttributeName;...

NSAttributedString 属性

http://qiita.com/yusuga/items/30ee7a851aea0447db06 转自 - (void)drawRect:(CGRect)rect { ...

带属性的字符串 NSMutableAttributedString/NSAttributedString

由于iOS7新出的NSTextStorge是NSMutableAttributedString的子类,所以要用好NSTextStorage,首先要学好NSMutableAttributedString...

iOS NSAttributedString所有文本属性详解(多图)

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的iOS SDK详解专栏 http://blog.csdn.net/column/details/huan...

NSAttributedString设置字体属性

/// 改变字体大小及字体颜色,区分字体的颜色还是字体背景色 + (void)AttributedString:(NSMutableAttributedString *)attributedStr  ...

NSAttributedString文本属性设置的使用

使用NSAttributedString进行文本属性的设置。 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12 NSFontAttributeName NSMutabl...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:属性字符串NSAttributedString简易教程
举报原因:
原因补充:

(最多只允许输入30个字)