gpt4 book ai didi

ios - 我们如何使用自动布局以编程方式根据数据增加 UITextview 的大小

转载 作者:行者123 更新时间:2023-11-30 14:09:52 28 4
gpt4 key购买 nike

嗨,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.
}

关于ios - 我们如何使用自动布局以编程方式根据数据增加 UITextview 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31874288/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com