Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UITextField different textRect for editing and static text. #344 #345

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

sdkdimon
Copy link

UITextField+NUI.m fix override_editingRectForBounds method.

…#344

UITextField+NUI.m fix override_editingRectForBounds method.
@Stunner
Copy link
Collaborator

Stunner commented Mar 26, 2016

The code you submitted causes an infinite recursion loop. Please test your additions before submitting them as a pull request. Additionally, I am hesitant to accept code that doesn't have some basic tests written as well.

@sdkdimon
Copy link
Author

Yes i am test it with my project, works as i expect.
About infinite recursion loop, please take a look on override_didMoveToWindow method,
which occurs in UI categories, it also should cause an infinite recursion loop, but it doesn't, seems to be it is runtime magic (method swizzling).

``` javascript`

  • (void)override_didMoveToWindow
    {
    if (!self.isNUIApplied) {
    [self applyNUI];
    }
    [self override_didMoveToWindow];
    }

And what do you mean by tests, test project with this case ?

@sdkdimon
Copy link
Author

NUISwizzler class implements swizzle for editingRectForBounds of UITextField and other methods for all views.

- (void)swizzleAll
{
   ............................
   [self swizzle:[UITextField class] methodName:@"editingRectForBounds:"];
   ............................
}
- (void)swizzle:(Class)class methodName:(NSString*)methodName
{
    SEL originalMethod = NSSelectorFromString(methodName);
    SEL newMethod = NSSelectorFromString([NSString stringWithFormat:@"%@%@", @"override_", methodName]);
    [self swizzle:class from:originalMethod to:newMethod];
}

- (void)swizzle:(Class)class from:(SEL)original to:(SEL)new
{
    Method originalMethod = class_getInstanceMethod(class, original);
    Method newMethod = class_getInstanceMethod(class, new);
    if (class_addMethod(class, original, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) {
        class_replaceMethod(class, new, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, newMethod);
    }
}

@alexeyt820
Copy link

If override_editingRectForBounds only calls the original method, should be editingRectForBounds simply excluded from swizzle?

@sdkdimon
Copy link
Author

I think, reason that override_editingRectForBounds exist, is for future features. At current version i never met of usage it.

@Stunner
Copy link
Collaborator

Stunner commented Apr 23, 2016

Cool if it works. But in order for me to be able to accept this PR, I need it to some with some rudimentary tests to ensure that this feature doesn't break in the future. Please add in tests and I'll be happy to revisit this.

@sdkdimon
Copy link
Author

Sorry, but i am can't write test for this case, this is because that

- (CGRect)editingRectForBounds:(CGRect)bounds;

method is not called by XCTest.framework. I think by this reason NUI project does not have test case for "padding" property of UITextField, which swizzle textRectForBounds method.

// Padding apparently can't be modified during didMoveToWindow
- (CGRect)override_textRectForBounds:(CGRect)bounds {
    if ([self nuiShouldBeApplied] &&
        [NUISettings hasProperty:@"padding" withClass:self.nuiClass]) {
        UIEdgeInsets insets = [NUISettings getEdgeInsets:@"padding" withClass:self.nuiClass];
        return CGRectMake(bounds.origin.x + insets.left,
                          bounds.origin.y + insets.top,
                          bounds.size.width - (insets.left + insets.right),
                          bounds.size.height - (insets.top + insets.bottom));
    } else {
        return [self override_textRectForBounds:bounds];
    }
}

Just run NUIDemo project with my pull request, and you see that it runs correctly.
Or tell me how I can write test for this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants