iOS设置textView的行间距

来源:互联网 发布:cocostudio 1.6 mac 编辑:程序博客网 时间:2024/05/21 03:24

- (void)viewDidLoad {

    [superviewDidLoad];

    

    

   UITextView *textView = [[UITextViewalloc] initWithFrame:CGRectMake(50,200, 150, 250)];

    textView.delegate =self;

    textView.layer.borderWidth =1.0f;

    textView.layer.borderColor = [UIColorblackColor].CGColor;

    

//    1.静态显示textView的内容为设置的行间距,执行如下代码:

    

    //    textview 改变字体的行间距

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

//    paragraphStyle.lineSpacing = 5;// 字体的行间距

//    

//    NSDictionary *attributes = @{

//                                 NSFontAttributeName:[UIFont systemFontOfSize:15],

//                                 NSParagraphStyleAttributeName:paragraphStyle

//                                 };

//    textView.attributedText = [[NSAttributedString alloc] initWithString:@"1、输入你的内容,如果只是静态显示textView的内容为设置的行间距,执行如下代码。2、输入你的内容,如果只是静态显示textView的内容为设置的行间距,执行如下代码" attributes:attributes];


    

    [self.viewaddSubview: textView];

    

}


//    2.如果是想在输入内容的时候就按照设置的行间距进行动态改变,那就需要将上面代码放到textViewdelegate方法里

-(void)textViewDidChange:(UITextView *)textView

{

    //    textview 改变字体的行间距

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStylealloc] init];

    

    paragraphStyle.lineSpacing =15;// 字体的行间距

    

   NSDictionary *attributes = @{

                                 

                                NSFontAttributeName:[UIFontsystemFontOfSize:17],

                                 

                                NSParagraphStyleAttributeName:paragraphStyle

                                 

                                };

    

    textView.attributedText = [[NSAttributedStringalloc] initWithString:textView.textattributes:attributes];

    

}

0 0
原创粉丝点击