How to increase the UITextView height and X axis value in iPhone? - 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.

Related

Text is not showing in uitextview

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

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.

Adding a label to a scroll view

I am trying to add a label to a scroll view, then set the contentSize of the scrollview to match the desired size of the label. The problem is I seem to have to make the contentSize bigger than the size of the label:
theScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320,480)];
bodyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320.0, 50.0)];
bodyLabel.textColor = [UIColor whiteColor];
bodyLabel.backgroundColor = [UIColor clearColor];
bodyLabel.textAlignment = UITextAlignmentLeft;
bodyLabel.text = event.description; <- basically some long ass text here
bodyLabel.font=[UIFont systemFontOfSize:16.0];
bodyLabel.numberOfLines = 0;
bodyLabel.lineBreakMode = UILineBreakModeWordWrap;
here I think Im getting the necessary label size to contain the text
CGSize size = [event.description sizeWithFont:bodyLabel.font constrainedToSize:CGSizeMake(bodyLabel.frame.size.width, 9500) lineBreakMode:bodyLabel.lineBreakMode];
frame = bodyLabel.frame; // to get the width
frame.size.height = size.height;
bodyLabel.frame = frame;
here I set the contentSize to the label's Size, but it isn't big enough, I need to
set it to bodyLabel.frame.size.height + 80 for it to be covered in the scrolling.
theScrollview.contentSize = CGSizeMake(bodyLabel.frame.size.width, bodyLabel.frame.size.height);
[theScrollview addSubview:bodyLabel];
Thanks for any thoughts in advance!
[theScrollview addSubView: bodyLabel];

center custom title in UINavigationBar?

I have a custom UINavigationBar title and a custom back button.
My problem is that the title is not centered on the iPhone.
It is as if my back button is pushing the title over to the right. Any Idea how I can center it?
int height = self.navigationController.navigationBar.frame.size.height;
int width = self.navigationController.navigationBar.frame.size.width;
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width + 300, 20)];
navLabel.backgroundColor = [UIColor whiteColor];
navLabel.textColor = [UIColor redColor];
navLabel.font = [UIFont fontWithName:#"Helvetica" size:30];
navLabel.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = navLabel;
[navLabel release];
((UILabel *)self.navigationItem.titleView).text = self.title;
Thanks!
edit I removed superfluous code and added this picture:
Notice how the title is pushed over to accomodate the button....
iOS is doing this because the frame you initialize is alway 300+width pixels. It is trying to center the full frame, the frame is larger than the space it wants to fit it in (because of the button) and therefore your label gets pushed to the right.
What you need to do is give the Frame of the navLabel the minimum size it needs.
So if your text is only 100px wide, but the frame is 400px, then iOS is trying to center the 400px inside the Navigation header, and doesn't have enough space. When you set the size to the actual 100px that is needed, iOS will center your header correctly, because there is plenty of space to center 100px.
The code snippet below should help you to detect the minimum size your frame needs, depending on the font and the text you try to put in.
Make sure the frame of the label is as small as possible, but does not exceed the max width.
(the width of the navigation bar).
UIFont* titleFont = [UIFont fontWithName:#"Helvetica" size:30];
CGSize requestedTitleSize = [titleText sizeWithAttributes:#{NSFontAttributeName: titleFont}];
CGFloat titleWidth = MIN(maxTitleWidth, requestedTitleSize.width);
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleWidth, 20)];
navLabel.backgroundColor = [UIColor whiteColor];
navLabel.textColor = [UIColor redColor];
navLabel.font = [UIFont fontWithName:#"Helvetica" size:30];
navLabel.textAlignment = NSTextAlignmentCenter;
navLabel.text = titleText;
self.navigationItem.titleView = navLabel;
Swift 5.0
let titleFont = UIFont.systemFont(ofSize: 17.0)
let title = "My Title"
let titleSize = title.size(withAttributes: [.font: titleFont])
let frame = CGRect(x: 0, y: 0, width: titleSize.width, height: 20.0)
let titleLabel = UILabel(frame: frame)
titleLabel.font = titleFont
titleLabel.textColor = .red
titleLabel.textAlignment = .center
titleLabel.text = title
navigationItem.titleView = titleLabel
I had the same problem before. I had a UINavigationbar with right- and left-button.
I want to center an image on the UINavigationbar. So I had to put the image into an UIView with width 0.
The image self get a x-value which is half of own width.
UINavigationItem *item = navigationController.topViewController.navigationItem;
UIView *backView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 30)];
[img_logo setFrame:CGRectMake(-45, 5, 90, 20)];
[backView addSubview:img_logo];
item.titleView = backView;
Nice answer! However sizeWithFont is now deprecated. You would want to do something like this now.
NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:#"Helvetica" size:30], NSFontAttributeName, nil];
CGSize requestedTitleSize = [[NSString stringWithFormat:#"Your String"] sizeWithAttributes:textTitleOptions];
CGFloat titleWidth = MIN(self.view.frame.size.width, requestedTitleSize.width);
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleWidth, 44)];
I used autolayout constraints to solve this issue:
Add UILabel into a UIView.
Set constraints of centerX for the UILabel:
self.titleLabelCenterXConstraint = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];
In the UIView layoutSubViews to adjust the centerX offset value:
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat screenCenter = CGRectGetMidX([UIScreen mainScreen].bounds);
CGFloat diffCenter = screenCenter - CGRectGetMidX(self.frame);
self.titleLabelCenterXConstraint.constant = diffCenter;
}
set UIView to navigationItem titleView
Swift 2.3:
let tlabel = UILabel(frame: CGRectMake(0, 0, 200, 40))
tlabel.text = self.title
tlabel.textColor = UIColor.whiteColor()
tlabel.font = UIFont.boldSystemFontOfSize(17) //UIFont(name: "Helvetica", size: 17.0)
tlabel.backgroundColor = UIColor.clearColor()
tlabel.adjustsFontSizeToFitWidth = true
tlabel.textAlignment = .Center
self.navigationItem.titleView = tlabel
Swift 3 for #BHuelse's answer:
let image = UIImage(named: "logo")
let logoView = UIView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 30))
let imageView = UIImageView(frame: CGRect(x: -45, y: 5, width: 90, height: 20))
imageView.image = image
imageView.contentMode = .scaleAspectFit
logoView.addSubview(imageView)
self.navigationItem.titleView = logoView
The title is not centered in the navbar itself. It is centered between the left buttons and the right buttons of the navbar. This means that if you have a big button on the left, the title will be shifted to the right.
You can change the center by adding a centered constraint to your title and then modifying it so it's really the center of the navbar :
// Position the title in the middle of the navbar and not in the middle of buttons
fileprivate func centerTitle () {
if let navigation = self.navigationController {
let titleMiddle = navigation.navigationBar.convert(titleViewLabel.frame, from: titleViewLabel.superview).midX
let diff = navigation.navigationBar.center.x - titleMiddle
constraintTitleViewCentered.constant += diff
}
}