我已经构建了一个带有页面的iPhone应用程序,以显示UIScrollView中.txt文件的内容。出于某种原因,我的普通txt文件导致Scrollview允许它稍微向左和向右平移,我希望它只是向下滚动。
真的需要帮助这个..
由于
#import "ITFAQController.h"
#import "FTCoreTextView.h"
@implementation ITFAQController
- (NSString *)textForView {
NSString *path = [[NSBundle mainBundle] pathForResource:@"faq" ofType:@"txt"];
NSError *error;
return [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
}
/*- (id)init
{
self = [super init];
if (self) {
// Custom initialization
[self showLogoInNavBar:YES];
}
return self;
}
*/
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
[self showLogoInNavBar:YES];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 330, (self.view.bounds.size.height - 90))];
[self.view addSubview:scrollView];
FTCoreTextView *ctView = [[FTCoreTextView alloc] initWithFrame:CGRectMake(10, 0, 300, scrollView.bounds.size.height)];
[ctView setText:[self textForView]];
FTCoreTextStyle *defaultStyle = [FTCoreTextStyle styleWithName:FTCoreTextTagDefault];
[defaultStyle setFont:kITDescriptionFont];
[defaultStyle setTextAlignment:FTCoreTextAlignementJustified];
[defaultStyle setParagraphInset:UIEdgeInsetsMake(5, 10, 0, 0)];
[ctView addStyle:defaultStyle];
FTCoreTextStyle *title = [FTCoreTextStyle styleWithName:@"title"];
[title setFont:kITTitleFont];
[title setColor:kITCellFontColor];
[title setParagraphInset:UIEdgeInsetsMake(10, 0, 5, 0)];
[ctView addStyle:title];
FTCoreTextStyle *bold = [FTCoreTextStyle styleWithName:@"bold"];
[bold setFont:[UIFont boldSystemFontOfSize:14]];
[ctView addStyle:bold];
FTCoreTextStyle *link = [FTCoreTextStyle styleWithName:FTCoreTextTagLink];
[link setColor:[UIColor blueColor]];
[ctView addStyle:link];
FTCoreTextStyle *bullets = [FTCoreTextStyle styleWithName:FTCoreTextTagBullet];
[bullets setBulletColor:kITCellFontColor];
[bullets setFont:kITItalicFont];
[bullets setParagraphInset:UIEdgeInsetsMake(0, 10, 0, 0)];
[ctView addStyle:bullets];
[ctView fitToSuggestedHeight];
[scrollView addSubview:ctView];
[scrollView setContentSize:[ctView suggestedSizeConstrainedToSize:CGSizeMake(scrollView.bounds.size.width, CGFLOAT_MAX)]];
//[ctView fitToSuggestedHeight];
//[ctView sizeToFit];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
答案 0 :(得分:2)
CGRectMake(0, 0, 330, (self.view.bounds.size.height - 90))];
将330替换为320.这应该可以解决问题。
修改:正如评论中所述,您应该更喜欢self.view.bounds.size.width
答案 1 :(得分:0)
问题可能与这一行有关:
[scrollView setContentSize:[ctView suggestedSizeConstrainedToSize:CGSizeMake(scrollView.bounds.size.width, CGFLOAT_MAX)]];
我不知道该方法的用途,但它可能会为您contentSize
提供width > 320
。之后添加以下行会解决它,我想:
if (scrollView.contentSize.width > self.view.bounds.size.width)
[scrollView setContentSize:CGSizeMake(self.view.bounds.size.width,scrollView.contentSize.height)];