UI控件(UITextView)

复制代码
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //UITextView与UITextField主要区别:
    //1、UITextView支持多行而UITextField只能是单行;
    //2、UITextView继承UIScrollView,而后者继承至UIController
    
    UITextView* textView = [[UITextView alloc] init];
    //注意:bounds的x、y起点都是0
    textView.frame = self.view.bounds;
    
    //实现协议UITextViewDelegate
    textView.delegate = self;
    
    //autoresizingMask是UIView就有的一个属性,用以调整子视图与父视图的宽高
    //    enum {
    //        UIViewAutoresizingNone                 = 0,
    //        UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    //        UIViewAutoresizingFlexibleWidth        = 1 << 1,
    //        UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    //        UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    //        UIViewAutoresizingFlexibleHeight       = 1 << 4,
    //        UIViewAutoresizingFlexibleBottomMargin = 1 << 5
    //    };
    //  UIViewAutoresizingNone就是不自动调整。
    //  UIViewAutoresizingFlexibleLeftMargin    自动调整与父视图左边的距离,保证与父视图右边的距离不变。
    //  UIViewAutoresizingFlexibleRightMargin   自动调整与父视图的右边距离,保证与父视图左边的距离不变。
    //  UIViewAutoresizingFlexibleTopMargin     自动调整与父视图顶部的距离,保证与父视图底部的距离不变。
    //  UIViewAutoresizingFlexibleBottomMargin  自动调整与父视图底部的距离,保证与与父视图顶部的距离不变。
    //  UIViewAutoresizingFlexibleWidth         自动调整自己的宽度,保证与父视图左边和右边的距离不变。
    //  UIViewAutoresizingFlexibleHeight        自动调整自己的高度,保证与父视图顶部和底部的距离不变。
    
    //本例子为自适应高宽
    textView.autoresizingMask =
    UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    //是否可以编辑
    textView.editable = YES;
    
    textView.backgroundColor = [UIColor whiteColor];
    textView.textColor = [UIColor blueColor];
    textView.font = [UIFont fontWithName:@"Arial" size:18.0];
    textView.text = @"\n第1行\n第2行\n第3行\n";
    
    [self.view addSubview:textView];

}

#pragma mark - UITextView Delegate Methods
//文字改变时
- (void)textViewDidChange:(UITextView *)textView {
    NSLog(@"textViewDidChange:%@", textView.text);
}

//此时回车将作为提交
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSLog(@"shouldChangeTextInRange:%@",text);
    
    if ([text isEqualToString:@"\n"]) {
        //第一响应对象是窗口中,应用程序认为最适合处理事件的对象
        //当文本框放弃第一响应对象,则软键盘退出
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

@end
复制代码

 

posted @   Fredric_2013  阅读(172)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示