Created
June 20, 2015 21:07
-
-
Save skagedal/e66309912058bb67e50a to your computer and use it in GitHub Desktop.
Plot of error in UILabel's vertical entering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# See http://stackoverflow.com/questions/30759598/how-does-uilabel-vertically-center-its-text/30910504#30910504 | |
# | |
import re | |
import matplotlib.pyplot as pyplot | |
input = open("trace-output.txt").read() | |
entries = input.split("=====") | |
NUMBER_REGEX = "(\d+(?:\.\d+)?)" | |
RECT_REGEX = "{{%s, %s}, {%s, %s}}" % ((NUMBER_REGEX, ) * 4) | |
RE_BOUNDS = re.compile("drawTextInRect:" + RECT_REGEX) | |
RE_LABEL = re.compile("drawWithRect:" + RECT_REGEX) | |
def get_values(s): | |
dict = {} | |
match = RE_BOUNDS.search(s) | |
if match is None: | |
return None | |
boundsHeight = float(match.group(4)) | |
match = RE_LABEL.search(s) | |
if match is None: | |
return None | |
labelOffsetY = float(match.group(2)) | |
labelHeight = float(match.group(4)) | |
return (boundsHeight, labelOffsetY, labelHeight) | |
entries = [get_values(s) for s in entries] | |
entries = [x for x in entries if x is not None] | |
entries = sorted(set(entries), key=lambda tuple: tuple[2]) | |
# entries is now a list of tuples of the form | |
# (boundsHeight, labelOffsetY, labelHeight) | |
# sorted by labelHeight | |
def calcOffset(boundsHeight, labelHeight): | |
return (boundsHeight - labelHeight) / 2.0 | |
calculatedOffsets = [calcOffset(bH, lH) for (bH, lY, lH) in entries] | |
appleOffsets = [lY for (bH, lY, lH) in entries] | |
diffs = [(a - b) for (a, b) in zip(appleOffsets, calculatedOffsets)] | |
labelHeights = [lH for (bH, lY, lH) in entries] | |
def plot(xlabel, x, ylabel, y): | |
pyplot.plot(x, y, "ro") | |
pyplot.xlabel(xlabel) | |
pyplot.ylabel(ylabel) | |
pyplot.show() | |
plot("label heights", labelHeights, "error", diffs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment