Calculate width of text string in default font/size - r

Is it at all possible to calculate the width size (in inches) of a particular string in the default font ( par()$family ) , and default size ( par()$ps ).
Calculating the height seems relatively simple:
par()$ps * 1/72
The challenge with width is that it is dependent on the string itself. A string of i's "iiiiii" is smaller than a string of o's "ooooooo"

You are looking for the function strwidth in base
strwidth('this is cool', font = 12, units = 'in')

Related

Mathematically scale large elements lesser then small elements

10 elements with the class xxx have different widths and heights. Putting transform: scale(1.1) enlarges the big ones clearly but the small ones barely show difference. This is bad UX. The mathematical question is how to make the bigger elements scale less then the smaller ones:
width 10 should get scale 1.1
width 5 should get scale 1.2
How can i mathematically solve this?
The question lacks context and details, so it is hard to give a generally meaningful answer. However, the given examples indicate the following solution:
x_new = 1 + 1/x_old
Where x_old is the input value, i.e. 10 or 5.
Using logarithmic scaling instead of just 1/x_old might be another option, depending on the context.
To illustrate the scenarios i made these pens:
non logarithmic scale: http://codepen.io/anon/pen/bwRVpj
logarhitmic scale: http://codepen.io/anon/pen/VKWLJK
var inlineStyle = ''
var divs = document.getElementsByTagName('div')
var len = divs.length
while(len--) {
var elWidth = divs[len].offsetWidth
var scale = 1+9/elWidth
inlineStyle += `#${divs[len].id}:hover {
transform: scale(${scale})
}`
}
document.getElementById('lolStyle').innerHTML = inlineStyle

How come sizeHint() is QSize(-1, -1)?

For the following, I get self.page1.sizeHint() and self.page1.minimumSizeHint() both as QSize(-1, -1). Does anyone know why? I was expecting the minimum size hint should be at least the size of the label.
# ...
self.page1 = QtGui.QWidget()
self.page1.setObjectName("page_General")
self.label_Server = QtGui.QLabel(self.page1)
self.label_Server.setGeometry(QtCore.QRect(20, 20, 39, 13))
self.label_Server.setObjectName("label_Server")
print self.page1.sizeHint(), self.page1.minimumSizeHint()
Thanks
The default implementation of sizeHint():
This property holds the recommended size for the widget.
If the value of this property is an invalid size, no size is
recommended.
The default implementation of sizeHint() returns an invalid size if
there is no layout for this widget, and returns the layout's preferred
size otherwise.
Same goes for minimumSizeHint.

How to Generate Placeholders in Stylus

