Thursday 30 April 2015

Drag and Drop View

#import "DragView.h"
@implementation DragView
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    CGPoint touchPoint = [[touches anyObject] locationInView:self];
    CGPoint previousPoint = [[touches anyObject] previousLocationInView:self];
    CGFloat xChange=touchPoint.x-previousPoint.x;
    CGFloat yChange=touchPoint.y-previousPoint.y;
    float newX=self.frame.origin.x+xChange;
    float newY=self.frame.origin.y+yChange;
    self.frame=CGRectMake(newX, newY, self.frame.size.width, self.frame.size.height);

}
@end

Friday 17 April 2015

UITextView detect hashtag

// TextView Delegate Function

- (void)textViewDidChange:(UITextView *)textView{
    NSLog(@"textViewDidChange:");

    textView.scrollEnabled = NO;
    NSRange selectedRange = textView.selectedRange;
    NSString *text = textView.text;

    // This will give me an attributedString with the base text-style
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];

    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"#(\\w+)" options:0 error:&error];
    NSArray *matches = [regex matchesInString:text
                                      options:0
                                        range:NSMakeRange(0, text.length)];

    for (NSTextCheckingResult *match in matches)
    {
        NSRange matchRange = [match rangeAtIndex:0];
        [attributedString addAttribute:NSForegroundColorAttributeName
                                 value:[UIColor blueColor]
                                 range:matchRange];
    }

    textView.attributedText = attributedString;
    textView.selectedRange = selectedRange;
    textView.scrollEnabled = YES;

}