UITextview TypingAttributes 不起作用 [英] UITextview typingAttributes not working

查看:50
本文介绍了UITextview TypingAttributes 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 UITextView,我想将它的行高设置为 50.0f,所以我使用了 TypingAttributes,但没有任何效果,我的代码在 ViewDidAppear 方法中是这样的

I have UITextView and I want to set it's line height to 50.0f so I'm using typingAttributes, but nothing works, my code goes like this in ViewDidAppear Method

UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight        = 50.0f;
paragraphStyle.lineHeightMultiple       = 50.0f;
paragraphStyle.maximumLineHeight        = 50.0f;

NSDictionary *textViewAttributeDic = [NSDictionary dictionaryWithObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
textView.typingAttributes  = textViewAttributeDic;

文本不受设置typingAttributes的影响,我尝试使用typingAttributes更改颜色和字体但没有任何效果

text doesn't effected by setting typingAttributes,and I tried to changed the color and font using typingAttributesbut nothing works

我已阅读所有堆栈答案和文档

i've read all stack answers and documentation

我做错了什么:(

更新:我什至试过

UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight        = 50.0f;
paragraphStyle.lineHeightMultiple       = 50.0f;
paragraphStyle.maximumLineHeight        = 50.0f;

NSDictionary *textViewAttributeDic = [NSDictionary dictionaryWithObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
textView.attributedText  = [[NSAttributedString alloc] initWithString:@"" attributes:textViewAttributeDic];

当我尝试

textView.attributedText  = [[NSAttributedString alloc] initWithString:@"blahblah" attributes:textViewAttributeDic];

它有效,但我需要没有空格或废话"字符的空 textView

It worked, but i need empty textView with no spaces or 'blah' characters

推荐答案

在ios6中我是这样解决的,在textView delegate中我写道:

in ios6 I solved it like this , in textView delegate I wrote:

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

     // 1st save current selected range ... we gonna use this value in 5th step
     NSRange textViewCurrentRange =  textView.selectedRange;

     // 2nd disable scrolling in textview
     [textView setScrollEnabled:NO];

     // 3rd set the new enterd text as attributed string to textview
     [textView setAttributedText:[[NSAttributedString alloc]initWithString:textView.text attributes:self.textAttributes]];

     // 4th enable scrolling
     [textView setScrollEnabled:YES];

     // 5th re set selected range so text inidicator will get back to it's place
     textView.selectedRange = textViewCurrentRange;
 }

这篇关于UITextview TypingAttributes 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