Text is not showing in uitextview - iphone

I have added a textview on uiview. and when i am trying to enter data into it then its not showing.
Following in my code
questionView1 = [[UIView alloc]initWithFrame:CGRectMake(30, 40, 260, 350)];
[questionView1 setBackgroundColor:[UIColor blackColor]];
quesText1 = [[UITextView alloc] initWithFrame:CGRectMake(10, 20, 240, 270)];
quesText1.layer.borderWidth = 10.0f;
[quesText1 setText:#"Everything Ok"];
quesText1.layer.borderColor = [[UIColor grayColor] CGColor];
quesText1.tag = 100;
quesText1.delegate = self;
[questionView1 addSubview:quesText1];
its showing the default text but tryinng to enter data through keyboard then its not showing.
However, when i checked in its delegate(below) then its printing the correct value
- (void)textViewDidEndEditing:(UITextView *)textView;{
NSLog(#"%#",textView.text);\\ print correct value but value not showing in uitextview area.
}
Dnt know how and why its happening.. anyone knows?

once check this one ,
UIView* questionView1 = [[UIView alloc]initWithFrame:CGRectMake(30, 40, 260, 350)];
[questionView1 setBackgroundColor:[UIColor blackColor]];
UITextView* quesText1 = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 240, 270)];
quesText1.contentInset = UIEdgeInsetsMake(20.0, 0.0, 20.0, 0.0);
quesText1.layer.borderWidth = 10.0f;
quesText1.contentMode=UIViewContentModeCenter;
[quesText1 setText:#"Everything Ok"];
quesText1.layer.borderColor = [[UIColor grayColor] CGColor];
quesText1.tag = 100;
quesText1.delegate = self;
[questionView1 addSubview:quesText1];
[self.view addSubview:questionView1];
questionView1.userInteractionEnabled=YES;
set this line in your code
quesText1.contentInset = UIEdgeInsetsMake(20.0, 0.0, 20.0, 0.0);

Things to try out
Your background color is black and the textcolor will be black by
default.So set text color to white
The problem may be setting the border such that it covers the
default text

Please try this...
step1:
[self.view addSubView:questionView1];
step2:
adopt the protocol 'UITextViewDelegate' in your .h file and also put the following code
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
}
return YES;
}

Related

UITextView like in Mail.app reply view

