OS X Accessibility API Minimum Width - objective-c

I found another question and answer explaining how to get the width, height, and position of a window using the Accessibility API. Is there any way to find the minimum size, maximum size, resize increments, etc?
Edit:
My current approach is to use AXUIElementCopyAttributeValue but I'm not sure that's possible . Looking at the reference you can see there are a lot of properties I can access, but I can't find any mention of the minimum or maximum size of the window. Note it seems position is accessible through this API.
Now I was also looking at some sample code called Son of Grab which is also able to access window size and position but I don't believe that method works for minimum or maximum size either.

As far as I can tell, there is no easy way about it. I have found one very crude method of getting the maximum and minimum size of the window.
It involves setting the width and height to a very large number to see how far the window resizes then remembering this size as the maximum, then setting the width and height to a small number and doing the same again. After all this I just reset the width and height to their original values.
The obvious problem with this is the fact that it is very visible to the user that the window has been resized.
As for the resize increments I can't think of (at the moment) any workaround to getting that information.
Anyway here's the code I have for working out the maximum and minimum size:
AXUIElementRef window; // The window
AXValueRef sizeValue = (AXValueRef)[UIElementUtilities valueOfAttribute:#"AXSize" ofUIElement:window];
CGSize windowSize;
AXValueGetValue(sizeValue, kAXValueCGSizeType, &windowSize);
CGFloat windowWidth = windowSize.width;
CGFloat windowHeight = windowSize.height;
// Set it to a very large number
[UIElementUtilities setStringValue:#"w=5000 h=5000" forAttribute:#"AXSize" ofUIElement:window];
AXValueRef maxSizeValue = (AXValueRef)[UIElementUtilities valueOfAttribute:#"AXSize" ofUIElement:window];
CGSize maxWindowSize;
AXValueGetValue(maxSizeValue, kAXValueCGSizeType, &maxWindowSize);
CGFloat maxWindowWidth = maxWindowSize.width;
CGFloat maxWindowHeight = maxWindowSize.height;
NSLog(#"max width = %f. max height = %f.", maxWindowWidth, maxWindowHeight);
// Set it to a very small number
[UIElementUtilities setStringValue:#"w=0 h=0" forAttribute:#"AXSize" ofUIElement:window];
AXValueRef minSizeValue = (AXValueRef)[UIElementUtilities valueOfAttribute:#"AXSize" ofUIElement:window];
CGSize minWindowSize;
AXValueGetValue(minSizeValue, kAXValueCGSizeType, &minWindowSize);
CGFloat minWindowWidth = minWindowSize.width;
CGFloat minWindowHeight = minWindowSize.height;
NSLog(#"min width = %f. min height = %f.", minWindowWidth, minWindowHeight);
// Reset size
[UIElementUtilities setStringValue:[NSString stringWithFormat:#"w=%f h=%f", windowWidth, windowHeight] forAttribute:#"AXSize" ofUIElement:window];
In case you were wondering, UIElementUtilities is a class I've taken from one of the Apple example projects called UIElementInspector.

Related

Dynamically changing size and frame position of UITextView

What I would like to do is to make a custom UITextView to allow dynamically changing size and positions of it like PicLab's text edit function.
PicLab tools article
In sum for my simple questions following
1, How to move a position of TextView at your disposal
2, How to change a size of TextView by finger slide
3, How to dynamically change size of fonts corresponding to size of TextView
That would be great if sharing me with useful links as well as codes.
to move the position of a UITextView (actually of any UI element in iOS), you can modify its frame property. the frame property defines the rectangle that this elements takes on the screen, being defined with x and y positions ( (0, 0) is the top left corner of the screen).
you can also change the size of a UITextView by modifying the frame, this time instead of modifying x and y, you'll have to modify its height and width
an example of setting a UITextField with a width of 200 and height of 50 would look like in the top left corner of the screen looks like:
CGFloat x = 0.0;
CGFloat y = 0.0;
CGFloat width = 2000.0;
CGFloat height = 50.0;
textField.frame = CGRectMake(x, y, width, height)
the changing of the font size will have to be implemented dynamically by a custom function of yours, you can use the class method of UIFont fontWithName:size for this.
Frame cannot be set for coordinates and dimension directly, you have to assign a new object. A quick search found this, Change ios Frame Size(width e height) keeping position (x,y).

iPhone MKMapView: set span/region value to show all pins on map

I'm working on a project (platform iOS 7) in which i required current location with stores around 5km, so how to calculate the span/region value to display all stores with current location on map.
MKMapRect zoomRect = MKMapRectNull;
double inset;
for (id <MKAnnotation> annotation in mapVW.annotations)
{
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
zoomRect = MKMapRectUnion(zoomRect, pointRect);
inset = -zoomRect.size.width * 20;
}
[mapVW setVisibleMapRect:MKMapRectInset(zoomRect, inset, inset) animated:YES];
this is what I'm trying
Thanks
It's not clear what your exact issue is but the following may help:
The calculation of the inset looks wrong. It's setting the inset (padding on the sides) to 20 times the width of the whole zoom area. What you probably want is to set the inset to a small fraction of the entire width. Maybe you meant 0.20 instead of 20.0:
inset = -zoomRect.size.width * 0.20;
You also don't need to repeatedly set the inset inside the for loop since it only depends on the final width. You can move the above line after the for loop before calling setVisibleMapRect.
You mention some issue with the current location. It's not clear what the issue is but maybe you mean that this zooming code doesn't include the current location? If so, maybe the current location hasn't been determined yet when this code is called. Try moving this code to (or also call it from) the didUpdateUserLocation delegate method. Make sure showsUserLocation is YES and that the map view's delegate is set.
By the way: iOS 7 includes the new method showAnnotations:animated: which automatically determines the bounding rectangle for some given annotations and sets the map's visible region for you. It doesn't let you specify a custom inset like you are doing (though the default isn't bad). So instead of the above loop, you would do:
[mapVW showAnnotations:mapVW.annotations animated:YES];
NSArray *anno_Arrr = mapview.annotations;
[mapview showAnnotations:anno_Arrr animated:YES];

Getting the number pixels in width and height inside an UI Object?

Currently I do something that looks a bit fuzzy because I am dealing with points with my UI Object but what I want to do is get the width and height of the UI Object (in my case a UIImageView) in pixels.
Is this possible? I have looked around the documentation but I have not seen anything that looks relevant.
Can anyone assist with this?
Thanks!
Have you tried this?
[object frame].size.height
[object frame].size.width
I'm pretty sure that anything with a visual representation will have a frame attribute to indicate where it's located. Of course, the origin of an object's frame is relative to its container, but the size should always be useable.
Edit/Update:
I misread the initial question, which wants to also convert points --> pixels. Here's how to make sure you get that correctly:
float scale;
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)]) {
// This message was implemented in iOS 4.0
scale = [[UIScreen mainScreen] scale];
} else {
// Anything that is running < 4.0 doesn't have a Retina display
scale = 1.0;
}
Then, multiply the height and width values by scale to get actual pixel count.

Maintain scroll position in UIWebView when resizing

I have a UIWebView which resizes when the device is rotated. When the webview is resized the scroll position from the top of the page remains the same, but since the height of the content is changing, you end up in a different place at the end of the rotation.
The content of the webview is text and images which are not being scaled to fit. At a smaller width the text breaks more making it taller.
Is there a good way to maintain the scroll position?
One way to kind of work around this problem is to adjust the contentOffset of the webviews's scrollview after a rotation. Due to changes in line breaks in texts the overall size of the page may change during a rotation.
So to solve this, you have to save the old size of the webviews content in the "willRotateToInterfaceOrientation"-method, then calculate the relative change in the "didRotateFromInterfaceOrientation"-method and adjust the scrollviews contentOffset. (That 'almost' solve the problem - i say almost because due to the changes in line breaks, you might end up one or two lines off your desired position.)
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
oldSize = self.webView.scrollView.contentSize;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
CGSize newSize = self.webView.scrollView.contentSize;
float xFactor = newSize.width / oldSize.width;
float yFactor = newSize.height / oldSize.height;
CGPoint newOffset = webView.scrollView.contentOffset;
newOffset.x *= xFactor;
newOffset.y *= yFactor;
webView.scrollView.contentOffset = newOffset;
}
Take a look at my solution: https://github.com/cxa/WebViewContentPositioner. In short, I use document.caretRangeFromPoint(0, 0) to capture the Range, and insert an element to track its position. After resizing, use the captured Range info to decide position.

Custom UITextField blurred text

When a UITextField with custom frame (200 × 54 px) loses focus, the text looks blurred.
(source: idzr.org)
Is this a common problem? Is there any workaround / solution known?
I found a number of references to this bug through Google, but everybody worked around it by playing with font sizes. After much hunting I found this thread that says anti-aliasing is applied when a view's frame contains fractional pixel values, e.g. if you calculate its size as a fraction of the super view.
Sure enough, casting the CGRect values to int for the view's frame worked perfectly. So as an example, if you wanted your text field to be centered vertically in the superview, you should use an int cast like this:
textFieldWidth = 300;
textFieldHeight = 31;
offsetX = 0;
offsetY = (superview.bounds.size.height - textFieldHeight) / 2;
textField.frame = CGRectMake((int) offsetX,
(int) offsetY,
(int) textFieldWidth,
(int) textFieldHeight);
There is also the CGRectIntegral function that you can use to convert a CGRect to integral values.