欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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

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

WKExpressionTextView繼承自UITextView, 提供
- (void)setExpressionWithImageName:(NSString *)imageName fontSize:(CGFloat)fontSize方法,用于根據(jù)圖片插入表情。 具體實(shí)現(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];
//在當(dāng)前編輯位置插入字符串
[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字段,為了保存表情對(duì)應(yīng)的文本,用于復(fù)制粘貼操作。

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

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

主要方法

+ (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;
}

/**
* 通過(guò)表情生成富文本
*
* @param expressionString 表情名
* @param fontSize 對(duì)應(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;
}

至此,基本功能實(shí)現(xiàn)完成。 接下來(lái)說(shuō)說(shuō)遇到的小問(wèn)題

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

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

textView在插入NSTextAttachment后,會(huì)默認(rèn)把font的size修改為12,需要記錄默認(rèn)的size

對(duì)應(yīng)selectedRange操作

具體的操作查看源碼

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

進(jìn)行復(fù)制、粘貼操作會(huì)發(fā)現(xiàn),不能對(duì)圖片進(jìn)行復(fù)制,所以需要自己重寫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];
}

記錄默認(rèn)font的size

利用實(shí)例變量defaultFontSize,在WKExpressionTextView實(shí)例化時(shí)記錄self.font.pointSize,以后需要取font的size時(shí),直接取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高仿微信表情輸入功能代碼分享,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論