How i can display vertical pages for UITextView in UIScrollview
self.textView = [[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 410)]autorelease];
self.textView.textColor = [UIColor blackColor];
self.textView.font = [UIFont fontWithName:#"Georgia-Bold" size:14];
self.textView.textAlignment = UITextAlignmentCenter;
//self.textView.backgroundColor = [UIColor colorWithHue:2.0/12 saturation:2.0 brightness:4.0/10 alpha:1.0];
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.editable = NO;
self.textView.delegate = self;
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.layer.borderWidth = 1;
self.textView.layer.borderColor = [[UIColor darkGrayColor] CGColor];
self.textView.layer.cornerRadius = 1;
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.text = #"Juice fasting makes every other natural method work faster...............";
[self.view addSubview:self.textView];
scrollView.contentSize = CGSizeMake(textView.frame.size.width, textView.frame.size.height);
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 0.75;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.delegate = self;
scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
[self.scrollView addSubview:self.textView];
[button release];
[super viewDidLoad];
}
I have a long text in UITextView. How i can display this UITextView in vertical pages when user scrolls.
Appreciate help.
Did you try paging?
textView.pagingEnabled = YES;
Is that what you want? Otherwise I probably would recommend using a default UILabel in a default UIScrollView, to set this up. (Adding one UILabel per page to the scrollView)
Make the text view large enough so that there is no scrolling within it (or change it to a UILabel), and put that inside a separate scrollView.
Then set
scrollView.pagingEnabled = YES;
and make sure the height of the content size is larger than the height of the actual scroll view. So if the text view is 320x960, make the scroll view 320x480.
Related
I saw a this question and answer and I tried a few options but non worked.
I would like to create a UIPickerView like the one below, (fixed labels inches and feet) but those wouldn't appear:
I create the UIImagePicker like this:
- (void)viewDidLoad
{
_picker = [[UIPickerView alloc] init];
CGRect pickerFrame = CGRectMake(0, 0, 200, 216);
pickerView.frame = pickerFrame;
pickerView.userInteractionEnabled = YES;
pickerView.dataSource = self;
pickerView.hidden = YES;
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
[self.view addSubview:pickerView];
[textField setInputView:pickerView];
textField.delegate = self;
[pickerView removeFromSuperview];
_picker.hidden = YES;
}
- (BOOL) textFieldShouldBeginEditing:(UITextView *)textView
{
if (textView.tag==1){ //field for the uipickerview
_picker.hidden = NO;
[self addPickerLabel:#"Feet" rightX:114 top:342 height:21];
[self addPickerLabel:#"Inches" rightX:241 top:342 height:21];
}
return YES;
}
- (void)addPickerLabel:(NSString *)labelString rightX:(CGFloat)rightX top:(CGFloat)top height:(CGFloat)height {
#define PICKER_LABEL_FONT_SIZE 18
#define PICKER_LABEL_ALPHA 0.7
UIFont *font = [UIFont boldSystemFontOfSize:PICKER_LABEL_FONT_SIZE];
CGFloat x = rightX - [labelString sizeWithFont:font].width;
// White label 1 pixel below, to simulate embossing.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, top + 1, rightX, height)];
label.text = labelString;
label.font = font;
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.opaque = NO;
label.alpha = PICKER_LABEL_ALPHA;
[_picker addSubview:label];
// Actual label.
label = [[UILabel alloc] initWithFrame:CGRectMake(x, top, rightX, height)];
label.text = labelString;
label.font = font;
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
label.opaque = NO;
label.alpha = PICKER_LABEL_ALPHA;
[_picker addSubview:label];
}
The picker appears, but without the fixed labels of inches and feet.
What is wrong?
Move this lines to viewDidLoad and try it.Labels need to be added
once.Not always when textfield did begin editing
[self addPickerLabel:#"Feet" rightX:114 top:342 height:21];
[self addPickerLabel:#"Inches" rightX:241 top:342 height:21];
Log the frame of both the label and set it correct if it appears wrong
NSLog(#"%#",NSStringFromCGRect(label.frame));
Your UIPickerView has height of 216, but you put the labels at height 342. This might be the reason you can't see them.
Edit:
Try replacing the lines where you make the labels to
[self addPickerLabel:#"Feet" rightX:114 top:98 height:21];
[self addPickerLabel:#"Inches" rightX:241 top:98 height:21];
I used the already implemented great component:
https://github.com/brunow/ActionSheetPicker2
Which provides a multicolumn picker view and I simply changed the text and the amount of columns
Further to this question I asked recently, I was able to get the explanation view appearing in front of the tableview successfully, but I now have a problem whereby the tableview's separator lines are briefly visible underneath my explanation view when the tableview first loads. This is in spite of setting the explanation view's background colour, setting it to opaque and bringing it to the front. It's just a brief flash, but it's noticeable and distracting.
Here is my code (_explanationView is a UIView instance variable that I set to nil in the view controller's dealloc method):
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.separatorColor = [UIColor darkGrayColor];
CGRect frame = CGRectMake(50.0f, 120.0f, 220.0f, 155.0f);
_explanationView = [[UIView alloc] initWithFrame:frame];
_explanationView.userInteractionEnabled = NO;
_explanationView.backgroundColor = [UIColor whiteColor];
_explanationView.opaque = YES;
_explanationView.layer.borderColor = [UIColor darkGrayColor].CGColor;
_explanationView.layer.borderWidth = 1.0f;
_explanationView.layer.cornerRadius = 10.0f;
_explanationView.layer.masksToBounds = YES;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 0.0f, 180.0f, 145.0f)];
label.backgroundColor = [UIColor whiteColor];
label.textColor = [UIColor darkGrayColor];
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
label.font = [UIFont systemFontOfSize:14.0f];
label.text = #"Explanation of this screen...";
[_explanationView addSubview:label];
[self.tableView addSubview:_explanationView];
}
I found that I had to bring the explanation view to the front in the viewDidAppear: method, otherwise the tableview's separator lines are permanently visible underneath.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.tableView bringSubviewToFront:_explanationView];
}
What can I do to stop the tableview's separator lines from briefly being visible underneath my custom view?
I found this answer which indicates that handling the order of a custom subview in tableView can be done in the layoutSubviews method.
I have a frame on my UIView with a layer. I have the layer set the background color and make the alpha 0.5 and make the frame.backgroundColor = clearColor so that people can see the lines behind it. However it makes the subviews which hold text also faded out. How do I prevent this?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setAlpha:kAlpha];
CALayer *layer = [self layer];
[layer setMasksToBounds:YES];
[layer setBackgroundColor:[UIColor redColor].CGColor];
[layer setCornerRadius:kCornerRadius];
}
return self;
}
- (id)init
{
if (self = [super init]) {
self.clipsToBounds = YES;
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = NO;
tileTitle = [[UILabel alloc] init];
tileTitle.textColor = [UIColor blackColor];
tileTitle.backgroundColor = [UIColor clearColor];
tileTitle.font = [UIFont boldSystemFontOfSize:13.0f];
tileDescription = [[UILabel alloc] init];
tileDescription.textColor = [UIColor blackColor];
tileDescription.backgroundColor = [UIColor clearColor];
tileDescription.font = [UIFont systemFontOfSize:11.0f];
tileDescription.lineBreakMode = UILineBreakModeTailTruncation;
[self addSubview:tileTitle];
[self addSubview:tileDescription];
}
return self;
}
To change the transparency of the view, but not its subviews, you can change its background color:
myView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.25];
This method also works with a CALayer.
you must not set alpha value on all uiview, you must se the color with alfa.
[self.view setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
Use a subview (with the same size as your UIView) and set the color and alpha on the subview to your desired tranparency, rather than setting the alpha of your view. Set the backgroundColor of the top-level UIView to clearColor.
I currently have a UITextView which is contained in a UIViewController using the following code:
UIViewController *container = [[UIViewController alloc] init];
container.view.frame = CGRectMake(0, 0,
[UIScreen mainScreen].bounds.size.width, 1000);
//[UIScreen mainScreen].bounds.size.height
container.view.userInteractionEnabled = YES;
container.view.clipsToBounds = YES;
UIImageView *imgView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"myImage.png"]];
imgView.frame = container.view.frame;
imgView.userInteractionEnabled = YES;
[container.view addSubview:imgView];
[imgView release];
UITextView *textContained = [[UITextView alloc]
initWithFrame:CGRectMake(0, 0, container.view.bounds.size.width, 1000)];
//container.view.bounds.size.height
textContained.font = [UIFont fontWithName:#"Calibri" size:14];
textContained.scrollEnabled = YES;
textContained.editable = NO;
textContained.textColor = [UIColor whiteColor];
textContained.backgroundColor = [UIColor clearColor];
textContained.font = [UIFont boldSystemFontOfSize:14];
textContained.userInteractionEnabled = YES;
textContained.alwaysBounceVertical = YES;
textContained.contentSize = CGSizeMake(container.view.frame.size.width,
container.view.frame.size.height);
I then set my UItextView text property with some text, which extends past the current screen size. I then add my UITextView to my container using the following code.
switch (indexPath.row) {
case 0:
textContained.text = #"LOTS AND LOTS OF TEXT";
[container.view addSubview:textContained];
[self.navigationController pushViewController:container animated:YES];
[textContained release];
break;
default:
break;
}
[container release];
When I test this code, the text appears just fine in the UITextView and everything looks ok. But the problem is when I try to scroll down to see the remainder of the text. Everytime I scroll down the UITextView scrolls back in to its original position. I have tried several ways to get this to work, but I think I need some fresh eyes to see what I'm doing wrong.
Any help is greatly appreciated.
First, you don't want to set the contentSize of a UITextView, since it is determined by the length of text automatically.
Try to make the height of textView smaller. For example:
UITextView *textContained = [[UITextView alloc]
initWithFrame:CGRectMake(0, 0, container.view.bounds.size.width, 50)];
And give it more text, like:
textContained.text = #"LOTS AND LOTS OF TEXT\nLOTS AND LOTS OF TEXT\nLOTS AND LOTS OF TEXT\n\nLOTS AND LOTS OF TEXT\nLOTS AND LOTS OF TEXT\n";
I'm using UINavigationItem's titleView property to set a custom UILabel with my desired font size/color. Here's my code:
self.headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 400.0, 44.0)];
self.headerLabel.textAlignment = UITextAlignmentCenter;
self.headerLabel.backgroundColor = [UIColor clearColor];
self.headerLabel.font = [UIFont boldSystemFontOfSize:20.0];
self.headerLabel.textColor = [UIColor colorWithRed:0.259 green:0.280 blue:0.312 alpha:1.0];
self.navigationItem.titleView = self.headerLabel;
In the navigation bar I also have a left bar button. The result is: the text isn't properly centered. I've tried setting the x origin of the label, but this has no effect.
In stead of initWithFrame just use init and put [self.headerLabel sizeToFit] after your last line of code.
If you make the headerLabel a subview of the titleView, you can then set headerLabel's frame to control where it goes within the titleView.
The way you are doing it now, you don't have that control. I think the OS chooses the titleView's frame for you based on the space available.
Hope this helps!
I've used custom title labels for my nav bars in every app I have in the app store. I've tested many different ways of doing so and by far the easiest way to use a custom label in a navigation bar is to completely ignore titleView and insert your label directly into navigationController.view.
With this approach, it's easy to have the title label's frame always match the navigationBar's frame -- even if you are using a custom navBar with a non-standard size.
- (void)viewDidLoad {
[self.navigationController.view addSubview:self.titleLabel];
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didRotateFromInterfaceOrientation:
(UIInterfaceOrientation)fromInterfaceOrientation {
[self frameTitleLabel];
}
- (UILabel *) titleLabel {
if (!titleLabel) {
titleLabel = [[UILabel alloc]
initWithFrame:self.navigationController.navigationBar.frame];
titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:18];
titleLabel.text = NSLocalizedString(#"Custom Title", nil);
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
}
return titleLabel;
}
- (void) frameTitleLabel {
self.titleLabel.frame = self.navigationController.navigationBar.frame;
}
The one caveat to this approach is that your title can flow over the top of any buttons you have in the navBar if you aren't careful and set the title to be too long. But, IMO, that is a lot less problematical to deal with than 1) The title not centering correctly when you have a rightBarButton or 2) The title not appearing if you have a leftBarButton.
I have a same problem; I just somehow solved this issue by calculating the title length and set the label frame width accordingly. Although this is not a perfect one but can be manageable. Here is the code.
label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.font = [ UIFont fontWithName: #"XXII DIRTY-ARMY" size: 32.0 ];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.0f];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor orangeColor];
//label.text=categoryTitle;
CGFloat verticalOffset = 2;
NSString *reqSysVer = #"5.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
{
if (categoryTitle.length > 8)
{
label.frame = CGRectMake(0, 0, 300, 44);
}else {
label.frame = CGRectMake(0, 0, 80, 44);
}
self.navigationItem.titleView = label;
self.navigationItem.title=label.text;
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setTintColor:[UIColor newBrownLight]];
}
Just calculate exact frame size needed and align to left:
UIFont* font = [UIFont fontWithName:#"Bitsumishi" size:20];
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [title sizeWithFont:font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeCharacterWrap];
CGRect frame = CGRectMake(0, 0, expectedLabelSize.width, expectedLabelSize.height);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.font = font;
label.textAlignment = UITextAlignmentLeft;
label.text = title;
self.titleView = label;
UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
lbl.text = #"Home";
lbl.textAlignment = UITextAlignmentCenter;
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
lbl.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:20];
lbl.shadowColor = [UIColor blackColor];
lbl.shadowOffset = CGSizeMake(0,1);
self.navigationItem.titleView = vw;
[self.navigationItem.titleView addSubview:lbl];
What worked for me was to update the titleView frame in the viewDidAppear method.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIView *titleView = self.navigationItem.titleView;
CGRect navBarFrame = self.navigationController.navigationBar.frame;
[titleView setFrame:CGRectMake((CGRectGetWidth(navBarFrame) - TitleWidth) / 2, (CGRectGetHeight(navBarFrame) - TitleHeight) / 2, TitleWidth, TitleHeight)];
}