Ios UITextview类型属性不工作
我有UITextView,我想将它的行高设置为50.0f,所以我使用的是typingAttributes,但没有任何效果,我的代码在ViewDidDisplay方法中是这样的Ios UITextview类型属性不工作,ios,uitextview,nsattributedstring,Ios,Uitextview,Nsattributedstring,我有UITextView,我想将它的行高设置为50.0f,所以我使用的是typingAttributes,但没有任何效果,我的代码在ViewDidDisplay方法中是这样的 UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] i
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更改颜色和字体,但没有任何效果
我已经阅读了所有的堆栈答案和文档
我做错了什么:(
更新:
我甚至试过
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];
它起作用了,但我需要没有空格或“blah”字符的空textView文档明确指出,
键入属性
用于文本字段的编辑模式
键入属性
应用于用户输入的新文本的属性
。。。
如果文本字段未处于编辑模式,则此属性包含值nil。同样,除非文本字段当前处于编辑模式,否则无法为此属性赋值
相反,您应该指定
attributedText
而不是text
属性。指定属性的机制是通过您指定的NSAttributedString
。您可以在IB中使用文本视图中的临时文本配置段落样式,并设置所有必需的属性。然后在la上清除文本视图unchtextView.text=nil
。文本属性应保持不变
在ios6中,我这样解决了它,在textView委托中,我写道:
- (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;
}
通过编程设置
attributedText
重置typingAttributes
。最后设置typingAttributes
。这里有一个问题-如其他答案所述,UITextField
必须处于编辑模式才能工作。如果您在模拟器中运行,并且键盘处于隐藏状态(cmd+k),如果使用计算机键盘键入,则文本字段未处于编辑模式。请确保显示模拟器的键盘。我看不出空文本字段如何显示错误的属性。设置“blahblah”属性字符串后,请尝试textView.text=nil
。