簡體   English   中英

我們如何使用自動布局以編程方式基於數據來增加UITextview的大小

[英]How can we Growing UITextview size based on data programmatically using Auto-layouts

嗨,我是iOS的目標初學者,我正在嘗試以編程方式在UIViewController上添加UITextView ,在這里我想根據數據大小來增長UItextview,我已經嘗試了一些代碼,但是它不起作用,請幫我一個

AUIAutoGrowingTextView

#import "AUIAutoGrowingTextView.h"

@interface AUIAutoGrowingTextView()

@property (strong, nonatomic) NSLayoutConstraint *heightConstraint;
@property (strong, nonatomic) NSLayoutConstraint *maxHeightConstraint;
@property (strong, nonatomic) NSLayoutConstraint *minHeightConstraint;

@end

@implementation AUIAutoGrowingTextView

-(id) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

-(void) awakeFromNib
{
    [self commonInit];    
}

-(void) commonInit
{
    // If we are using auto layouts, than get a handler to the height constraint.
    for (NSLayoutConstraint *constraint in self.constraints) {
        if (constraint.firstAttribute == NSLayoutAttributeHeight) {
            self.heightConstraint = constraint;
            break;
        }
    }    
    if (!self.heightConstraint) {
        // TODO: We might use auto layouts but set the height of the textView by using a top + bottom constraints. In this case we would want to manually create a height constraint    
    }
}

- (void) layoutSubviews
{
    [super layoutSubviews];

    if (self.heightConstraint) {
        [self handleLayoutWithAutoLayouts];
    }
    else {
        [self handleLayoutWithoutAutoLayouts];
    }

    // Center vertically
    // We're  supposed to have a maximum height contstarint in code for the text view which will makes the intrinsicSide eventually higher then the height of the text view - if we had enough text.
    // This code only center vertically the text view while the context size is smaller/equal to the text view frame.
    if (self.intrinsicContentSize.height <= self.bounds.size.height) {
        CGFloat topCorrect = (self.bounds.size.height - self.contentSize.height * [self zoomScale])/2.0;
        topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect );
        self.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
    }
}

-(void) handleLayoutWithAutoLayouts
{
    CGSize intrinsicSize = self.intrinsicContentSize;
    if (self.minHeight) {
        intrinsicSize.height = MAX(intrinsicSize.height, self.minHeight);
    }
    if (self.maxHeight) {
        intrinsicSize.height = MIN(intrinsicSize.height, self.maxHeight);
    }
    self.heightConstraint.constant = intrinsicSize.height;
}

-(void) handleLayoutWithoutAutoLayouts
{
    // TODO:
}

- (CGSize)intrinsicContentSize
{
    CGSize intrinsicContentSize = self.contentSize;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        intrinsicContentSize.width += (self.textContainerInset.left + self.textContainerInset.right ) / 2.0f;
        intrinsicContentSize.height += (self.textContainerInset.top + self.textContainerInset.bottom) / 2.0f;
    }

    return intrinsicContentSize;
}


@end

主分類:-

#import "ViewController.h"
#import "AUIAutoGrowingTextView.h"

@interface ViewController ()

{
    AUIAutoGrowingTextView * growingTextView;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    growingTextView = [AUIAutoGrowingTextView new];
    growingTextView = [[AUIAutoGrowingTextView alloc]init];
    growingTextView.translatesAutoresizingMaskIntoConstraints = NO;
    growingTextView.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
    growingTextView.maxHeight = 200;
    [self.view addSubview:growingTextView];

    NSLayoutConstraint * constraint2 = [NSLayoutConstraint constraintWithItem:growingTextView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:20.0f];
    [self.view addConstraint:constraint2];

     constraint2 = [NSLayoutConstraint constraintWithItem:growingTextView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:5.0f];
    [self.view addConstraint:constraint2];

    constraint2 = [NSLayoutConstraint constraintWithItem:growingTextView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-5.0f];
    [self.view addConstraint:constraint2];

    constraint2 = [NSLayoutConstraint constraintWithItem:growingTextView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:50.0f];
    [self.view addConstraint:constraint2];
}

@end

我認為您想以編程方式添加自定義UITextView

var textView:AUIAutoGrowingTextView?
override func viewDidLoad() {
    super.viewDidLoad()

    self.textView = AUIAutoGrowingTextView()
    self.textView?.text = "Hello"
    self.textView?.frame = self.view.frame // set frame as per your need.
    self.view.addSubview(self.textView!)
    // Do any additional setup after loading the view, typically from a nib.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM