mthoutai

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

直接上代码:

//
//  RootViewController.m
//  UIText_test
//
//

#import "RootViewController.h"
#import <QuartzCore/QuartzCore.h>    /// 用户视觉反馈


@interface RootViewController ()<UITextViewDelegate>

@property (nonatomic, strong) UIToolbar *toolBar ;
@property (nonatomic, strong) UITextView *textView ;

@end

@implementation RootViewController

- (UIToolbar *)toolBar {
    if (!_toolBar) {
        self.toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 375, 30)];
        [_toolBar setBarStyle:UIBarStyleBlack];
        UIBarButtonItem *oneButton = [[UIBarButtonItem alloc] initWithTitle:@"開始" style:UIBarButtonItemStyleDone target:self action:nil];

        UIBarButtonItem *twoButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];

        UIBarButtonItem *threeButton = [[UIBarButtonItem alloc] initWithTitle:@"完毕" style:UIBarButtonItemStyleDone target:self action:@selector(handleThreeBtn)];

        NSArray *buttonArray = [NSArray arrayWithObjects:oneButton, twoButton, threeButton, nil];
        [_toolBar setItems:buttonArray]; // 加入到 toolBar 上
    }
    return _toolBar;
}

- (UITextView *)textView {
    if (!_textView) {
        self.textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 200, 375, 190)];
        _textView.textColor = [UIColor redColor];
        _textView.font = [UIFont systemFontOfSize:20];
        _textView.delegate = self;
        [_textView.layer setCornerRadius:10];  // 设置成圆角
        _textView.backgroundColor = [UIColor cyanColor];
        _textView.text = @"sfjlegjklaeyg a";
        _textView.scrollEnabled = YES;
        _textView.autoresizingMask = UIViewAutoresizingFlexibleHeight ; // 自适应高度
        [_textView setInputAccessoryView:self.toolBar];  // 设置辅助视图
    }
    return _textView;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.textView];

    // 自己定义菜单项
    UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"分享到新浪微博" action:@selector(changeColor)];
    UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu setMenuItems:[NSArray arrayWithObject:menuItem]]; //加入
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Action -

- (void)handleThreeBtn {
    [self.textView resignFirstResponder];    // 撤销第一次响应
}

- (void)changeColor {
    self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
}

- (void)handleDone {
    [self.textView resignFirstResponder]; // 撤销第一响应
}

#pragma mark - UITextViewDelegate -

// 完毕開始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView {
    UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(handleDone)];
    self.navigationItem.rightBarButtonItem = done;
}

// 完毕结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView {
    self.navigationItem.rightBarButtonItem = nil;   /// 导航条按钮设置为空
}

// 当文本发生改变时
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqual:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}


@end
posted on 2017-08-10 10:21  mthoutai  阅读(275)  评论(0编辑  收藏  举报