textView 基本用法 设置行间距 设置文字大小重量 文字布局

本文详细介绍了如何使用 iOS 中的 UITextView 控件进行完全定制,包括文本编辑、样式设置、触摸事件处理以及代理方法的应用等,为开发者提供了一个全面的配置教程。

//初始化·

    self.textview = [[UITextView alloc] initWithFrame:CGRectMake(30, 100, KWIDTH - 60, KWIDTH)];

    //字体大小粗细

    self.textview.font = [UIFont systemFontOfSize:30 weight:20];

    //字体颜色

    self.textview.textColor = [UIColor redColor];

    //是否可以编辑

    self.textview.editable = YES;

    //文字显示方式

    self.textview.textAlignment = NSTextAlignmentRight;

    //背景颜色

    self.textview.backgroundColor = [UIColor whiteColor];

    //文本框的颜色·和宽度;

    self.textview.layer.cornerRadius = 6.0f;

    self.textview.layer.borderWidth = 5.0;

    self.textview.layer.borderColor = [UIColor blueColor].CGColor;

    //设置是否编辑文本框的属性

    self.textview.allowsEditingTextAttributes = YES;

    //是否允许滚动

    self.textview.scrollEnabled = YES;

    

    //设置文字大小及文字宽度

    self.textview.font = [UIFont systemFontOfSize:18.0 weight:16];

//设定行距

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    paragraphStyle.lineSpacing = 12; //行距

    NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:18], NSParagraphStyleAttributeName:paragraphStyle};

    self.textview.attributedText = [[NSAttributedString alloc]initWithString: self.textview.text attributes:attributes];

//添加到视图

    

    [self.view addSubview:self.textview];

    //添加代理;

    self.textview.delegate = self;


-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{

    NSLog(@"%s,",__FUNCTION__);

    return YES;

}

-(void)textViewDidBeginEditing:(UITextView *)textView{

    NSLog(@"%s,",__FUNCTION__);

    

}

-(BOOL)textViewShouldEndEditing:(UITextView *)textView{

    NSLog(@"%s,",__FUNCTION__);

    return YES;

}

-(void)textViewDidEndEditing:(UITextView *)textView{

    NSLog(@"%s,",__FUNCTION__);

    

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [self.textview resignFirstResponder];

}


-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{


    if ([text isEqualToString:@"\n"]) {

        [textView resignFirstResponder];

        return NO;

    }

    NSLog(@"%s,",__FUNCTION__);

    return YES;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值