Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ZIMInputToolbar/Classes/ZIMExpandingTextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ typedef NS_ENUM(int16_t, ZIMExpandingTextViewVerticalAlign) {
- (BOOL) hasText;
- (void) scrollRangeToVisible:(NSRange)range;
- (void) clearText;
- (void) replaceString:(NSString *)str withObjectFromString:(NSString *)stringToConvert;
- (void) replaceString:(NSString *)str inRange:(NSRange)range withObjectFromString:(NSString *)stringToConvert;
- (NSString *) getLastEnteredWord;
- (void) addReplacementString:(NSString *)rString toReplaceString:(NSString *)sString;
- (void) removeAllReplacementStrings;
- (void) replaceLastEnteredWordWithUserName:(NSString *)userName andUserId:(NSString *)userId;

@end
71 changes: 57 additions & 14 deletions ZIMInputToolbar/Classes/ZIMExpandingTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ - (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer prop

@interface ZIMExpandingTextView()

@property (strong, nonatomic) NSMutableDictionary *replacementStringsDictionary;
@property (strong, nonatomic) UITextRange *lastEnteredWordRange;
@end

@implementation ZIMExpandingTextView
Expand Down Expand Up @@ -90,6 +92,7 @@ - (id)initWithFrame:(CGRect)frame
self.textViewBackgroundImage = [[UIImageView alloc] initWithFrame:backgroundFrame];
self.internalTextView = [[UITextView alloc] initWithFrame:self.textViewBackgroundImage.bounds];
self.placeholderLabel = [[UILabel alloc] initWithFrame: textViewFrame];
self.replacementStringsDictionary = [[NSMutableDictionary alloc] init];

/* Custom Background image */
UIImage *textViewBackgroundImage = nil;
Expand Down Expand Up @@ -487,33 +490,59 @@ - (BOOL) resignFirstResponder {
-(void) setText:(NSString *)atext {
if (atext) {
self.internalTextView.attributedText = [[NSAttributedString alloc] initWithString:atext];
if(atext.length > 0) {
for (NSString *keyString in self.replacementStringsDictionary.allKeys) {
[self replaceString:keyString withObjectFromString:self.replacementStringsDictionary[keyString]];
}
}
[self performSelector:@selector(textViewDidChange:) withObject:self.internalTextView];
}
}

- (void) replaceString:(NSString *)str withObjectFromString:(NSString *)stringToConvert {
NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithAttributedString:self.internalTextView.attributedText];
NSRange range = [self.internalTextView.attributedText.string rangeOfString:str options: NSBackwardsSearch];
[[attrText mutableString] replaceOccurrencesOfString:str withString:@"" options:NSCaseInsensitiveSearch range:range];
UIImage *image = [self imageFromString:stringToConvert];
InlineTextAttachment *attch = [[InlineTextAttachment alloc] initWithData:nil ofType:nil];
UIFont *font = self.internalTextView.font;
attch.fontDescender = font.descender;
attch.image = image;
attch.realText = [@"@" stringByAppendingString:stringToConvert];
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attch];
[attrText appendAttributedString:attachmentString];
self.internalTextView.attributedText = [attrText copy];
[self performSelector:@selector(textViewDidChange:) withObject:self.internalTextView];
while (range.location != NSNotFound) { //if the text filled from mesageDraft - the whole text should be analyzed for replacements, not only the last word
NSMutableAttributedString *aTextMutableString = [self.internalTextView.attributedText mutableCopy];

UIImage *image = [self imageFromString:stringToConvert];
InlineTextAttachment *attch = [[InlineTextAttachment alloc] initWithData:nil ofType:nil];
UIFont *font = self.internalTextView.font;
attch.fontDescender = font.descender;
attch.image = image;
attch.realText = str;
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attch];

[aTextMutableString replaceCharactersInRange:[self.internalTextView.attributedText.string rangeOfString:str options: nil] withAttributedString:attachmentString];
self.internalTextView.attributedText = [aTextMutableString copy];
range = [self.internalTextView.attributedText.string rangeOfString:str options: nil];
[self performSelector:@selector(textViewDidChange:) withObject:self.internalTextView];
}
}

- (NSString *)getLastEnteredWord
{
NSRange cursorPosition = [self.internalTextView selectedRange];
NSMutableCharacterSet *workingSet = [[NSCharacterSet whitespaceAndNewlineCharacterSet] mutableCopy];
NSString *internalText = self.internalTextView.attributedText.string;
NSRange newRange = [self.internalTextView.attributedText.string rangeOfCharacterFromSet:workingSet
options:NSBackwardsSearch
range:NSMakeRange(0, cursorPosition.location)];
//The below code could probably be done in a better way
UITextPosition *beginning = self.internalTextView.beginningOfDocument;
UITextPosition *start = [self.internalTextView positionFromPosition:beginning offset:cursorPosition.location];
UITextPosition *end = [self.internalTextView positionFromPosition:beginning offset:newRange.location+1];
self.lastEnteredWordRange = [self.internalTextView textRangeFromPosition:end toPosition:start];

NSString* lastWord = [self.internalTextView textInRange:self.lastEnteredWordRange];
return lastWord;
}

- (UIImage *)imageFromString:(NSString *)string
{
NSDictionary *attrDict = [NSDictionary dictionaryWithObject:[UIFont boldSystemFontOfSize:12.0f] forKey:NSFontAttributeName];
CGSize size = [string sizeWithAttributes:attrDict];
// Create a bitmap context into which the text will be rendered.
UIGraphicsBeginImageContext(size);
// Render the text
UIGraphicsBeginImageContextWithOptions(size, NO, 2.0);
[string drawAtPoint:CGPointMake(0.0, 0.0) withAttributes:attrDict];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Expand Down Expand Up @@ -541,6 +570,20 @@ - (NSString *) text {
return attrString.string;
}

- (void) replaceLastEnteredWordWithUserName:(NSString *)userName andUserId:(NSString *)userId{
[self.internalTextView replaceRange:self.lastEnteredWordRange withText:userId];
[self addReplacementString:userId toReplaceString:userName];
[self replaceString:userId withObjectFromString:userName];
}

- (void) addReplacementString:(NSString *)value toReplaceString:(NSString *)key {
[self.replacementStringsDictionary setObject:value forKey:key];
}

- (void) removeAllReplacementStrings {
[self.replacementStringsDictionary removeAllObjects];
}

- (void) substringToRange:(NSRange)range {
self.internalTextView.attributedText = [self.internalTextView.attributedText attributedSubstringFromRange:NSMakeRange(0, range.location)];
}
Expand Down