UITextview typingAttributes not working

那年仲夏 提交于 2020-01-02 13:55:11

问题


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;

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

what i'm doing wrong :(

update: i even tried

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];

when I tried

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

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


回答1:


The documentation clearly states that typingAttributes is for the editing mode of the text field.

typingAttributes

The attributes to apply to new text being entered by the user.

... If the text field is not in editing mode, this property contains the value nil. Similarly, you cannot assign a value to this property unless the text field is currently in editing mode.

Instead, you should assign attributedText instead of the text property. The mechanism to specify the attributes is via NSAttributedString that you assign.




回答2:


You may configure paragraph style in IB using temporary text in text view and setting all required attributes. Then you clear your text view on launch textView.text = nil. Text attributes should remain the same.




回答3:


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;
 }



回答4:


Here's one gotcha - as mentioned in other answers, the UITextField must be in editing mode for this to work. If you are running in the simulator, and the keyboard is hidden (cmd+k), the textfield is not in editing mode if you type using your computer's keyboard. Make sure the simulator's keyboard is displayed.




回答5:


Programmatically setting attributedText resets the typingAttributes. Set typingAttributes last.



来源:https://stackoverflow.com/questions/18817651/uitextview-typingattributes-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!