首先,先實現普通字符串轉屬性字符串吧(網上現成的??):
+(NSMutableAttributedString *)stringToAttributeString:(NSString *)text
{
//先把普通的字符串text轉化生成Attributed類型的字符串
NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc]initWithString:text];
NSString * zhengze = @"\\[\\([a-zA-Z0-9\u4e00-\u9fa5]+\\)\\]"; //正則表達式 ,例如 [(呵呵)] = ??
NSError * error;
NSRegularExpression * re = [NSRegularExpression regularExpressionWithPattern:zhengze options:NSRegularExpressionCaseInsensitive error:&error];
if (!re)
{
NSLog(@"%@??",[error localizedDescription]);//打印錯誤??
}
NSArray * arr = [re matchesInString:text options:0 range:NSMakeRange(0, text.length)];//遍歷字符串,獲得所有的匹配字符串
NSBundle *bundle = [NSBundle mainBundle];
NSString * path = [bundle pathForResource:@"emj" ofType:@"plist"]; //plist文件,制作一個 數組,包含文字,表情圖片名稱
NSArray * face = [[NSArray alloc]initWithContentsOfFile:path];//獲取 所有的數組
//如果有多個表情圖麸塞,必須從后往前替換,因為替換后Range就不準確了
for (int j =(int) arr.count - 1; j >= 0; j--) {
//NSTextCheckingResult里面包含range
NSTextCheckingResult * result = arr[j];
for (int i = 0; i < face.count; i++) {
if ([[text substringWithRange:result.range] isEqualToString:face[i][@"key"]])//從數組中的字典中取元素
{
NSString * imageName = [NSString stringWithString:face[i][@"picture"]];
NSTextAttachment * textAttachment = [[NSTextAttachment alloc]init];//添加附件,圖片
textAttachment.image = [UIImage imageNamed:imageName];
NSAttributedString * imageStr = [NSAttributedString attributedStringWithAttachment:textAttachment];
[attStr replaceCharactersInRange:result.range withAttributedString:imageStr];//替換未圖片附件
break;
}
}
}
return attStr;
}
當然,你肯定會需要轉回來??:
//把帶有圖片的屬性字符串轉成普通的字符串
+ (NSString *)textString:(NSAttributedString *)attributedText
{
NSMutableAttributedString * resutlAtt = [[NSMutableAttributedString alloc]initWithAttributedString:attributedText];
EmoticonsHelper * helper = [EmoticonsHelper new];
//枚舉出所有的附件字符串
[attributedText enumerateAttributesInRange:NSMakeRange(0, attributedText.length) options:NSAttributedStringEnumerationReverse usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) {
NSTextAttachment * textAtt = attrs[@"NSAttachment"];//從字典中取得那一個圖片
if (textAtt)
{
UIImage * image = textAtt.image;
NSString * text = [helper stringFromImage:image];
[resutlAtt replaceCharactersInRange:range withString:text];
}
}];
return resutlAtt.string;
}
//獲取圖片數組
-(NSArray *)getAllImagePaths//數組結構還是上述的截圖的數組結構
{
NSBundle *bundle = [NSBundle mainBundle];
NSString * path = [bundle pathForResource:@"emojo" ofType:@"plist"];
NSArray * face = [[NSArray alloc]initWithContentsOfFile:path];
return face;
}
會不會需要屬性字符串的尺寸大小呢?不用著急侵歇,直接有代碼:
+(CGSize)getAttributedTextSize:(NSString *)text
{
//先把普通的字符串text轉化生成Attributed類型的字符串
NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc]initWithString:text];
NSString * zhengze = @"\\[\\([a-zA-Z0-9\u4e00-\u9fa5]+\\)\\]";
NSError * error;
NSRegularExpression * re = [NSRegularExpression regularExpressionWithPattern:zhengze options:NSRegularExpressionCaseInsensitive error:&error];
if (!re)
{
NSLog(@"正則表達式匹配錯誤%@" ,[error localizedDescription]);
}
NSArray * arr = [re matchesInString:text options:0 range:NSMakeRange(0, text.length)];
if (!arr.count)//說明字符串中沒有表情通配符,是普通的文本,則計算文本size
{
NSDictionary *dic=@{NSFontAttributeName: [UIFont systemFontOfSize:14]};
CGSize size1=[text boundingRectWithSize:CGSizeMake(160, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
if (size1.height<=60)
{
size1.height=60;
}
else
{
size1.height+=15;
}
return size1;
}
NSBundle *bundle = [NSBundle mainBundle];
NSString * path = [bundle pathForResource:@"emj" ofType:@"plist"];
NSArray * face = [[NSArray alloc]initWithContentsOfFile:path];
//如果有多個表情圖,必須從后往前替換忌堂,因為替換后Range就不準確了
for (int j =(int) arr.count - 1; j >= 0; j--) {
//NSTextCheckingResult里面包含range
NSTextCheckingResult * result = arr[j];
for (int i = 0; i < face.count; i++) {
if ([[text substringWithRange:result.range] isEqualToString:face[i][@"key"]])
{
NSString * imageName = [NSString stringWithString:face[i][@"picture"]];
NSTextAttachment * textAttachment = [[NSTextAttachment alloc]init];
textAttachment.image = [UIImage imageNamed:imageName];
NSAttributedString * imageStr = [NSAttributedString attributedStringWithAttachment:textAttachment];
[attStr replaceCharactersInRange:result.range withAttributedString:imageStr];
break;
}
}
}
CGSize size2=[attStr boundingRectWithSize:CGSizeMake(180, 1000) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
size2.height+=40; //表情文字增加高度
return size2;//返回屬性字符串的尺寸
}
當然,以上這些都是網上現成的??酗洒,下面要寫一點不一樣的東西了??????????????????????????????????????????????
首先我們知道和字符串相關的UI控件呢不外乎那么幾種士修,怎么樣利用上屬性字符串呢???
1樱衷、UITextView棋嘲、UITextField
以UITextView為例,假設你得到的屬性字符串是NSAttributedString *result矩桂,我要顯示出表情圖片還得:
[TextView.textStorage insertAttributedString:result TextView.selectedRange.location];
TextView.selectedRange = NSMakeRange(TextView.selectedRange.location+1, 0);
//重置格式
NSRange wholeRange = NSMakeRange(0,TextView.textStorage.length);
[TextView.textStorage removeAttribute:NSFontAttributeName range:wholeRange];
[TextView.textStorage addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20.0f] range:wholeRange];
這樣不但能顯示表情圖片到你的TextView里??沸移,還能通過UIFont
設置顯示的大小。
2侄榴、UILabel
這個控件就不用多說了雹锣,直接有個屬性名字叫attributedText,設上result
就能顯示了癞蚕。
本文就到此結束了??????????蕊爵。。桦山。攒射。醋旦。