I'm looking to generate placeholders and variables that can change depending on configured proportions such as the following:
$small-margin-top
$large-margin-top
$small-padding-bottom
Where each placeholder applies the corresponding, generated variable to the rule:
$small-margin-top
margin-top $marginsmall
$large-margin-top
margin-top $marginLarge
$small-padding-bottom
margin-bottom $marginSmall
I have statically defined the variables for now:
/* Margin */
$margin = 1rem
$marginsmall = $margin / $multiplier
$marginlarge = $margin * $multiplierLarge
$marginmini = $marginSmall / $multiplierLarge
But I get an error:
TypeError: expected "name" to be a string, but got ident:marginmini
properties = margin padding
proportions = mini small medium large
directions = top left bottom right
for property in properties
for proportion in proportions
for direction in directions
${property}-{direction}-{proportion}
{property}-{direction} lookup(property + proportion)
How can I generate placeholders for my proportions variable, such that the generated placeholders may be extended later (#extend $margin-large)?
EDIT: here is the working solution
The lookup bif accepts a string, and you are passing an ident (margin, padding, etc. without quotes). You can convert them to string by using concatenation. Also, you miss a $ sign:
properties = margin padding
proportions = mini small medium large
directions = top left bottom right
for property in properties
for proportion in proportions
for direction in directions
${proportion}-{property}-{direction}
{property}-{direction} lookup('$' + property + proportion)

NSString boundingRectWithSize bounding not being respected

Does anyone know of a work around or what I am currently doing wrong here.
CGSize boundingSize = CGSizeMake(288, 9999);
CGRect boundingRect = [text boundingRectWithSize:boundingSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:[NSDictionary dictionaryWithObjectsAndKeys:theFont, NSFontAttributeName, nil]
context:nil];
When the code runs on iOS7 (both on the phone and sim) the size returned is 416.3 wide with a height of 15.5. Clearly the boundingRectWithSize is just being ignored. Has anyone else come across this and if so has anyone an idea on how to fix it or can someone point me to where I am going wrong.
As per the Apple documentation:
You can use this method to compute the space required to draw the
string. The constraints you specify in the size parameter are a guide
for the renderer for how to size the string. However, the actual
bounding rectangle returned by this method can be larger than the
constraints if additional space is needed to render the entire string.
Typically, the renderer preserves the width constraint and adjusts the
height constraint as needed.
If you specify a fixed font and too small a space, something has to give. In this case, it's the bounding space. I'm guessing you expected line wrapping. Does a width of 288 allow any reasonable wraps?
I knows that this is old question, but I found a workaround for your problem. below is the code example
1) First create a macro for minimum height
#define MIN_HEIGHT 10.0f
2) After that use below code to give variable height based on your text specified. But for this you need to specify the frame for UILabel OR whatever you are using for displaying the text.
// Initialize UILabel with initial frame.
UILabel *lblMakeModel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 180, 50)];
// Set numberOfLines as zero
lblMakeModel.numberOfLines = 0;
// Set text here
lblMakeModel.text = #"sdbsbdjhsbdhjsbdhjasd bhbdhjasbdsahjdbahjsdbjhsd bdhjsabdhjsbdhsbdhsad dhbsadbasdhbsajhdbsadyogi";
// create a constraint for fixed width and maximum 20000 height.
CGSize constraint = CGSizeMake(lblMakeModel.frame.size.width, 20000.0f);
// Get the CGRect with the given constraint for the text of UILabel
CGRect textRect = [lblMakeModel.text boundingRectWithSize:constraint
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:#{NSFontAttributeName:lblMakeModel.font}
context:nil];
// Set LineBreakMode for UIlabel
[lblMakeModel setLineBreakMode:NSLineBreakByWordWrapping];
[lblMakeModel setAdjustsFontSizeToFitWidth:NO];
// Again set the frame from the height you get from CGRect object.
[lblMakeModel setFrame:CGRectMake(lblMakeModel.frame.origin.x, lblMakeModel.frame.origin.y, lblMakeModel.frame.size.width, MAX(textRect.size.height, MIN_HEIGHT))];
There is a real bug still exist in the method "boundingRectWithSize", sometimes it gives wrong width greater than the limited width, i solved it by this work around and get the label height after the method "sizeToFit"
UILabel *textLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0,maxTextWidth, CGFLOAT_MAX)];
[textLabel setNumberOfLines:0];
[textLabel setFont:Font];
[textLabel setLineBreakMode:NSLineBreakByWordWrapping];
[textLabel setText:#"Text bla bla bla"];
[textLabel sizeToFit];
float height = textLabel.frame.size.height;

Is css property "top" a string or number in javascript?

say I write the following javascript code:
var top = document.getElementById("SOMEDIVID").style.top;
Would the variable "top" end up storing the top value as a string or as a number? I want it to be a number.
It is a complex value - a number followed by a unit.
If a unit is missing, it is assumed to be pixels (px).
So, all of these are valid:
80px
80
50%
17em
use parseInt() then
var top = parseInt(document.getElementById("SOMEDIVID").style.top);
It would be a string. You can split off the integer with regular expression then a recast.
parseInt(top.match(/^\d+/)[0]); // integer only
This will tell you if it's a string or a number:
var type = typeof( top );
If it's a string, this will give you a number
var number = parseInt( top );

Resources