自己筆記 感覺直接上代碼比較直觀,文筆不好也寫不好文章
static ? UITextView * textView = [UITextView new ];
1:NSString? 轉(zhuǎn) NSAttributedString ?同時(shí)設(shè)置富文本行間距
NSString *text =@"string";
NSAttributedString? * textStr =[[NSAttributedString alloc] initWithString:text];
NSMutableAttributedString *textstring = [[NSMutableAttributedString alloc] initWithAttributedString:textView.attributedText];
NSRange range1 = [textView selectedRange];
[textstring insertAttributedString:textStr atIndex:range1.location];
//設(shè)置行間距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 6;// 字體的行間距
NSDictionary *attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:16],
NSParagraphStyleAttributeName:paragraphStyle
};
//光標(biāo)位置
NSRange range11 = [textView selectedRange];
[textViewstring addAttributes:attributes range:NSMakeRange(0, range11.location)];
textView.attributedText = text string;
2:UITextView 中加入圖片
//添加圖片
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithAttributedString:textView.attributedText];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil] ;
textAttachment.image =ID;
//圖片寬度
CGFloat imageWidth = kScreenWidth -KLeft*3;
textAttachment.bounds = CGRectMake(0, 0, imageWidth, textAttachment.image.size.height*imageWidth/textAttachment.image.size.width);
NSAttributedString *textAttachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment] ;
NSRange range1 = [textView selectedRange];
[string insertAttributedString:textAttachmentString atIndex:range1.location];//為用戶指定要插入圖片的位置
textView.attributedText = string;
以此類同 ?同樣的方式可以設(shè)置換行處理等 操作,用UITextView 顯示復(fù)雜的頁面布局,可用于常見的發(fā)帖,發(fā)表文章等需求。