iOS富文本(NSAttributedString)

參考
iOS中關(guān)于AttributedString的那些事兒
iOS 7中文字排版和渲染引擎——Text Kit

富文本的所有屬性

    //NSFontAttributeName   字號(hào) UIFont 默認(rèn)12
    //NSParagraphStyleAttributeName    段落樣式  NSParagraphStyle
    //NSForegroundColorAttributeName    前景色   UIColor
    //NSBackgroundColorAttributeName    背景色   UIColor
    //NSObliquenessAttributeName        字體傾斜     NSNumber
    //NSExpansionAttributeName          字體加粗     NSNumber  比例 0就是不變 1增加一倍
    //NSKernAttributeName               字間距   CGFloat
    //NSUnderlineStyleAttributeName     下劃線     1或0
    //NSUnderlineColorAttributeName     下劃線顏色
    //NSStrikethroughStyleAttributeName 刪除線   1或0
    //NSStrikethroughColorAttributeName 某種顏色
    //NSStrokeColorAttributeName        same as ForegroundColor
    //NSStrokeWidthAttributeName        CGFloat
    //NSLigatureAttributeName           連筆字  1或0  沒看出效果
    // NSStrokeWidthAttributeName         設(shè)置筆畫寬度烤镐,取值為 NSNumber 對(duì)象(整數(shù))烧栋,負(fù)值填充效果,正值中空效果
    //NSShadowAttributeName             陰影    NSShawdow
    //NSTextEffectAttributeName          設(shè)置文本特殊效果虱肄,取值為 NSString 對(duì)象呀狼,目前只有圖版印刷效果可用:
    //NSAttachmentAttributeName         NSTextAttachment  設(shè)置文本附件,常用插入圖片
    //NSLinkAttributeName               鏈接  NSURL (preferred) or NSString
    //NSBaselineOffsetAttributeName     基準(zhǔn)線偏移   NSNumber

    //NSWritingDirectionAttributeName   文字方向     @[@(1),@(2)]  分別代表不同的文字出現(xiàn)方向等等,我想你一定用不到它 - -
    //NSVerticalGlyphFormAttributeName  水平或者豎直文本  1豎直 0水平 在iOS沒卵用,不支持豎版
NSAttributedString
@property (readonly, copy) NSString *string;
- (NSDictionary<NSString *, id> *)attributesAtIndex:(NSUInteger)location effectiveRange:(nullable NSRangePointer)range;
@property (readonly) NSUInteger length;
- (nullable id)attribute:(NSString *)attrName atIndex:(NSUInteger)location effectiveRange:(nullable NSRangePointer)range;
- (NSAttributedString *)attributedSubstringFromRange:(NSRange)range;

- (NSDictionary<NSString *, id> *)attributesAtIndex:(NSUInteger)location longestEffectiveRange:(nullable NSRangePointer)range inRange:(NSRange)rangeLimit;
- (nullable id)attribute:(NSString *)attrName atIndex:(NSUInteger)location longestEffectiveRange:(nullable NSRangePointer)range inRange:(NSRange)rangeLimit;

- (BOOL)isEqualToAttributedString:(NSAttributedString *)other;

- (instancetype)initWithString:(NSString *)str;
- (instancetype)initWithString:(NSString *)str attributes:(nullable NSDictionary<NSString *, id> *)attrs;
- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;

typedef NS_OPTIONS(NSUInteger, NSAttributedStringEnumerationOptions) {
  NSAttributedStringEnumerationReverse = (1UL << 1),
  NSAttributedStringEnumerationLongestEffectiveRangeNotRequired = (1UL << 20)
};

