简体   繁体   English

UIScrollView中的UITextView无法滚动-目标c

[英]UITextView inside UIScrollView not scrolling - Objective c

I am trying to display a message with dynamic height using UITextView but when the message is long enough to cover the screen, I don't get the functionality of scrolling. 我正在尝试使用UITextView显示具有动态高度的消息,但是当消息足够长以覆盖屏幕时,我没有滚动功能。 I have tried many online solutions, but all failed. 我尝试了许多在线解决方案,但都失败了。 I use NSMutableAttributedString to format my text. 我使用NSMutableAttributedString格式化文本。

Here is my last attempt: 这是我最后的尝试:

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;

In Interface Builder I used a UIScrollView, inside it I placed a UIView and inside UIView I added a UITextView. 在Interface Builder中,我使用了UIScrollView,在其中放置了UIView,在UIView中添加了UITextView。 textviewHeight is the height constraint of the UITextView. textviewHeight是UITextView的高度约束。 I can't believe that a simple thing like this took me so many hours and I still don't have a solution. 我简直不敢相信像这样的简单事情花了我很多时间,但我仍然没有解决方案。 Please help ! 请帮忙 !


Other solutions I tried and failed: 我尝试和失败的其他解决方案:

1. 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. 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;

I don't mind using a UILabel if it's easier to implement. 我不介意使用UILabel是否更容易实现。

If I understand what you're attempting to accomplish, you shouldn't need to do any programmatic height calculations. 如果我了解您要完成的工作,则无需进行任何程序化高度计算。

My best guess is that you're setting the bottom constraint for the TextView (which is constraining it from scrolling it's complete contents). 我最好的猜测是您正在设置TextView的底部约束(这限制了它滚动其全部内容)。

Try putting the TextView as a subview of a standard UIView and set the leading/trailing/bottom/top constraints of that view (for the total area you want the TextView to be displayed in), then put the TextView within that and set the leading/trailing/top NOT BOTTOM (I'm assuming you want it to be able to scroll vertically) constraints, it will complain about no height/y constraint so go ahead and set the centerY equal to the view that's holding it's centerY (ie Center in view vertically). 尝试将TextView作为标准UIView的子视图,并设置该视图的前导/后部/底部/顶部约束(针对要在其中显示TextView的总区域),然后将TextView放入其中并设置前导/ trailing / top NOT BOTTOM (我假设您希望它能够垂直滚动)约束,它将抱怨没有高度/ y约束,因此继续设置centerY等于保持其为centerY的视图(即Center垂直查看)。

I've attached an example of what the constraint look like (I've added the views above and below just to show that it can be constrained to an area based on the view that's holding it's constraints) 我已附加了约束外观的示例(我在上方和下方添加了视图,只是为了表明可以根据持有约束的视图将其约束到某个区域)

在此处输入图片说明

在此处输入图片说明

Your Code is here below with the minor adjustments: 您的代码在此处进行了细微调整:

#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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM