Expression input micro-channel imitation

Oh wow, I have not written a blog, some time ago too busy. Recently the project needs to achieve a similar expression input micro letter, so the expression of micro-channel push lightly out, to achieve one. From here you can download the source code . Enter the expression seems not much left, is nothing more than a NSTextAttachment to achieve photo-text, the result of a lot of small problems encountered in the process of implementation, the following will be introduced one by one encountered the pit. First on a renderings:

effect

First, to achieve expression selecting View (WKExpressionView)

Concrete implementation will not elaborate, the main function is clicked expression, the expression of the corresponding picture name of the notification to delegate.

Second, to achieve expression textView (WKExpressionTextView)

WKExpressionTextView inherited from UITextView, provided 
- (void) setExpressionWithImageName: (NSString *) imageName fontSize: (CGFloat) fontSize method for inserting expression according to the picture. Implementation:

//富文本
    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];

 Wherein WKExpressionTextAttachment inherited from NSTextAttachment, and add the text field, in order to preserve the expression corresponding text for copy and paste operations.

@interface WKExpressionTextAttachment : NSTextAttachment

@property (nonatomic, copy) NSString *text;

@end

 WKExpressionTool provided to convert the rich text string ordinary method, primarily for generating a copy expression. 
The main method

+ (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         对应字体大小
 *
 *  @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;

}

 

Thus, the basic functions to achieve complete. Then talk about the problems encountered by small

  • Editing should correspond selectedRange
  • Copy and paste operations to reimplement
  • After insertion textView NSTextAttachment, modify the default font size is 12, the default size to be recorded

Operation corresponds selectedRange

Specific Actions View source

Re-implement copy, cut method

Copy, paste operations will find that the picture can not be copied, so the need to rewrite their own 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];
}

 

The default size font of record

Using the instance variables when DefaultFontSize, when recording self.font.pointSize WKExpressionTextView instantiation later need to take the size font, direct access 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;
}

 

Reproduced in: https: //www.cnblogs.com/pretty-guy/p/5427224.html

Guess you like

Origin blog.csdn.net/weixin_34082695/article/details/93438740