最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS高仿微信表情輸入功能代碼分享

 更新時間:2016年11月14日 16:41:35   作者:pretty guy  
最近項目需求,要實現(xiàn)一個類似微信的的表情輸入功能,今天小編抽空給大家分享iOS高仿微信表情輸入功能代碼,非常不錯,感興趣的朋友參考下吧

最近項目需求,要實現(xiàn)一個類似微信的的表情輸入,于是把微信的表情扒拉出來,實現(xiàn)了一把??梢詮倪@里下載源碼??雌饋肀砬檩斎霙]有多少東西,不外乎就是用NSTextAttachment來實現(xiàn)圖文混排,結(jié)果在實現(xiàn)的過程中遇到了很多小問題,接下來會一一介紹遇到過的坑。先上一張效果圖:

一、實現(xiàn)表情選擇View(WKExpressionView)

具體的實現(xiàn)就不細說了,主要功能就是點擊表情時,將對應(yīng)表情的圖片名稱通知給delegate。

二、實現(xiàn)表情textView(WKExpressionTextView)

WKExpressionTextView繼承自UITextView, 提供
- (void)setExpressionWithImageName:(NSString *)imageName fontSize:(CGFloat)fontSize方法,用于根據(jù)圖片插入表情。 具體實現(xiàn):

//富文本
WKExpressionTextAttachment *attachment = [[WKExpressionTextAttachment alloc] initWithData:nil ofType:nil];
UIImage *image = [UIImage imageNamed:imageName];
attachment.image = image;
attachment.text = [WKExpressionTool getExpressionStringWithImageName:imageName];
attachment.bounds = CGRectMake(0, 0, fontSize, fontSize);
NSAttributedString *insertAttributeStr = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *resultAttrString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
//在當前編輯位置插入字符串
[resultAttrString insertAttributedString:insertAttributeStr atIndex:self.selectedRange.location];
NSRange tempRange = self.selectedRange;
self.attributedText = resultAttrString;
self.selectedRange = NSMakeRange(tempRange.location + 1, 0);
[self.textStorage addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:_defaultFontSize]} range:NSMakeRange(0, self.attributedText.length)];
[self scrollRangeToVisible:self.selectedRange];
[self textChanged];

其中WKExpressionTextAttachment繼承自NSTextAttachment, 并新增text字段,為了保存表情對應(yīng)的文本,用于復制粘貼操作。

@interface WKExpressionTextAttachment : NSTextAttachment
@property (nonatomic, copy) NSString *text;
@end

WKExpressionTool的提供將普通字符串轉(zhuǎn)換為富文本的方法,主要用于復制時生成表情。

主要方法

+ (NSAttributedString *)generateAttributeStringWithOriginalString:(NSString *)originalString fontSize:(CGFloat)fontSize
{
NSError *error = NULL;
NSMutableAttributedString *resultAttrString = [[NSMutableAttributedString alloc] initWithString:originalString];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\[[a-zA-Z0-9\u4e00-\u9fa5]{1,}\\]" options:NSRegularExpressionAllowCommentsAndWhitespace error:&error];
NSArray *results = [regex matchesInString:originalString options:NSMatchingReportCompletion range:NSMakeRange(0, originalString.length)];
if (results) {
for (NSTextCheckingResult *result in results.reverseObjectEnumerator) {
NSRange resultRange = [result rangeAtIndex:0];
NSString *stringResult = [originalString substringWithRange:resultRange];
NSLog(@"%s %@\n", __FUNCTION__, stringResult);
NSAttributedString *expressionAttrString = [self getAttributeStringWithExpressionString:stringResult fontSize:fontSize];
[resultAttrString replaceCharactersInRange:resultRange withAttributedString:expressionAttrString];
}
}
return resultAttrString;
}

/**
* 通過表情生成富文本
*
* @param expressionString 表情名
* @param fontSize 對應(yīng)字體大小
*
* @return 富文本
*/
+ (NSAttributedString *)getAttributeStringWithExpressionString:(NSString *)expressionString fontSize:(CGFloat)fontSize
{
NSString *imageName = [self getExpressionStringWithImageName:expressionString];
WKExpressionTextAttachment *attachment = [[WKExpressionTextAttachment alloc] initWithData:nil ofType:nil];
UIImage *image = [UIImage imageNamed:imageName];
attachment.image = image;
attachment.text = [WKExpressionTool getExpressionStringWithImageName:imageName];
attachment.bounds = CGRectMake(0, 0, fontSize, fontSize);
NSAttributedString *appendAttributeStr = [NSAttributedString attributedStringWithAttachment:attachment];
return appendAttributeStr;
}

至此,基本功能實現(xiàn)完成。 接下來說說遇到的小問題

編輯是應(yīng)該對應(yīng)selectedRange

復制粘貼操作需要重新實現(xiàn)

textView在插入NSTextAttachment后,會默認把font的size修改為12,需要記錄默認的size

對應(yīng)selectedRange操作

具體的操作查看源碼

重新實現(xiàn)copy、cut方法

進行復制、粘貼操作會發(fā)現(xiàn),不能對圖片進行復制,所以需要自己重寫copy、cut方法

- (void)copy:(id)sender
{
NSAttributedString *selectedString = [self.attributedText attributedSubstringFromRange:self.selectedRange];
NSString *copyString = [self parseAttributeTextToNormalString:selectedString];
UIPasteboard *pboard = [UIPasteboard generalPasteboard];
if (copyString.length != 0) {
pboard.string = copyString;
}
}
- (void)cut:(id)sender
{
[self copy:sender];
NSMutableAttributedString *originalString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
[originalString deleteCharactersInRange:self.selectedRange];
self.attributedText = originalString;
NSLog(@"--%@", NSStringFromRange(self.selectedRange));
[self textChanged];
}

記錄默認font的size

利用實例變量defaultFontSize,在WKExpressionTextView實例化時記錄self.font.pointSize,以后需要取font的size時,直接取defaultFontSize

@interface WKExpressionTextView : UITextView
@property (nonatomic, assign) CGFloat defaultFontSize;
@end
@implementation WKExpressionTextView
{
CGFloat _defaultFontSize;
}
- (void)awakeFromNib
{
[self setup];
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (void)setup
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange:) name:UITextViewTextDidChangeNotification object:self];
_defaultFontSize = self.font.pointSize;
self.delegate = self;
}

以上所述是小編給大家介紹的iOS高仿微信表情輸入功能代碼分享,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論

唐山市| 晋中市| 云南省| 固镇县| 巨鹿县| 垦利县| 九龙坡区| 社旗县| 黔西县| 涿州市| 桦川县| 浦县| 固原市| 兴化市| 浦东新区| 大悟县| 柘城县| 徐汇区| 长汀县| 衡阳市| 布拖县| 乌审旗| 习水县| 绥阳县| 上蔡县| 修武县| 泰和县| 错那县| 宕昌县| 灵山县| 柏乡县| 白沙| 永城市| 天津市| 康定县| 天气| 汉川市| 普安县| 肃宁县| 阿合奇县| 米易县|