mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 11:01:21 +08:00
feat(text-view): allow easy subclassing on ios (#8663)
This commit is contained in:
@ -30,7 +30,7 @@ class UITextViewDelegateImpl extends NSObject implements UITextViewDelegate {
|
||||
public textViewShouldBeginEditing(textView: UITextView): boolean {
|
||||
const owner = this._owner.get();
|
||||
if (owner) {
|
||||
owner.showText();
|
||||
return owner.textViewShouldBeginEditing(textView);
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -39,47 +39,28 @@ class UITextViewDelegateImpl extends NSObject implements UITextViewDelegate {
|
||||
public textViewDidBeginEditing(textView: UITextView): void {
|
||||
const owner = this._owner.get();
|
||||
if (owner) {
|
||||
owner._isEditing = true;
|
||||
owner.notify({ eventName: TextView.focusEvent, object: owner });
|
||||
owner.textViewDidBeginEditing(textView);
|
||||
}
|
||||
}
|
||||
|
||||
public textViewDidEndEditing(textView: UITextView) {
|
||||
public textViewDidEndEditing(textView: UITextView): void {
|
||||
const owner = this._owner.get();
|
||||
if (owner) {
|
||||
if (owner.updateTextTrigger === "focusLost") {
|
||||
textProperty.nativeValueChange(owner, textView.text);
|
||||
}
|
||||
|
||||
owner._isEditing = false;
|
||||
owner.dismissSoftInput();
|
||||
owner._refreshHintState(owner.hint, textView.text);
|
||||
owner.textViewDidEndEditing(textView);
|
||||
}
|
||||
}
|
||||
|
||||
public textViewDidChange(textView: UITextView) {
|
||||
public textViewDidChange(textView: UITextView): void {
|
||||
const owner = this._owner.get();
|
||||
if (owner) {
|
||||
if (owner.updateTextTrigger === "textChanged") {
|
||||
textProperty.nativeValueChange(owner, textView.text);
|
||||
}
|
||||
owner.requestLayout();
|
||||
owner.textViewDidChange(textView);
|
||||
}
|
||||
}
|
||||
|
||||
public textViewShouldChangeTextInRangeReplacementText(textView: UITextView, range: NSRange, replacementString: string): boolean {
|
||||
const owner = this._owner.get();
|
||||
if (owner) {
|
||||
const delta = replacementString.length - range.length;
|
||||
if (delta > 0) {
|
||||
if (textView.text.length + delta > owner.maxLength) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (owner.formattedText) {
|
||||
_updateCharactersInRangeReplacementString(owner.formattedText, range.location, range.length, replacementString);
|
||||
}
|
||||
return owner.textViewShouldChangeTextInRangeReplacementText(textView, range, replacementString);
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -88,13 +69,7 @@ class UITextViewDelegateImpl extends NSObject implements UITextViewDelegate {
|
||||
public scrollViewDidScroll(sv: UIScrollView): void {
|
||||
const owner = this._owner.get();
|
||||
if (owner) {
|
||||
const contentOffset = owner.nativeViewProtected.contentOffset;
|
||||
owner.notify(<ScrollEventData>{
|
||||
object: owner,
|
||||
eventName: "scroll",
|
||||
scrollX: contentOffset.x,
|
||||
scrollY: contentOffset.y
|
||||
});
|
||||
return owner.scrollViewDidScroll(sv);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -153,6 +128,59 @@ export class TextView extends TextViewBaseCommon {
|
||||
return this.nativeViewProtected;
|
||||
}
|
||||
|
||||
public textViewShouldBeginEditing(textView: UITextView): boolean {
|
||||
this.showText();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public textViewDidBeginEditing(textView: UITextView): void {
|
||||
this._isEditing = true;
|
||||
this.notify({ eventName: TextView.focusEvent, object: this });
|
||||
}
|
||||
|
||||
public textViewDidEndEditing(textView: UITextView): void {
|
||||
if (this.updateTextTrigger === "focusLost") {
|
||||
textProperty.nativeValueChange(this, textView.text);
|
||||
}
|
||||
|
||||
this._isEditing = false;
|
||||
this.dismissSoftInput();
|
||||
this._refreshHintState(this.hint, textView.text);
|
||||
}
|
||||
|
||||
public textViewDidChange(textView: UITextView): void {
|
||||
if (this.updateTextTrigger === "textChanged") {
|
||||
textProperty.nativeValueChange(this, textView.text);
|
||||
}
|
||||
this.requestLayout();
|
||||
}
|
||||
|
||||
public textViewShouldChangeTextInRangeReplacementText(textView: UITextView, range: NSRange, replacementString: string): boolean {
|
||||
const delta = replacementString.length - range.length;
|
||||
if (delta > 0) {
|
||||
if (textView.text.length + delta > this.maxLength) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.formattedText) {
|
||||
_updateCharactersInRangeReplacementString(this.formattedText, range.location, range.length, replacementString);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public scrollViewDidScroll(sv: UIScrollView): void {
|
||||
const contentOffset = this.nativeViewProtected.contentOffset;
|
||||
this.notify(<ScrollEventData>{
|
||||
object: this,
|
||||
eventName: "scroll",
|
||||
scrollX: contentOffset.x,
|
||||
scrollY: contentOffset.y
|
||||
});
|
||||
}
|
||||
|
||||
public _refreshHintState(hint: string, text: string) {
|
||||
if (this.formattedText) {
|
||||
return;
|
||||
|
Reference in New Issue
Block a user