我有UITextView
。我希望它的文本从顶部开始,但我不是从顶部开始。请看一下我的代码:
UITextView *myTextView = [[UITextView alloc] init];
myTextView.frame = rect;
myTextView.editable = NO;
myTextView.font = [UIFont fontWithName:@"Helvetica" size:MAIN_FONT_SIZE];
myTextView.backgroundColor = [UIColor clearColor];
myTextView.text = sourceNode.label;
myTextView.dataDetectorTypes = UIDataDetectorTypeAll;
[cell.contentView addSubview:myTextView];
[myTextView sizeToFit];
[self alignTextToTopTextView:myTextView];
alignTextToTopTextView :
-(void)alignTextToTopTextView :(UITextView*)textView{
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height;
textView.frame = frame;
}
请参阅以下屏幕截图。
UITextView
位于右侧。
答案 0 :(得分:47)
在UITextView中设置内容插入
youtextView.contentInset = UIEdgeInsetsMake(-7.0,0.0,0,0.0);
按照您想要的方式调整Top值。这个问题可以解决你的问题。
修改强>
如果您遇到iOS7或更高版本的问题,请尝试使用...
[yourTextView setContentOffset:CGPointMake(x,y)animated:BOOL];
答案 1 :(得分:47)
在viewDidLoad方法中添加
self.automaticallyAdjustsScrollViewInsets = false
答案 2 :(得分:21)
- (void)viewDidLoad{
myTextView.scrollEnabled = NO;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
myTextView.scrollEnabled = YES;
}
答案 3 :(得分:19)
现有的解决方案都没有帮助,但帮助:
目标-C:
- (void)viewDidLayoutSubviews {
[self.yourTextView setContentOffset:CGPointZero animated:NO];
}
Swift 3 :
override func viewDidLayoutSubviews() {
textView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
}
答案 4 :(得分:3)
这就是我使用的
textView.textContainerInset =
UIEdgeInsetsMake(
0,
-textView.textContainer.lineFragmentPadding,
0,
-textView.textContainer.lineFragmentPadding
)
;
答案 5 :(得分:0)
- (void) viewDidLoad {
[textField addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
[super viewDidLoad];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
UITextView *tv = object;
//Center vertical alignment
//CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height * [tv zoomScale])/2.0;
//topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect );
//tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
//Bottom vertical alignment
CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height);
topCorrect = (topCorrect <0.0 ? 0.0 : topCorrect);
tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
}
答案 6 :(得分:0)
以下是答案
在Swift中
termsTextView.contentOffset = CGPointMake(0, -220)
在Objective-C
中[termsTextView setContentOffset: CGPointMake(0,-220) animated:NO];
答案 7 :(得分:0)
在iOS 7和8中设置autoresizingMask
的{{1}}属性后,会发生这种情况。
我找到了一个可选的解决方法:
UITextView
答案 8 :(得分:0)
如果您只希望在情节提要中布局的文本视图正确加载而无需添加视图控制器管理,则可以这样做(iOS 12,Swift 4.2):
final class UITopLoadingTextView: UITextView {
var shouldEnableScroll = false
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
shouldEnableScroll = isScrollEnabled
self.isScrollEnabled = false
}
override func layoutSubviews() {
super.layoutSubviews()
isScrollEnabled = shouldEnableScroll
}
}
答案 9 :(得分:0)
swift 4解决方案:
self.textView.scrollRangeToVisible(NSMakeRange(0, 0))
答案 10 :(得分:-1)
self.yourTextView.scrollEnabled = NO;
self.yourTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);