In the iOS Mail app when you hit reply, you are presented with a text view like this. The vertical blue line keeps growing or shrinking (as the case may be) when you try to edit the previous reply while doing an inline reply or something. The top portion (your main reply) looks normal. Any idea on a really top level on how to pull this kind of text view off?
textViewHolder = [[UIView alloc]initWithFrame:CGRectMake(20, 100, 280, 20)];
simpleLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 2, 20)];
simpleLine.backgroundColor = [UIColor blueColor];
[textViewHolder addSubview:simpleLine];
myTextView = [[UITextView alloc]initWithFrame:CGRectMake(20, 0, 240, 24)];
myTextView.delegate = self;
[textViewHolder addSubview:myTextView];
[self.view addSubview:textViewHolder];
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
int numLines = myTextView.contentSize.height / myTextView.font.lineHeight;
if([text isEqualToString:#"\n"])
numLines++;
if(numLines>1)
{
CGRect frame = textViewHolder.frame;
frame.size.height = 20*(numLines-1);
textViewHolder.frame =frame;
CGRect frame2 = simpleLine.frame;
frame2.size.height = 20*(numLines-1);
simpleLine.frame =frame2;
CGRect frame3 = myTextView.frame;
frame3.size.height = 24*(numLines-1);
myTextView.frame =frame3;
}
return YES;
}
The (numLines-1) is because I always got one line more that I needed. I'm sure it's solvable with a bit debugging.
The [text isEqualToString:#"\n"] part is because you also want to increment the line number if the user presses done and going down a line.

UITextView - Horizontal Scrolling?

How can I create a horizontal scrolling UITextView?
When I set my text programmatically it adds automatic line breaks, so I can only scroll vertically...
titleView.text = #"this is a very long text. this is a very long text. this is a very long text. this is a very long text. this is a very long text.";
Thanks for your answers.
EDIT:
So far I tried this:
UIScrollview *yourScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0 ,0 , self.view.frame.size.width, 50)];
CGFloat textLength = [titleView.text sizeWithFont:titleView.font constrainedToSize:CGSizeMake(9999, 50) lineBreakMode:NSLineBreakByWordWrapping].width;
yourScrollview.contentSize = CGSizeMake(textLength + 200, 500); //or some value you like, you may have to try this out a few times
titleView.frame = CGRectMake(titleView.frame.origin.x, titleView.frame.origin.y, textLength, titleView.frame.size.height);
[yourScrollview addSubview: titleView];
NSLog(#"%f", textLength);
but I received: 'Threat 1: signal SIGABRT'
I have not yet done something like this, but I would try the following steps to accomplish this:
Create a UIScrollview *yourScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0 ,0 , self.view.frame.size.width, 50)]; //
Use CGFloat textLength = [titleView.text sizeWithFont:titleView.font constrainedToSize:CGSizeMake(9999, 50) lineBreakMode:NSLineBreakByWordWrapping].width;
to get the final length of your text
Set yourScrollView.contentSize = CGSizeMake(textLength + 20, 50); //or some value you like, you may have to try this out a few times
Also set titleTextView.frame = CGRectMake(titleTextView.frame.origin.x, titleTextView.frame.origin.y, textLength, titleTextView.frame.size.height);
Make titleView a subview of yourScrollView: [yourScrollView addSubview: titleView];
Hope this gives you a good start!
EDIT: This Code will work:
Please notice I used a UILabel instead of a UITextView.
UILabel *titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
titleView.text = #"this is a very long text. this is a very long text. this is a very long text. this is a very long text. this is a very long text.";
titleView.font = [UIFont systemFontOfSize:18];
titleView.backgroundColor = [UIColor clearColor];
titleView.numberOfLines = 1;
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
CGFloat textLength = [titleView.text sizeWithFont:titleView.font constrainedToSize:CGSizeMake(9999, 50) lineBreakMode:NSLineBreakByWordWrapping].width;
myScrollView.contentSize = CGSizeMake(textLength + 20, 50); //or some value you like, you may have to try this out a few times
titleView.frame = CGRectMake(titleView.frame.origin.x, titleView.frame.origin.y, textLength, titleView.frame.size.height);
[myScrollView addSubview: titleView];
[self.view addSubview:myScrollView];
[titleView release];
[myScrollView release];

Sizing UITextView to its content isn't working

