Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. keicoder created this gist Mar 15, 2014.
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,220 @@
    //making UITextView programmatically with custom class and UITextViewDelegate


    //1. make basic UITextView programmatically with custom class


    //AppDelegate.h

    @interface AppDelegate : UIResponder <UIApplicationDelegate>

    @property (strong, nonatomic) UIWindow *window;

    @end


    //AppDelegate.m

    @implementation AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
    return YES;
    }

    @end



    //JTextView.h

    @interface JTextView : UITextView

    @property (strong, nonatomic) UITextView *textView;

    @end



    //JTextView.m

    @implementation JTextView

    //in case making JTextView in code
    - (id)initWithFrame:(CGRect)frame
    {
    if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
    self = [super initWithFrame:frame];
    if (self)
    {
    [self setup];
    }
    return self;
    }


    //in case making JTextView in storyboard
    - (id)initWithCoder:(NSCoder *)aDecoder
    {
    if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
    self = [super initWithCoder:aDecoder];

    if (self)
    {
    [self setup];
    }
    return self;
    }


    - (void)setup
    {
    if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
    CGRect applicationBounds = [[UIScreen mainScreen] bounds]; //Bounds: 0, 0, 320, 568
    NSLog (@"applicationBounds: %f, %f, %f, %f", applicationBounds.origin.x, applicationBounds.origin.y, applicationBounds.size.width, applicationBounds.size.height);

    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; //Frame: : 0, 20, 320, 548
    CGFloat statusBarOffset = 20.0; //iOS7

    //텍스트 뷰 생성
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0.0, statusBarOffset, applicationFrame.size.width, applicationFrame.size.height)]; //Frame: : 0, 20, 320, 548
    CGRect textViewFrame = self.textView.frame;
    NSLog (@"textViewFrame: %f, %f, %f, %f", textViewFrame.origin.x, textViewFrame.origin.y, textViewFrame.size.width, textViewFrame.size.height);

    UIEdgeInsets textViewInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0); //top, left, bottom, right
    self.textView.contentInset = textViewInsets;
    self.textView.scrollIndicatorInsets = textViewInsets;

    //텍스트 뷰 속성
    self.textView.text = @"Text View...";
    self.textView.font = [UIFont systemFontOfSize:14.0];
    self.textView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.93 alpha:1];
    self.textView.scrollEnabled = YES;
    self.textView.alwaysBounceVertical = YES;
    self.textView.editable = YES;
    self.textView.clipsToBounds = YES;
    self.textView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
    self.textView.keyboardType = UIKeyboardTypeDefault;

    CALayer *imageLayer = self.textView.layer;
    [imageLayer setCornerRadius:10];
    [imageLayer setBorderWidth:2];
    imageLayer.borderColor=[[UIColor lightGrayColor] CGColor];
    }


    @end



    //NoteViewController.h

    @interface NoteViewController : UIViewController

    @end



    //NoteViewController.m

    #import "JTextView.h"

    @interface NoteViewController () <UITextViewDelegate>
    {
    UITextView *_textView;
    }

    @end

    @implementation NoteViewController


    #pragma mark - View Life Cycle

    - (void)viewDidLoad
    {
    if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
    [super viewDidLoad];

    //뷰 속성
    self.view.backgroundColor = [UIColor colorWithRed:0.99 green:0.4 blue:0.43 alpha:1];

    //텍스트 뷰 생성
    JTextView *jTextView = [[JTextView alloc] init];
    _textView = jTextView.textView;
    [self.view addSubview:_textView];

    _textView.delegate = self;
    }


    #pragma mark - UITextView delegate method (optional)

    - (BOOL)textViewShouldBeginEditing:(UITextView *)textView
    {
    if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
    return YES;
    }


    - (void)textViewDidBeginEditing:(UITextView *)textView
    {
    if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
    _textView.backgroundColor = [UIColor colorWithRed:0.65 green:0.8 blue:1 alpha:1];
    }


    - (BOOL)textViewShouldEndEditing:(UITextView *)textView
    {
    if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
    return YES;
    }


    - (void)textViewDidEndEditing:(UITextView *)textView
    {
    if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
    _textView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.93 alpha:1];
    }


    //Each time a user types a character on the keyboard, just before the character is displayed,
    //the method textView:shouldChangeCharactersInRange:replacementString is called
    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
    {
    if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
    if ([text isEqualToString:@"\n"]){
    [_textView resignFirstResponder];
    return NO;
    }
    return YES;
    }


    //called when a section of text is selected or the selection is changed, such as when copying or pasting a section of text
    - (void)textViewDidChangeSelection:(UITextView *)textView
    {
    if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
    }


    - (void)textViewDidChange:(UITextView *)textView
    {
    if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
    }



    @end