第一版耍鬓,以實(shí)現(xiàn)功能為主流妻,相比較簡(jiǎn)陋一點(diǎn),多多包含,先上效果圖
1.先修改鍵盤上的工具欄
self.contentView.inputAccessoryView = [self createInputAccessoryView];
//鍵盤上的工具欄
- (UIView *)createInputAccessoryView {
// 相機(jī)
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"edit_pic"] style:UIBarButtonItemStylePlain target:self action:@selector(item1Action)];
//收起鍵盤
UIBarButtonItem *th1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(cancleClick)];
//兩個(gè)item之間的間距
UIBarButtonItem *th2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
th1.tintColor = [UIColor blackColor];
UIToolbar *tool = [UIToolbar new];
[tool sizeToFit];
[tool setItems:@[item1,th2,th1]];
return tool;
}
2.相機(jī)的點(diǎn)擊事件涣达,也就是圖片上傳服務(wù)器的方法
//工具欄的點(diǎn)擊事件
- (void)item1Action {
首先將圖片上傳到你們的服務(wù)器(這里需要圖片和url)
NSTextAttachment* textAttachment = [[NSTextAttachment alloc] init];
//userInfo這個(gè)屬性需要寫延展证薇,在下面
textAttachment.userInfo = 圖片的URL;
//照片賦值
textAttachment.image = image;
//設(shè)置圖片的大小
textAttachment.bounds = CGRectMake(0, 0, myWidth/2, myWidth/2);
//類型轉(zhuǎn)換
NSAttributedString* imageAttachment = [NSAttributedString attributedStringWithAttachment:textAttachment];
NSMutableAttributedString *attriStr = [self.contentView.attributedText mutableCopy];
//獲取光標(biāo)的位置
NSRange range = self.contentView.selectedRange;
//插入
[attriStr insertAttributedString:imageAttachment atIndex:range.location];
//拼接
//[attriStr appendAttributedString:imageAttachment];
self.contentView.attributedText = attriStr;
}
3.上傳服務(wù)器的相關(guān)操作
正常來(lái)說(shuō),直接富文本文字轉(zhuǎn)換成HTML格式的字符串上傳服務(wù)器就可以了浑度。如果沒(méi)有特殊要求,
上傳字符串到服務(wù)器就告一段落了箩张。
我們這后臺(tái)特殊要求完残,需要轉(zhuǎn)換成字符串横漏,然后圖片鏈接拼接到相對(duì)應(yīng)的位置(可以選擇性忽略)
NSString *textStr = [[self textStringWithSymbol:@"[圖片]" attributeString:self.contentView.attributedText] mutableCopy];
int index = 0;
//通過(guò)替換
if (textStr.length > 3) {
for (int i = 0; i <= textStr.length - 4; i ++) {
NSString *tempStr = [textStr substringWithRange:NSMakeRange(i, 4)];
if ([tempStr isEqualToString:@"[圖片]"]) {
NSString *imgStr = [NSString stringWithFormat:@"<p><img src=\"%@\"></p>",self.imgArray[index]];
textStr = [textStr stringByReplacingCharactersInRange:NSMakeRange(i, 4) withString:imgStr];
index ++;
}
}
}
執(zhí)行上傳服務(wù)器的操作熟掂。
textStr 就是最終上傳上傳服務(wù)器的文本。
下面的方法是將富文本轉(zhuǎn)換成純文本的信息
/** 將富文本轉(zhuǎn)換為帶有圖片標(biāo)志的純文本*/
- (NSString *)textStringWithSymbol:(NSString *)symbol attributeString:(NSAttributedString *)attributeString{
NSString *string = attributeString.string;
//調(diào)用的方法在下面
string = [self stringDeleteString:@"\n" frontString:@"[圖片]" inString:string];
//最終純文本
NSMutableString *textString = [NSMutableString stringWithString:string];
//替換下標(biāo)的偏移量
__block NSUInteger base = 0;
//遍歷
[attributeString enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, attributeString.length)
options:0
usingBlock:^(id value, NSRange range, BOOL *stop) {
//檢查類型是否是自定義NSTextAttachment類
if (value && [value isKindOfClass:[NSTextAttachment class]]) {
//替換
[textString replaceCharactersInRange:NSMakeRange(range.location + base, range.length) withString:symbol];
//增加偏移量
base += (symbol.length - 1);
//將富文本中最終確認(rèn)的照片取出來(lái)
NSTextAttachment *attachmentImg = (NSTextAttachment *)value;
//將圖片存入數(shù)組素跺,后面需要用到
[self.imgArray addObject:attachmentImg.userInfo];
}
}];
return textString;
}
/** 刪除字符串*/
- (NSString *)stringDeleteString:(NSString *)deleteString frontString:(NSString *)frontString inString:(NSString *)inString{
NSArray *ranges = [self rangeOfSymbolString:frontString inString:inString];
NSMutableString *mutableString = [inString mutableCopy];
NSUInteger base = 0;
for (NSString *rangeString in ranges) {
NSRange range = NSRangeFromString(rangeString);
[mutableString deleteCharactersInRange:NSMakeRange(range.location - deleteString.length + base, deleteString.length)];
base -= deleteString.length;
}
return [mutableString copy];
}
/** 統(tǒng)計(jì)文本中所有圖片資源標(biāo)志的range*/
- (NSArray *)rangeOfSymbolString:(NSString *)symbol inString:(NSString *)string {
NSMutableArray *rangeArray = [NSMutableArray array];
NSString *string1 = [string stringByAppendingString:symbol];
NSString *temp;
for (int i = 0; i < string.length; i ++) {
temp = [string1 substringWithRange:NSMakeRange(i, symbol.length)];
if ([temp isEqualToString:symbol]) {
NSRange range = {i, symbol.length};
[rangeArray addObject:NSStringFromRange(range)];
}
}
return rangeArray;
}
以上是轉(zhuǎn)換成純文本指厌,然后拼接上url操作,可以選擇性忽略踩验。
4.NSTextAttachment 延展,為NSTextAttachment添加一個(gè)屬性(關(guān)鍵操作)
.h文件
#import <UIKit/UIKit.h>
@interface NSTextAttachment (LMText)
//添加一個(gè)屬性
@property (nonatomic, strong) id userInfo;
@end
//.m文件
#import "NSTextAttachment+LMText.h"
#import <objc/runtime.h>
@implementation NSTextAttachment (LMText)
static void * keyOfUserInfo = &keyOfUserInfo;
//實(shí)現(xiàn)它的set箕憾,get方法
- (id)userInfo {
return objc_getAssociatedObject(self, keyOfUserInfo);
}
- (void)setUserInfo:(id)userInfo {
objc_setAssociatedObject(self, keyOfUserInfo, userInfo, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end