简体   繁体   English

Obj-C-检测textView断线?

[英]Obj-C- Detecting textView line break?

When a user is typing inside of my textView, I want to know when a new line begins, as I want to change the height of my textView when this happens.当用户在我的 textView 中打字时,我想知道新行何时开始,因为我想在发生这种情况时更改 textView 的高度。 That said, I know how to accomplish this if the return button is hit - but if a new line just begins automatically (eg a new line is started simply because a user keeps typing), this doesn't seem as simple.也就是说,如果按下返回按钮,我知道如何实现这一点——但如果新行只是自动开始(例如,仅仅因为用户不断输入而开始新行),这似乎并不那么简单。 The code I'm using below works successfully when 'return' is tapped - but I can't seem to get it to detect any sort of line change otherwise.当点击“return”时,我在下面使用的代码可以成功运行 - 但我似乎无法让它检测到任何类型的换行。

Hope I worded this well enough.希望我的措辞足够好。 Thanks!谢谢!

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
 replacementText:(NSString *)text
{


    if ([text isEqualToString:@"\n"]) {
        self.replyField.text=[NSString stringWithFormat:@"%@\n",self.replyField.text];

        CGFloat fixedWidth = self.replyField.frame.size.width;
        CGSize newSize = [self.replyField sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
        CGRect newFrame = self.replyField.frame;
        newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
        self.replyField.frame = newFrame;

        CGRect newFrame1 = self.upView.frame;

        NSLog(@"WHAT IS THE NEW HEIGHT %f", newFrame1.size.height);

         self.upView.frame = CGRectOffset(self.upView.frame, 0, -15);

        return NO;

    }

    // For any other character return TRUE so that the text gets added to the view
    return YES;
}

Im not sure if this is gonna help at all since im also fairly new to obj-c but give it a go我不确定这是否会有所帮助,因为我对 obj-c 也很陌生,但给它一个 go

-(void)textViewDidChange:(UITextView *)textView
  {
    CGFloat fixedWidth = textView.frame.size.width;
    CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGRect newFrame = textView.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), 
    newSize.height);
    textView.frame = newFrame;
 }

Make sure you also disable scrolling确保您还禁用滚动

   textview.scrollEnabled = NO;

To detect the change in number of lines, you can use KVO, as @rmaddy suggested with a comment.要检测行数的变化,您可以使用 KVO,正如 @rmaddy 所建议的那样。

- (void)viewDidLoad {
     [textView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if (change != nil && [keyPath isEqualToString:@"contentSize"] && [object isKindOfClass:UITextView.class]) {
        NSNumber *oldValue = change[NSKeyValueChangeOldKey];
        NSNumber *newValue = change[NSKeyValueChangeNewKey];
        //Do anything you need with the height values here
    }
}

In swift在 swift

override func viewDidLoad() {
    textView.addObserver(self, forKeyPath: "contentSize", options: [.old, .new], context: nil)
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    guard let change = change, keyPath == "contentSize" && object is UITextView else {
        return
    }

    let oldHeight = (change[.oldKey] as! CGSize).height
    let newHeight = (change[.newKey] as! CGSize).height

    //Do anything you need with the height values here
}

deinit {
    // iOS10 was crashing for me when I didn't explicitly remove
    // the observer before destroying the containing object
    inputTextView.removeObserver(self, forKeyPath: "contentSize", context: nil)
}

Try this:尝试这个:

For dynamically change UITextView height you can create property of height constraints and set/connect this to textview height constraints from storyboard .对于动态更改UITextView高度,您可以创建height约束的属性并将其设置/连接到storyboardtextview高度约束。

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *txtViewHeightConstraint;

and then after add didChange for checking textview inputs然后添加didChange以检查textview输入

-(void)textViewDidChange:(UITextView *)textView
{
     [self SetupTextField] ;
}

- (void)SetupTextField{

    CGRect newFrame = self.txtInfo.frame;
    newFrame.size.height = self.txtInfo.contentSize.height;


    NSLog(@"NEW HEIGHT %f", newFrame1.size.height);

    if (newFrame.size.height < 100.0) { // if condition is optional 
        self.txtViewHeightConstraint.constant = newFrame.size.height ;
    }
}

check screenshot for set height constraint of textview检查屏幕截图以获取textview的设置高度约束

UITextView 高度约束

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

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