【问题标题】:UITextView inside UIScrollView not scrolling - Objective cUIScrollView 内的 UITextView 不滚动 - 目标 c
【发布时间】:2019-01-31 06:57:31
【问题描述】:

我正在尝试使用 UITextView 显示具有动态高度的消息,但是当消息的长度足以覆盖屏幕时,我无法获得滚动功能。我尝试了许多在线解决方案,但都失败了。我使用NSMutableAttributedString 来格式化我的文本。

这是我最后一次尝试:

NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[self.descString dataUsingEncoding:NSUTF8StringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

    __block BOOL found = NO;
    [attrStr beginEditing];
    [attrStr enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attrStr.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
        if (value) {
            UIFont *oldFont = (UIFont *)value;
            UIFont *newFont = [oldFont fontWithSize:16];
            [attrStr addAttribute:NSFontAttributeName value:newFont range:range];
            [attrStr addAttribute:NSForegroundColorAttributeName
                            value:[UIColor darkGrayColor]
                            range:range];

            found = YES;
        }
    }];
    if (!found) {
        // No font was found - do something else?
    }
    [attrStr endEditing];

CGRect labelSize = [attrStr boundingRectWithSize:CGSizeMake(self.view.frame.size.width, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];



self.textviewHeight.constant = labelSize.size.height;

在 Interface Builder 中,我使用了 UIScrollView,在其中放置了 UIView,在 UIView 中添加了 UITextView。 textviewHeight 是 UITextView 的高度约束。我不敢相信像这样简单的事情花了我这么多小时,我仍然没有解决方案。请帮忙!


我尝试过但失败的其他解决方案:

1.

CGRect labelSize = [self.descString boundingRectWithSize:CGSizeMake(self.view.frame.size.width * 0.97, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];

    self.descView.frame = CGRectMake(labelSize.origin.x,
                                    labelSize.origin.y,
                                    ceil(labelSize.size.width),
                                    ceil(labelSize.size.height));


    [self.descView sizeToFit];

2.

CGRect labelSize = [attrStr boundingRectWithSize:CGSizeMake(self.view.frame.size.width * 0.97, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];

    CGRect frame = self.descView.frame;
    frame.size.height = labelSize.size.height;
    self.descView.frame = frame;

如果更容易实现,我不介意使用 UILabel。

【问题讨论】:

  • 有点明显......但仍然如此。您是否不小心取消了 IB 中启用滚动的复选框?
  • 是的,我做到了。你的意思是在 UITextView 上,对吧?
  • 是的。所以检查一下?
  • 我已经用这个选项尝试了所有可能的组合。没有任何帮助。
  • 组合?当您单击 Attributes Inspector 中的 UITextView 时,Storyboard 上会出现一个名为 Scrolling Enabled 的框。是“开”还是“关”?

标签: ios objective-c xcode uiscrollview


【解决方案1】:

如果我了解您想要完成的工作,您应该不需要进行任何程序化高度计算。

我最好的猜测是您正在为 TextView 设置底部约束(这会限制它滚动其完整内容)。

尝试将 TextView 作为标准 UIView 的子视图并设置该视图的前导/尾随/底部/顶部约束(对于您希望 TextView 显示的总区域),然后将 TextView 放入其中并设置前导/尾随/顶部 NOT BOTTOM (我假设您希望它能够垂直滚动)约束,它会抱怨没有高度/y 约束,所以继续设置 centerY 等于到持有它的视图的 centerY(即垂直视图中的中心)。

我附上了一个约束是什么样子的例子(我在上面和下面添加了视图只是为了表明它可以根据持有它的约束的视图被约束到一个区域)

您的代码如下所示,稍作调整:

#import "ViewController.h"

@interface ViewController ()
@property (strong, nullable) IBOutlet UITextView *scrollingTextView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *reallyLongString = @"Really Long String here";
    // your code to adjust it below
    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[reallyLongString dataUsingEncoding:NSUTF8StringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

    __block BOOL found = NO;
    [attrStr beginEditing];
    [attrStr enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attrStr.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
        if (value) {
            UIFont *oldFont = (UIFont *)value;
            UIFont *newFont = [oldFont fontWithSize:23];
            [attrStr addAttribute:NSFontAttributeName value:newFont range:range];
            [attrStr addAttribute:NSForegroundColorAttributeName
                            value:[UIColor darkGrayColor]
                            range:range];

            found = YES;
        }
    }];
    if (!found) {
        // No font was found - do something else?
    }
    [attrStr endEditing];
    [self.scrollingTextView setAttributedText:attrStr];
    // Do any additional setup after loading the view, typically from a nib.
}


@end

【讨论】:

  • 只有一期。我需要为 UITextView 设置背景颜色,但颜色必须只在文本后面。当我使用你的方法时,如果文字很小,背景颜色会覆盖整个屏幕。知道如何解决这个问题吗?
  • @Student 我不确定我是否理解问题所在。您可以将 UITextView (scrollingTextView) 的背景颜色设置为所需的颜色,或者设置持有它的 UIView 的背景颜色,然后将 UITextView (scrollingTextView) 的背景颜色设置为清除。如果这些都不起作用,那么查看问题所在的屏幕截图会很有帮助。
猜你喜欢
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多