zoukankan      html  css  js  c++  java
  • UITextView 的使用

    直接上代码:

    //
    //  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:@"
    "]) {
            [textView resignFirstResponder];
            return NO;
        }
        return YES;
    }
    
    
    @end
  • 相关阅读:
    第二周教学设计反思
    uni-app,一套代码应用在多个平台
    新CMS
    学习js
    关于数据库
    自己写的一段jquery实现当滚动条滑动到某个位置实现向中间移动
    js平滑滚动到顶部、底部、指定地方
    统一一个帝国中搜索页面的模板
    帝国CMS的总结
    回到顶部的功能
  • 原文地址:https://www.cnblogs.com/mthoutai/p/7337825.html
Copyright © 2011-2022 走看看