【问题标题】:UITextView text not starting from topUITextView 文本不是从顶部开始
【发布时间】:2012-05-29 03:58:18
【问题描述】:

我有一个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 在右侧。

【问题讨论】:

  • @Nitish,两个textview的字体大小一样???
  • 左侧视图是 UILabel,右侧视图是 UITextView。是的,两者的尺寸相同。
  • 将两者都更改为 uilabels 或同时更改为 uitextviews 有什么问题吗?
  • 我认为这是 UITextView 的正常行为。你可以做的是根据 UITextView 对齐 UILabel !!!
  • 我必须将右视图保留为 UITextView,因为我需要检测正在使用 dataDetectors 的字符串。将其更改为 UILabel 没有第二点。左边应该是 UILabel。

标签: iphone uitextview


【解决方案1】:

在你的 UITextView 中像这样设置 Content Inset

youtextView.contentInset = UIEdgeInsetsMake(-7.0,0.0,0,0.0);

以您想要的方式调整顶部值。这应该可以解决您的问题。

编辑:

如果您在使用 iOS7 或更高版本时遇到问题,请尝试使用...

[yourTextView setContentOffset: CGPointMake(x,y) animated:BOOL];

【讨论】:

  • 看起来不错,但是你知道如何计算top inset,而不是设置一个固定值吗?
  • 有什么方法可以为具有可变字体大小的 UITextView 处理这个问题?
  • 也可以在情节提要中使用User Defined Runtime Attributes
【解决方案2】:

在viewDidLoad方法中添加

self.automaticallyAdjustsScrollViewInsets = false

【讨论】:

  • 如果我添加这一行,并且文本很长,这根本没有任何意义。这样我就不会来这里寻求解决方案了。
  • @crazy_phage 即使是长文本,它也能很好地工作,也许您还有其他一些限制会为您禁用此解决方案
  • 是的,也许我当时用的是sb,还有一些奇怪的代码。
  • 这是 Swift 的最佳解决方案,自动布局有和没有故事板。非常感谢!
  • 有史以来最好的答案。我尝试了所有其他解决方案,只有这个可以解决问题。谢谢
【解决方案3】:

我是这样做的。当滚动禁用时,从顶部加载的文本。加载后比启用滚动。

- (void)viewDidLoad{
myTextView.scrollEnabled = NO;  
}       
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
myTextView.scrollEnabled = YES;
}

【讨论】:

  • 这是一个解决方案。 (iOS10.2 with swift3)
  • 这在 iOS 11 中对我有用。这似乎很奇怪。
【解决方案4】:

现有的解决方案都没有帮助,但这个有帮助

目标-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】:
      self.yourTextView.scrollEnabled = NO;
      
      self.yourTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
      

      【讨论】:

      • 纯代码答案不被视为好答案。请通过提供更多上下文和解释来改进您的答案。
      • 请连同代码一起写一些解释。在 SO --Review 中通常不鼓励仅代码答案
      【解决方案7】:
      - (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};
      }
      

      【讨论】:

        【解决方案8】:

        这就是答案

        在斯威夫特中

        termsTextView.contentOffset = CGPointMake(0, -220)
        

        在 Objective-C 中

        [termsTextView setContentOffset: CGPointMake(0,-220) animated:NO];
        

        【讨论】:

        • 您的 Swift 代码不是 Objective-C 的正确版本。它应该是'yourTextView.setContentOffset(CGPoint(x: 0, y: -220), animated: true)'
        • 这对我来说是正确的代码,它有效!干得好Sameera
        • 建议编辑:myTextView.contentOffset = CGPointMake(0, -myTextView.contentSize.height) 理由:OP 的 UITextView 称为 myTextView,而不是 termsTextView。并且动态拉高使答案对其他读者(我希望)更有用,例如在 Interface Builder 中制作 UITextViews 的那些。
        【解决方案9】:

        这发生在我在 iOS 7 和 8 中设置 UITextViewautoresizingMask 属性之后。

        我找到了一个可选的解决方法:

        - (void)viewWillAppear:(BOOL)animated {
            [super viewDidAppear:animated];
            [yourTextView scrollRectToVisible:CGRectMake(0, 0, yourTextView.contentSize.width, 10) animated:NO];
        }
        

        【讨论】:

          【解决方案10】:

          如果您只想在不添加视图控制器管理的情况下正确加载故事板中布局的文本视图,则可以这样做(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
              }
          }
          

          【讨论】:

            【解决方案11】:

            swift 4 解决方案:

            self.textView.scrollRangeToVisible(NSMakeRange(0, 0))
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-05-18
              • 2016-03-19
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-02-21
              • 1970-01-01
              • 2019-06-14
              相关资源
              最近更新 更多