org.robovm.apple.foundation.NSRange Java Examples

The following examples show how to use org.robovm.apple.foundation.NSRange. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GameViewController.java    From robovm-samples with Apache License 2.0 6 votes vote down vote up
@Override
public void viewDidLoad() {
    super.viewDidLoad();

    gameModel = new GameModel();

    gameOver = false;
    scoreRequestTextField.setDelegate(new UITextFieldDelegateAdapter() {
        // Code to limit our text field to 4 characters.
        @Override
        public boolean shouldChangeCharacters(UITextField textField, NSRange range, String string) {
            final int maxLength = 4;

            int oldLength = textField.getText().length();
            int replacementLength = string.length();
            long rangeLength = range.getLength();
            long newLength = oldLength - rangeLength + replacementLength;

            boolean returnKey = string.indexOf('\n') != -1;

            return newLength <= maxLength || returnKey;
        }
    });
}
 
Example #2
Source File: IOSMini2DxInput.java    From mini2Dx with Apache License 2.0 6 votes vote down vote up
@Override
public boolean shouldChangeCharacters(UITextField textField, NSRange range, String string) {
	for (int i = 0; i < range.getLength(); i++) {
		app.input.inputProcessor.keyTyped((char) 8);
	}

	if (string.isEmpty()) {
		if (range.getLength() > 0)
			Gdx.graphics.requestRendering();
		return false;
	}

	char[] chars = new char[string.length()];
	string.getChars(0, string.length(), chars, 0);

	for (int i = 0; i < chars.length; i++) {
		app.input.inputProcessor.keyTyped(chars[i]);
	}
	Gdx.graphics.requestRendering();

	return true;
}
 
Example #3
Source File: RootViewController.java    From robovm-samples with Apache License 2.0 5 votes vote down vote up
@IBAction
private void urlFieldWasTapped(UITextView sender) {
    // Select the url.
    urlField.setSelectedRange(new NSRange(0, urlField.getText().length()));
    // Show the copy menu.
    UIMenuController.getSharedMenuController().setTargetRect(urlField.getBounds(), urlField);
    UIMenuController.getSharedMenuController().setMenuVisible(true, true);

}
 
Example #4
Source File: AAPLTextViewController.java    From robovm-samples with Apache License 2.0 5 votes vote down vote up
private void handleKeyboardNotification(UIKeyboardAnimation animation) {
    double animationDuration = animation.getAnimationDuration();

    // Convert the keyboard frame from screen to view coordinates.
    CGRect keyboardScreenBeginFrame = animation.getStartFrame();
    CGRect keyboardScreenEndFrame = animation.getEndFrame();

    CGRect keyboardViewBeginFrame = getView().convertRectFromView(keyboardScreenBeginFrame, getView().getWindow());
    CGRect keyboardViewEndFrame = getView().convertRectFromView(keyboardScreenEndFrame, getView().getWindow());
    double originDelta = keyboardViewEndFrame.getOrigin().getY() - keyboardViewBeginFrame.getOrigin().getY();

    // The text view should be adjusted, update the constant for this
    // constraint.
    textViewBottomLayoutGuideConstraint
            .setConstant(textViewBottomLayoutGuideConstraint.getConstant() - originDelta);

    getView().setNeedsUpdateConstraints();

    UIView.animate(animationDuration, 0, UIViewAnimationOptions.BeginFromCurrentState, new Runnable() {
        @Override
        public void run() {
            getView().layoutIfNeeded();
        }
    }, null);

    // Scroll to the selected text once the keyboard frame changes.
    NSRange selectedRange = textView.getSelectedRange();
    textView.scrollRangeToVisible(selectedRange);
}
 
Example #5
Source File: ThreeViewController.java    From robovm-samples with Apache License 2.0 5 votes vote down vote up
@Override
public boolean shouldChangeCharacters(UITextField textField, NSRange range, String string) {
    boolean result = true;

    // restrict the maximum number of characters to 5
    if (textField.getText().length() == 5 && string.length() > 0)
        result = false;

    return result;
}
 
Example #6
Source File: LaunchMe.java    From robovm-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method that parses a URL and returns a UIColor object representing
 * the first HTML color code it finds or nil if a valid color code is not
 * found. This logic is specific to this sample. Your URL handling code will
 * differ.
 */