- (void)enumerateAttributesInRange:(NSRange)enumerationRange options:(NSAttributedStringEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(NSDictionary<NSString *, id> *attrs, NSRange range, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);
- (void)enumerateAttribute:(NSString *)attrName inRange:(NSRange)enumerationRange options:(NSAttributedStringEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(id _Nullable value, NSRange range, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);
@interface NSMutableAttributedString : NSAttributedString

- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str;
- (void)setAttributes:(nullable NSDictionary<NSString *, id> *)attrs range:(NSRange)range;

@end
@interface NSMutableAttributedString (NSExtendedMutableAttributedString)

@property (readonly, retain) NSMutableString *mutableString;

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
- (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range;
- (void)removeAttribute:(NSString *)name range:(NSRange)range;

- (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString;
- (void)insertAttributedString:(NSAttributedString *)attrString atIndex:(NSUInteger)loc;
- (void)appendAttributedString:(NSAttributedString *)attrString;
- (void)deleteCharactersInRange:(NSRange)range;
- (void)setAttributedString:(NSAttributedString *)attrString;

- (void)beginEditing;
- (void)endEditing;

@end
段落樣式

段落樣式主要改行距加袋、段距、首行縮進(jìn)抱既、最大最小行高职烧、多倍行距等十幾個(gè)屬性,把這些總結(jié)了你就比我更全..

    //NSMutableParagraphStyle 同 NSParagraphStyle
    NSMutableParagraphStyle *muParagraph = [[NSMutableParagraphStyle alloc]init];
    muParagraph.lineSpacing = 10; // 行距
    muParagraph.paragraphSpacing = 20; // 段距
    muParagraph.firstLineHeadIndent = 30; // 首行縮進(jìn)
    paragraphStyle.alignment = NSTextAlignmentJustified;//(兩端對(duì)齊的)文本對(duì)齊方式:(左防泵,中蚀之,右,兩端對(duì)齊捷泞,自然)    
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;//結(jié)尾部分的內(nèi)容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")    
    paragraphStyle.headIndent = 20;//整體縮進(jìn)(首行除外)    
    paragraphStyle.tailIndent = 20;//    
    paragraphStyle.minimumLineHeight = 10;//最低行高    
    paragraphStyle.maximumLineHeight = 20;//最大行高    
    paragraphStyle.paragraphSpacing = 15;//段與段之間的間距    
    paragraphStyle.paragraphSpacingBefore = 22.0f;//段首行空白空間/* Distance between the bottom of the previous paragraph (or the end of its paragraphSpacing, if any) and the top of this paragraph. */    
    paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;//從左到右的書寫方向(一共??三種)    
    paragraphStyle.lineHeightMultiple = 15;/* Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. */    
    paragraphStyle.hyphenationFactor = 1;//連字屬性 在iOS足删,唯一支持的值分別為0和1
附件(圖片)
    NSTextAttachment *attachment=[[NSTextAttachment alloc] initWithData:nil ofType:nil];
    UIImage *img=[UIImage imageNamed:@"test.png"];
    attachment.image=img;
    attachment.bounds=CGRectMake(0, 0, 20, 20);
    NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];
加載HTML標(biāo)簽文本
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
參數(shù)options里面的字典有三對(duì)key value
 NSPlainTextDocumentType  // 普通文本
 NSRTFTextDocumentType   //  富文本
 NSRTFDTextDocumentType   // 帶附件的富文本
 NSHTMLTextDocumentType  // 這個(gè)可以加載HTML格式的文本

UITextView

@property(nonatomic) NSRange selectedRange;
@property(null_resettable,copy) NSAttributedString *attributedText 
@property(nonatomic,copy) NSDictionary<NSString *, id> *typingAttributes NS_AVAILABLE_IOS(6_0); // automatically resets when the selection changes
- (void)scrollRangeToVisible:(NSRange)range;
@property (nullable, readwrite, strong) UIView *inputView;             
@property (nullable, readwrite, strong) UIView *inputAccessoryView;
// Get the text container for the text view
@property(nonatomic,readonly) NSTextContainer *textContainer NS_AVAILABLE_IOS(7_0);
// Inset the text container's layout area within the text view's content area
@property(nonatomic, assign) UIEdgeInsets textContainerInset NS_AVAILABLE_IOS(7_0);
// Convenience accessors (access through the text container)
@property(nonatomic,readonly) NSLayoutManager *layoutManager NS_AVAILABLE_IOS(7_0);
@property(nonatomic,readonly,strong) NSTextStorage *textStorage NS_AVAILABLE_IOS(7_0);
@property(nonatomic) UIDataDetectorTypes dataDetectorTypes NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
@protocol UITextViewDelegate <NSObject, UIScrollViewDelegate>
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
- (void)textViewDidChangeSelection:(UITextView *)textView;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市锁右,隨后出現(xiàn)的幾起案子失受,更是在濱河造成了極大的恐慌,老刑警劉巖咏瑟,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拂到,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡码泞,警方通過查閱死者的電腦和手機(jī)兄旬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來浦夷,“玉大人辖试,你說我怎么就攤上這事辜王。” “怎么了罐孝?”我有些...
    開封第一講書人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵呐馆,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我莲兢,道長(zhǎng)汹来,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任改艇,我火速辦了婚禮收班,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘谒兄。我一直安慰自己摔桦,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開白布承疲。 她就那樣靜靜地躺著邻耕,像睡著了一般。 火紅的嫁衣襯著肌膚如雪燕鸽。 梳的紋絲不亂的頭發(fā)上兄世,一...
    開封第一講書人閱讀 52,268評(píng)論 1 309
  • 那天,我揣著相機(jī)與錄音啊研,去河邊找鬼御滩。 笑死,一個(gè)胖子當(dāng)著我的面吹牛党远,可吹牛的內(nèi)容都是我干的削解。 我是一名探鬼主播,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼麸锉,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼钠绍!你這毒婦竟也來了舆声?” 一聲冷哼從身側(cè)響起花沉,我...
    開封第一講書人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎媳握,沒想到半個(gè)月后碱屁,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蛾找,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年娩脾,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片打毛。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡柿赊,死狀恐怖俩功,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情碰声,我是刑警寧澤诡蜓,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站胰挑,受9級(jí)特大地震影響蔓罚,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜瞻颂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一豺谈、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧贡这,春花似錦茬末、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至炼彪,卻和暖如春吐根,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背辐马。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來泰國打工拷橘, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人喜爷。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓冗疮,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親檩帐。 傳聞我的和親對(duì)象是個(gè)殘疾皇子术幔,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • Text Kit學(xué)習(xí)(入門和進(jìn)階): http://www.cocoachina.com/industry/201...
    F麥子閱讀 4,052評(píng)論 1 13
  • 最近浮躁,毛線都沒寫湃密,不斷有人關(guān)注點(diǎn)贊我诅挑,必須總結(jié)點(diǎn)干貨了。 項(xiàng)目上要加載html格式的文本泛源,學(xué)習(xí)一下富文本相關(guān)內(nèi)...
    簡(jiǎn)單也好閱讀 48,936評(píng)論 18 229
  • 前言:富文本是iOS開發(fā)中經(jīng)常使用拔妥,配置上不同的屬性和range可以讓一個(gè)label顯示出好幾種樣式,不再需要去使...
    無神閱讀 3,481評(píng)論 4 4
  • 在iOS開發(fā)中,常常會(huì)有一段文字顯示不同的顏色和字體,或者給幾個(gè)文字加刪除線或者下劃線的需求硬纤。之前在網(wǎng)上找一些資料...
    Song___閱讀 918評(píng)論 2 3
  • 搬離汀沙已有兩周筝家,我再也不會(huì)回去伐蒂。 汀沙是我從恒大離職后找到的第一個(gè)落腳處,離市橋地鐵站很近肛鹏,從住的地方走到地鐵站...
    顧廣城閱讀 403評(píng)論 0 0