【问题标题】:Obj-C- Detecting textView line break?Obj-C-检测textView换行符?
【发布时间】:2019-09-26 21:52:56
【问题描述】:

当用户在我的 textView 中键入时,我想知道新行何时开始,因为我想在发生这种情况时更改我的 textView 的高度。也就是说,如果按下返回按钮,我知道如何完成此操作 - 但如果新行只是自动开始(例如,仅仅因为用户不断输入而开始新行),这似乎并不那么简单。当点击“返回”时,我在下面使用的代码可以成功运行 - 但我似乎无法让它检测到任何类型的换行。

希望我的措辞足够好。谢谢!

 - (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;
}

【问题讨论】:

标签: ios objective-c


【解决方案1】:

我不确定这是否会有所帮助,因为我对 obj-c 也很陌生,但试一试

-(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;
 }

确保您也禁用滚动

   textview.scrollEnabled = NO;

【讨论】:

  • 这对您有帮助吗?
  • 干得好,而且如此简单。谢谢你。这简直要了我的命。很想听听其他人对此答案的看法。
【解决方案2】:

要检测行数的变化,您可以使用 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
    }
}

迅速

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)
}

【讨论】:

  • 这很快,但问题是用objective-c标记的。
  • 哎呀,还没看到呢,很快就会添加objc代码。
  • 此方法仅在我点击返回键时检测换行 - 但在我键入时不会检测到自动换行。
【解决方案3】:

试试这个:

对于动态更改 UITextView 高度,您可以创建 height 约束的属性并将其设置/连接到来自 storyboardtextview 高度约束。

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

然后在添加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 ;
    }
}

检查屏幕截图以了解textview 的设置高度限制

【讨论】:

  • 奇怪的是,如果我通过情节提要连接它,一旦我开始输入我的 textView,这实际上会使我的应用程序崩溃。 “由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[UITextView setConstant:]: unrecognized selector sent to instance 0x12c865400'” - 但是,如果我只是将代码设置为我的 textView(例如 self.myTextView.frame 而不是self.txtInfo.frame) - 它给了我 NEW HEIGHT 日志,但不会改变实际 textView 的高度(同样,只有在点击返回时)。
猜你喜欢
  • 2019-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多