private UIColor extractColorFromLaunchURL(NSURL url) {
    /*
     * Hexadecimal color codes begin with a number sign (#) followed by six
     * hexadecimal digits. Thus, a color in this format is represented by
     * three bytes (the number sign is ignored). The value of each byte
     * corresponds to the intensity of either the red, blue or green color
     * components, in that order from left to right. Additionally, there is
     * a shorthand notation with the number sign (#) followed by three
     * hexadecimal digits. This notation is expanded to the six digit
     * notation by doubling each digit: #123 becomes #112233.
     */

    // Convert the incoming URL into a string. The '#' character will be
    // percent escaped. That must be undone.
    String urlString = NSURL.decodeURLString(url.getAbsoluteString(), NSStringEncoding.UTF8);
    // Stop if the conversion failed.
    if (urlString == null)
        return null;

    /*
     * Create a regular expression to locate hexadecimal color codes in the
     * incoming URL. Incoming URLs can be malicious. It is best to use
     * vetted technology, such as NSRegularExpression, to handle the parsing
     * instead of writing your own parser.
     */
    NSRegularExpression regex;
    try {
        regex = new NSRegularExpression("#[0-9a-f]{3}([0-9a-f]{3})?", NSRegularExpressionOptions.CaseInsensitive);
    } catch (Exception e) {
        // Check for any error returned. This can be a result of incorrect
        // regex syntax.
        System.err.println(e);
        return null;
    }

    /*
     * Extract all the matches from the incoming URL string. There must be
     * at least one for the URL to be valid (though matches beyond the first
     * are ignored.)
     */
    NSArray<NSTextCheckingResult> regexMatches = regex.getMatches(urlString, new NSMatchingOptions(0), new NSRange(
            0,
            urlString.length()));
    if (regexMatches.size() < 1)
        return null;

    // Extract the first matched string
    int start = (int) regexMatches.get(0).getRange().getLocation();
    int end = start + (int) regexMatches.get(0).getRange().getLength();
    String matchedString = urlString.substring(start, end);

    /*
     * At this point matchedString will look similar to either #FFF or
     * #FFFFFF. The regular expression has guaranteed that matchedString
     * will be no longer than seven characters.
     */

    // Convert matchedString into a long. The '#' character should not be
    // included.
    long hexColorCode = Long.parseLong(matchedString.substring(1), 16);

    float red, green, blue;

    // If the color code is in six digit notation...
    if (matchedString.length() - 1 > 3) {
        /*
         * Extract each color component from the integer representation of
         * the color code. Each component has a value of [0-255] which must
         * be converted into a normalized float for consumption by UIColor.
         */

        red = ((hexColorCode & 0x00FF0000) >> 16) / 255.0f;
        green = ((hexColorCode & 0x0000FF00) >> 8) / 255.0f;
        blue = (hexColorCode & 0x000000FF) / 255.0f;
    }
    // The color code is in shorthand notation...
    else {
        /*
         * Extract each color component from the integer representation of
         * the color code. Each component has a value of [0-255] which must
         * be converted into a normalized float for consumption by UIColor.
         */
        red = (((hexColorCode & 0x00000F00) >> 8) | ((hexColorCode & 0x00000F00) >> 4)) / 255.0f;
        green = (((hexColorCode & 0x000000F0) >> 4) | (hexColorCode & 0x000000F0)) / 255.0f;
        blue = ((hexColorCode & 0x0000000F) | ((hexColorCode & 0x0000000F) << 4)) / 255.0f;
    }
    // Create and return a UIColor object with the extracted components.
    return UIColor.fromRGBA(red, green, blue, 1);
}
 
Example #7
Source File: RootViewController.java    From robovm-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Deselects the text in the urlField if the user taps in the white space of
 * this view controller's view.
 */
@Override
public void touchesEnded(NSSet<UITouch> touches, UIEvent event) {
    urlField.setSelectedRange(new NSRange(0, 0));
}
 
Example #8
Source File: AAPLWebViewController.java    From robovm-samples with Apache License 2.0 4 votes vote down vote up
@Override
public boolean shouldChangeCharacters(UITextField textField, NSRange range, String string) {
    return true;
}
 
Example #9
Source File: AAPLTextFieldViewController.java    From robovm-samples with Apache License 2.0 4 votes vote down vote up
@Override
public boolean shouldChangeCharacters(UITextField textField, NSRange range, String string) {
    return true;
}