Here's the code for a UITextView that I want to size to the height of its content.
If I write the textView.frame height explicitly like:
textView.frame = CGRectMake(100, 12, 320, 458);
the textView sizes to it's content as expected.
If, however, I write it like the following. It doesn't even display although the NSLog statement says that there's a value to textView.contentSize.height
UITextView *textView = [[UITextView alloc] init];
textView.layer.borderWidth = 5.0f;
textView.layer.borderColor = [[UIColor redColor] CGColor];
textView.text = [item objectForKey:#"description"];
textView.frame = CGRectMake(100, 12, 320, textView.contentSize.height);
NSLog(#"%f textviewcontnet size", textView.contentSize.height);
textView.editable = NO;
[self.view addSubview:textView];
When I log the output of:
NSLog(#"%f textviewcontent size", textView.contentSize.height);
I get "458.000000 textviewcontent size"
thanks for any help
I'd suggest trying:
UITextView *textView = [[UITextView alloc] init];
textView.layer.borderWidth = 5.0f;
textView.layer.borderColor = [[UIColor redColor] CGColor];
textView.text = [item objectForKey:#"description"];
textView.frame = CGRectMake(100, 12, 320, 458);
textView.editable = NO;
[self.view addSubview:textView];
textView.frame = CGRectMake(100, 12, 320, textView.contentSize.height);
I've heard that textView.contentSize.height doesn't work until it's been added to a view (though that's not my experience). More importantly, I don't know how it would interpret textView.contentSize.height if it doesn't yet know what the width of the control is. So go ahead, set the initial frame, do addSubview and then readjust the size based upon textView.contentSize.height.
Quickly copied out of one of my projects:
AppDelegate *appDelegate;
CGSize textSize1, textSize2;
appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
textSize1 = [self.subjectLabel.text sizeWithFont:[appDelegate fontNormal] constrainedToSize:CGSizeMake(300.0f, 10000.0f) lineBreakMode:NSLineBreakByWordWrapping];
self.subjectLabel.frame = CGRectMake(10, 5, 300, textSize1.height);
textSize2 = [self.descriptionLabel.text sizeWithFont:[appDelegate fontNormal] constrainedToSize:CGSizeMake(300.0f, 10000.0f) lineBreakMode:NSLineBreakByWordWrapping];
self.descriptionLabel.frame = CGRectMake(10, 5 + textSize1.height + 5, 300, textSize2.height);
[appDelegate fontNormal] just returns a UIFont object, the one that I am using for all "normal" text items. Don't worry about that too much. But it is important that you use the same font that is used for the text view too.
My example is a bit easier because it is a UILable. That works with a text view too but you will have to consider the insects. Easy solution, just substract some "fuzzy offset" from the width compared to the frame width of your text view.

How to increase the UITextView height and X axis value in iPhone?

Am working in an iPhone app using UITextView. I am trying to increase the height of UITextView based on it's content length. It is working fine. But, when the height of uitextview increase the UITextView should change the height in up side not increase height in down.
This is the code am trying in my project,
baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 50)];
baseView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:baseView];
TextView = [[UITextView alloc] initWithFrame:CGRectMake(20, 10, 280, 40)];
TextView.backgroundColor = [UIColor whiteColor];
TextView.layer.borderWidth = 2;
TextView.delegate = self;
TextView.font = [UIFont fontWithName:#"Helvetica" size:16];
[baseView addSubview: TextView];
-(void)textViewDidChange:(UITextView *)textView
{
CGRect frame = messageTextView.frame;
frame.size.height = messageTextView.contentSize.height;
TextView.frame = CGRectMake(20, 10, 280, frame.size.height);
baseView.frame = CGRectMake(0, 50, 320, frame.size.height+20);
}
When the UITextView height is changing the effect textview height will be increase on top side. Can anyone please help me on this? Thanks in advance.
In the method you change the frames, also edit the frame.origin.y value of the textView
-(void)textViewDidChange:(UITextView *)textView
{
CGRect frame = messageTextView.frame;
frame.size.height = messageTextView.contentSize.height;
TextView.frame = CGRectMake(20, 0, 280, frame.size.height); //previous y was 10
baseView.frame = CGRectMake(0, 50, 320, frame.size.height+20);
}
Nonetheless, I believe you are aware that you have only 10 pixels for the textView to go up, as you are adding it to a BaseView view.

Text appears after hitting the return key in a UITextField

I have a custom class to create a textview with lines like the Notes app from Apple.
This is how the class looks:
NoteView.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface NoteView : UITextView <UITextViewDelegate> {
}
#end
NoteView.m
#import "NoteView.h"
#implementation NoteView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor colorWithRed:1.0f green:1.0f blue:0.6f alpha:1.0f];
self.font = [UIFont fontWithName:#"Marker Felt" size:20];
}
return self;
}
- (void)drawRect:(CGRect)rect {
//Get the current drawing context
CGContextRef context = UIGraphicsGetCurrentContext();
//Set the line color and width
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.2f].CGColor);
CGContextSetLineWidth(context, 1.0f);
//Start a new Path
CGContextBeginPath(context);
//Find the number of lines in our textView + add a bit more height to draw lines in the empty part of the view
NSUInteger numberOfLines = (self.contentSize.height + self.bounds.size.height) / self.font.leading;
//Set the line offset from the baseline.
CGFloat baselineOffset = 6.0f;
//Iterate over numberOfLines and draw each line
for (int x = 0; x < numberOfLines; x++) {
//0.5f offset lines up line with pixel boundary
CGContextMoveToPoint(context, self.bounds.origin.x, self.font.leading*x + 0.5f + baselineOffset);
CGContextAddLineToPoint(context, self.bounds.size.width, self.font.leading*x + 0.5f + baselineOffset);
}
//Close our Path and Stroke (draw) it
CGContextClosePath(context);
CGContextStrokePath(context);
}
#end
I add the view as a subview. Everything works fine, but the text only apears after I hit the return key. I can see the flashing cursor, when I type I can see the cursor moving, but the text is gone... After hitting the return key, the text becomes visible and after that everything works fine. Even when I select all the text, remove it and start typing the text is visible.
This is how the textarea looks like:
How to solve this?
Thanks in advance!
UPDATE:
This is how I alloc the view:
annotateText = [[NoteView alloc] initWithFrame:CGRectMake(85.0, 168.0, 600.0, 567.0)];
annotateText.backgroundColor = [UIColor clearColor];
[annotateText setText:[annotationNotes objectAtIndex:0]];
[paperAnnotationView addSubview:annotateText];
I know this question is pretty old. I ran into the same issue and was looking for a solution. So I thought I would post the answer here, just in case, if any one comes and looks for it. After pulling my hair for hours, I figured out that the text view wont display the text in the very first line (unless we press enter/return or scroll the text) when the bounds of the textview is off the screen. Even when the parent view is off the screen, I ran into the same issue.
Here is the code that I was working on.
self.captionView = [[[UIImageView alloc] initWithFrame:CGRectMake(231, 769, 563, 643)] autorelease]; //63
self.captionView.image = [UIImage imageNamed:#"ekp006_preview_fbsharecontainer"];
self.captionView.userInteractionEnabled = YES;
//self.captionView.layer.contents = (id)[UIImage imageNamed:#"ekp006_preview_fbsharecontainer"].CGImage;
UIButton *cancelButton = [[[UIButton alloc] initWithFrame:CGRectMake(10, 10, 73, 34)] autorelease];
[cancelButton setImage:[UIImage imageNamed:#"ekp006_preview_fbshare_btn_cancel"] forState:UIControlStateNormal];
[cancelButton setImage:[UIImage imageNamed:#"ekp006_preview_fbshare_btn_cancel_down"] forState:UIControlStateHighlighted];
[cancelButton addTarget:self action:#selector(cancelFacebookShare) forControlEvents:UIControlEventTouchUpInside];
[self.captionView addSubview:cancelButton];
UIButton *shareButton = [[[UIButton alloc] initWithFrame:CGRectMake(480, 10, 73, 34)] autorelease];
[shareButton setImage:[UIImage imageNamed:#"ekp006_preview_fbshare_btn_share"] forState:UIControlStateNormal];
[shareButton setImage:[UIImage imageNamed:#"ekp006_preview_fbshare_btn_share_down"] forState:UIControlStateHighlighted];
[shareButton addTarget:self action:#selector(facebookshare) forControlEvents:UIControlEventTouchUpInside];
[self.captionView addSubview:shareButton];
self.captionTextView = [[[UITextView alloc] initWithFrame:CGRectMake(20, 60, 523, 100)] autorelease];
self.captionTextView.textColor = [UIColor lightGrayColor];
self.captionTextView.delegate = self;
self.captionTextView.layer.shadowRadius = 5.0;
self.captionTextView.layer.shadowColor = [UIColor blackColor].CGColor;
self.captionTextView.layer.shadowOpacity = 0.8;
self.captionTextView.layer.borderColor = [UIColor blackColor].CGColor;
self.captionTextView.layer.borderWidth = 0.75;
self.captionTextView.layer.cornerRadius = 5.0f;
self.captionTextView.font = [UIFont fontWithName:#"VAGRoundedBlackSSi" size:18];
[self.captionView addSubview:self.captionTextView];
[[self topMostViewController].view addSubview:self.captionView];
If you see closely, the parent view for me (the caption view) was 769 in y origin and I made this explicit, so that I can make the view slide from bottom to top using UIView animations. In order to solve this, I had to take the alternate path of making the parent view frame as CGRectMake(231,63,563,643) and hide the entire view. And to present it I used something like this.
- (void) presentCaptionCaptureScreen
{
// The code which works
[AnimationEffects bounceAnimationForView:self.captionView afterTimeInterval:0];
self.captionTextView.textColor = [UIColor lightGrayColor];
self.captionTextView.text = #"Enter your caption for the photo";
// The code which didn't work
/*
[UIView animateWithDuration:0.7 animations:^
{
self.captionTextView.text = #"";
self.captionView.frame = CGRectMake(231, 63, 563, 643);
} completion:^(BOOL finished)
{
self.captionTextView.textColor = [UIColor lightGrayColor];
self.captionTextView.text = #"Enter your caption for the photo";
}];
*/
}
It worked for me!