IOS High Imitation micro-letter expression Input function code sharing _ios

Source: Internet
Author: User

Recent project needs, to achieve a similar micro-letter expression input, so the micro-letter expression push lightly out, to achieve a. You can download the source code from here. Looks like the expression input does not have how many things, is simply uses the nstextattachment to realize the picture and text mixed row, the result in realizes the process to encounter many small problems, next will introduce one by one to meet the pit. First, the previous effect chart:

One, the realization expression Choice view (Wkexpressionview)

Specific implementation will not elaborate, the main function is to click the expression, the corresponding expression of the picture name to delegate.

Second, the realization expression TextView (Wkexpressiontextview)

Wkexpressiontextview inherits from Uitextview, provides
-(void) Setexpressionwithimagename: (NSString *) imagename fontsize: (cgfloat) FontSize method, used to insert an expression based on a picture. Specific implementation:

Rich Text 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];
Inserts a string at the current edit position [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];

Where Wkexpressiontextattachment inherits from Nstextattachment, and adds the text field, which is used for copy and paste operations in order to save the text for the expression.

@interface wkexpressiontextattachment:nstextattachment
@property (nonatomic, copy) NSString *text;
@end

Wkexpressiontool provides a way to convert ordinary strings to rich text, primarily for generating expressions when replicating.

Main methods

+ (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 range
ATINDEX: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; /** * Generate Rich Text through expression * * @param expressionstring expression name * @param fontsize corresponding Font size * * @return Rich Text/+ (nsattributedstring *) get Attributestringwithexpressionstring: (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; }

So far, the basic skills can be accomplished. Now, let's talk about the little problems that come up

Editors are supposed to correspond to Selectedrange

Copy and paste operations need to be implemented again

TextView after inserting nstextattachment, the font size is changed to 12 by default, and the default size needs to be recorded.

corresponding Selectedrange operation

Specific operations view the source code

Re-implement the copy, cut method

Copy, paste operation will find that the picture can not be copied, so you need to rewrite the copy, cut method

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

Record size of default font

Using the instance variable defaultfontsize, the self.font.pointSize is recorded when the Wkexpressiontextview is instantiated, and the size of the font is required to fetch the defaultfontsize directly.

@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;
}

The above is a small set to introduce the iOS high imitation micro-letter expression input function code to share, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.