Текст UITextView не начинается сверху
У меня есть 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
находится справа.
![enter image description here]()
Ответы
Ответ 1
Установите вставку содержимого, как это, в UITextView
youtextView.contentInset = UIEdgeInsetsMake(-7.0,0.0,0,0.0);
Отрегулируйте верхнее значение так, как вы хотите. это поможет устранить вашу проблему.
EDIT:
Если у вас возникли проблемы с iOS7 или выше, попробуйте использовать...
[yourTextView setContentOffset: CGPointMake (x, y) анимированный: BOOL];
Ответ 2
в методе viewDidLoad добавьте
self.automaticallyAdjustsScrollViewInsets = false
Ответ 3
Я сделал это. Когда прокрутка отключена, текст загружается сверху. После загрузки, чем разрешить прокрутку.
- (void)viewDidLoad{
myTextView.scrollEnabled = NO;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
myTextView.scrollEnabled = YES;
}
Ответ 4
Ни один из существующих решений не помог, но этот помог:
Objective-C:
- (void)viewDidLayoutSubviews {
[self.yourTextView setContentOffset:CGPointZero animated:NO];
}
Swift 3:
override func viewDidLayoutSubviews() {
textView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
}
Ответ 5
Это то, что я использую
textView.textContainerInset =
UIEdgeInsetsMake(
0,
-textView.textContainer.lineFragmentPadding,
0,
-textView.textContainer.lineFragmentPadding
)
;
Ответ 6
- (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};
}
Ответ 7
Вот ответ
в Swift
termsTextView.contentOffset = CGPointMake(0, -220)
в Objective-C
[termsTextView setContentOffset: CGPointMake(0,-220) animated:NO];
Ответ 8
Это происходит после того, как я установил свойство autoresizingMask
для UITextView
как в iOS 7, так и в 8.
Я нашел необязательное обходное решение:
- (void)viewWillAppear:(BOOL)animated {
[super viewDidAppear:animated];
[yourTextView scrollRectToVisible:CGRectMake(0, 0, yourTextView.contentSize.width, 10) animated:NO];
}
Ответ 9
Если вы просто хотите, чтобы текстовые представления, размещенные в раскадровках, загружались правильно без добавления управления контроллером представления, это будет сделано (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
}
}
Ответ 10
решение swift 4:
self.textView.scrollRangeToVisible(NSMakeRange(0, 0))
Ответ 11
self.yourTextView.scrollEnabled = NO;
self.yourTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);