I want to display one paragraph in UITextView. I'm able to put the content of paragraph in Text view, but I'm unable to scroll UITextView (Some of the content is not visible as it's not scrolling).
What I'm trying to do is:
CGRect frame = CGRectMake(0, 0, 320, 480);
//allocate view
self.view = [[UIView alloc]initWithFrame:frame];textview = [[UITextView alloc]initWithFrame:CGRectMake(30, 200, 275, 400)];
[textview setFont:[UIFont fontWithName:#"Arial" size:12]];
[textview setScrollEnabled:YES];
[textview setUserInteractionEnabled:NO];
[textview setBackgroundColor:[UIColor clearColor]];
//[textview setText:#"Hi,I'm working fine with this space"];
[self.view addSubview:textview];
In this case, scrolling is enabled. Can any one tell me why it's not scrolling?
set [textview setUserInteractionEnabled:TRUE];and I think you dont want to keyboard to pop up then you can hide the keyboard by implementing the uitextview delegate methode -(void)textViewDidBeginEditing:(UITextView *)textView{[textView resignFirstResponder];
set YES for setUserInteractionEnabled property of UITextView
set this line
[textview setUserInteractionEnabled:YES];
instead of
[textview setUserInteractionEnabled:NO];
I have a ViewController that only has a text view with about 5 pages of text.
Here is what I did in Swift 4.0:
Very important: Make sure you have auto layout set on the text
view. In my ViewController, I set my constraints to the text view
to 8,8,8,8 with constrain to margins checked. The whole thing does
not scroll if auto layout is not set.
This is the code I used:
Swift 4.0
textForPrivacyPolicy.showsVerticalScrollIndicator = true
textForPrivacyPolicy.isEditable = false
textForPrivacyPolicy.isScrollEnabled = true
textForPrivacyPolicy.scrollRangeToVisible(NSMakeRange(0, 0))
textForPrivacyPolicy.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
Paste this code
CGRect frame = CGRectMake(0, 0, 320, 480);
//allocate view
self.view = [[UIView alloc]initWithFrame:frame];textview = [[UITextView alloc]initWithFrame:CGRectMake(30, 200, 275, 400)];
[textview setFont:[UIFont fontWithName:#"Arial" size:12]];
[textview setScrollEnabled:YES];
[textview setUserInteractionEnabled:YES];
[textview setBackgroundColor:[UIColor clearColor]];
//[textview setText:#"Hi,I'm working fine with this space"];
[self.view addSubview:textview];
Add this line : [textview setUserInteractionEnabled:YES]; in place of this:
[textview setUserInteractionEnabled:NO];
Set UserInteractionEnabled as YES.
I had the same issue and I had setscrollable and userinteractionenabled to TRUE. It turned out to be a problem with Constraints. In order to fix this, click on your view controller in ur storyboard (the battery icon on top right), then go to "Editor"->"Resolve Auto Layout Issues"->"Add Missing Constraints In ". Fixed the issue for me!
bapi rout , There is a property for the user interaction with textView. You have set it to No. If you want to perform scroll on TextView, You have to enable this property.
[textview setUserInteractionEnabled:YES];
This will help you.
Related
I have added few UI components using Storyboard (UILabel, UIImageView). I am trying to put UIScrollView in this view, below all other elements:
Here is my code right at the end of -viewWillAppear method (code from my View Controller):
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;
[self.view addSubview:scroll];
[self.view sendSubviewToBack:scroll];
The reason why I need a UIScrollView is large amount of text in detailsLabel - I'm attaching code I use for handling this label programatically:
detailsLabel.text = #"Some long text taken from databse";
[detailsLabel sizeToFit];
detailsLabel.numberOfLines = 0;
detailsLabel.lineBreakMode = UILineBreakModeWordWrap;
What I am doing wrong? How do I put UIScrollView below all other elements of my main view?
Try this:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;
[self.view insertSubview:scroll atIndex:0];
...
[self.view insertSubview:detailsLabel atIndex:1];
Put your code in ViewDidLoad . Then add the labels after you have added the UIScrollView to the self.View in the ViewDidLoad itself. So by default the UIScrollView would be added on the self.View and then your label. Now you can either add the label on the scrollview or onto the self.view .
I've followed the instructions in this answer and it worked great: I've changed the Custom Class in Identity Inspector from UIView to UIScrollView and added this line to my -viewDidLoad in the View Controller file:
[(UIScrollView *)self.view setContentSize:CGSizeMake(320, 700)];
Option One:
Declare detailsLabel in .h:
IBOutlet UILabel * detailsLabel; // MAP IT WITH NIB or STORYBOARD
give it property and Synthesise in .m.
here in viewDidLoad declare:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;
[self.view addSubview:scroll];
[self.view sendSubviewToBack:scroll];
[scroll addSubview:detailsLabel];
// IT SHOULD WORK.
Option two:
If you have large amount of text to display, then you can also use UITextView, it has inbuilt scrollview. And you can also set its text edit property.
I am working on a message based iPhone app. I have a screen to reply to a received message. This screen contains two UITextViews like bottomTextView and topTextView.
topTextView is added as bottomTextView's InputAccessory view.
When the user enter into the screen topTextView has to becomeFirstResponder. It is showing but the cursor is not placed in the topTextView. The cursor is located in the textview bottomTextView. How to make topTextView become first responder with cursor in place?
Here is the code i have tried:
- (void)viewDidLoad
{
[super viewDidLoad];
bottomBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 380, 320, 40)];
bottomBarViewImage.image = [UIImage imageNamed: #"toolbarbg~iphone.png"];
[bottomBarView addSubview: bottomBarViewImage];
[self.view addSubview: bottomBarView];
bottomTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 7.5, 210, 25)];
bottomTextView.delegate = self;
bottomTextView.backgroundColor = [UIColor clearColor];
bottomTextView.font = [UIFont fontWithName:#"Helvetica" size:14];
bottomTextView.scrollEnabled = NO;
[bottomBarView addSubview: bottomTextView];
topBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 380, 320, 40)];
topBarViewImage.image = [UIImage imageNamed: #"toolbarbg~iphone.png"];
[topBarView addSubview: topBarViewImage];
topTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 7.5, 210, 25)];
topTextView.delegate = self;
topTextView.backgroundColor = [UIColor clearColor];
topTextView.font = [UIFont fontWithName:#"Helvetica" size:14];
topTextView.scrollEnabled = NO;
[topBarView addSubview: topTextView];
[bottomTextView becomeFirstResponder];
[bottomTextView setInputAccessoryView: topBarView];
}
-(void) textViewDidBeginEditing:(UITextView *)textView
{
if(textView == bottomTextView)
{
bottomTextView.scrollEnabled = NO;
[topTextView becomeFirstResponder];
}
}
The topTextView with topBarView is showing but the cursor is not placed in topTextView. Could you please help me to solve this issue? Thanks in advance.
I think it may be because you call [topTextView becomeFirstResponder]; in UITextView's delegate textViewDidBeginEditing:. So the topTextView only becomes first responder when you start editing bottomTextView. Try calling [topTextView becomeFirstResponder]; instead of [bottomTextView becomeFirstResponder]; in viewDidLoad. See how it goes. I'm not sure, but becomeFirstResponder may not call textViewDidBeginEditing:. Not sure it'll work, but worth the try...
EDIT :
I found a related problem here. It may be because the textView does not appear right away, so it can not become first responder. Here is the accepted answer, by #Tom :
My solution: check when the keyboard (and thus the accessory view)
appeared!
Step 1) Listen for the notification (make sure this code is read
before you want to receive the notification).
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(changeFirstResponder)
name:UIKeyboardDidShowNotification
object:nil];
Step 2) When the keyboard has appeared, you can set the textfield in
your inputaccessoryview to become first responder:
-(void)changeFirstResponder
{
[textField becomeFirstResponder]; //will return TRUE;
}
can any one help to change the font size in UITextView.
[textView setFont:[UIFont fontWithName:#"ArialMT" size:30]];
I use this one but it doesn't work.
Have you bind textView with xib or allocated textView?
If yes,
You can use following code to check whether this works
[textView setFont:[UIFont boldSystemFontOfSize:15]];
or
[textView setFont:[UIFont systemFontOfSize:15]];
Check, if "Selectable" is checked in the xib/Storyboard attribute inspector. If it's not selectable, the size wont be changed
Swift 4.0
textField.font = UIFont.systemFont(ofSize: 17.0)
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[textView setFont: [UIFont fontWithName:#"ArialMT" size:30]];
textView.text = #"test my font size";
[self.view addSubview:textView];
[textView release];
Your code is correct. The problem is from other part of your code. Provide more details about your textView
Set custom font and size in Swift:
textView.font = UIFont(name: "ArialMT", size: 20)
Use this:
UITextView *myTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
myTextView.font = [UIFont fontWithName:#"arial" size:14];
Here is for swift 3.0
textview.font = UIFont(name: textview.font.fontName, size: 18)
I tried it the same way like you with
[textView setFont:[UIFont fontWithName:kFontName size:kFontSize]];
I set the UITextView inside ViewDidLoad. My problem was, I transformed my view:
[_viewEnd setTransform:CGAffineTransformMakeScale(0.0, 0.0)];
My problem was solved with setting the properties of the UITextView after transforming my UIView which includes the UITextView.
OK, I'm struggling to make this work but without success.
Basically I want to add a UILabel to an UIView and center it.
The code looks like this:
UIView *customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[customTitleView setBackgroundColor:[UIColor clearColor]];
// Screen title
CGSize constraint = CGSizeMake(200.0, 44.0f);
CGSize size = [screenTitle sizeWithFont:[UIFont boldSystemFontOfSize:20.0]
constrainedToSize:constraint
lineBreakMode:UILineBreakModeCharacterWrap];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, size.width, size.height)];
[titleLabel setText:screenTitle];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20.0]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
titleLabel.center = customTitleView.center;
[customTitleView addSubview:titleLabel];
[titleLabel release];
self.navigationItem.titleView = customTitleView;
[customTitleView release];
I expected that the UILabel would be centered within the UIView.
Well, it isn't. It is somehow right aligned and not even close to the center of the UIView.
What am I doing wrong?
So here's what's going wrong:
self.navigationItem.titleView = customTitleView;
When you set customTitleView as the title view of the navigation item the frame of customTitleView will change. customTitleView won't be 320 pixels wide anymore, but the frame of titleLabel will remain the same. Thus it will no longer be centered.
Can't you just set titleLabel as titleView? I think it should be centered automatically. If not, I know a more complicated way to solve it. Drop a comment and I'll tell you how.
EDIT: So here is how you realign the titleLabel after customTitleView's frame has changed.
When the frame of a view is changed layoutSubviews is called on that view. So instead of letting customTitleView be a regular UIView you need to make it a custom view that inherits from UIView. In that custom view you override layoutSubviews and in your implementation of layoutSubviews you make sure everything is aligned the way you want based on the new frame (self.frame). I'll leave the implementation to you.
I want that searchbar should also scroll with scrolling mean when i scroll down searchbar should be visible.
as there anyway to do so...
thanks. and sorry for my bad english..
If I understand you correctly, this is what you should do:
Open up the interface builder and make the SearchBar the child of the table view if you want it to scroll along with the contents. If you need it to be always visible, drag it out of the tableview.
Well you can also simply do that
SearchBar =[[UISearchBar alloc]initWithFrame:CGRectMake(10, 5, 300, 50)];
SearchBar.tintColor=[UIColor blackColor];
[SearchBar setDelegate:self];
//[self.view addSubview:search];
table=[[UITableView alloc]initWithFrame:CGRectMake(0, 15, 320, 480) style:UITableViewStyleGrouped];
table.delegate=self;
table.dataSource=self;
table.backgroundColor=[UIColor darkGrayColor];
table.tableHeaderView=SearchBar;
[self.view addSubview:table];