0

我已经构建了一个带有页面的 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
4

2 回答 2

2
CGRectMake(0, 0, 330, (self.view.bounds.size.height - 90))];

用 320 替换 330。这应该可以解决问题。

编辑:如评论中所写,您应该更喜欢self.view.bounds.size.width

于 2013-04-03T14:46:56.113 回答
0

问题可能与这一行有关:

[scrollView setContentSize:[ctView suggestedSizeConstrainedToSize:CGSizeMake(scrollView.bounds.size.width, CGFLOAT_MAX)]];

我不知道那个方法是做什么的,但它可能会给你一个contentSizewith width > 320。我认为,在那之后添加以下几行可以解决它:

if (scrollView.contentSize.width > self.view.bounds.size.width)
    [scrollView setContentSize:CGSizeMake(self.view.bounds.size.width,scrollView.contentSize.height)];
于 2013-04-03T15:47:07.250 回答