Objective C, How to change line in UITextView
I am downloading UITextView contents from a web server and i开发者_Python百科t just returns text without any line feeds. I inserted \n
, \\n
, [CR]
, and more into the place I want to change the line but none of these worked. How can I change the line for the text in UITextView?
Weird. Works for me.
_subtypeField = [[UITextView alloc] initWithFrame:CGRectMake(28, 189, self.bounds.size.width-30, 100)];
[_subtypeField setFont:[UIFont fontWithName:@"Helvetica" size:13]];
[_subtypeField setText:@"Examples:\n- Fentanyl\n- Dilaudid 2 mg PO q 4 hours prn moderate pain"];
[_subtypeField setTextAlignment:UITextAlignmentLeft];
[_subtypeField setTextColor:[UIColor colorWithRed:0.494 green:0.647 blue:0.808 alpha:1.0]];
[_subtypeField setOpaque:NO];
[_subtypeField setBackgroundColor:[UIColor clearColor]];
[_subtypeField setUserInteractionEnabled:NO];
[self addSubview:_subtypeField];
Perhaps something goes wrong when you convert the C string into an NSString? Are the linefeeds still in the NSString?
If your text that you want to display is dynamic and the width of text view is fixed then you can first calculate the height of textview according to the text and then display that text in text view. This will automatically change the line in the text view
Code that I used:-
CGSize labelsize;
UITextView *commentsTextView = [[UITextView alloc] init];;
[commentsTextView setBackgroundColor:[UIColor clearColor]];
NSString *text=@"sample Text";//give your text
[commentsTextView setFont:[UIFont fontWithName:@"Helvetica"size:14]];
labelsize=[text sizeWithFont:commentsTextView.font constrainedToSize:CGSizeMake(268, 2000.0)];
commentsTextView.frame=CGRectMake(10, 24, 268, labelsize.height);
[self.view addSubview:commentsTextView];
[commentsTextView release];
精彩评论