I know this has been asked before, (I several posts on reddit), but I thought I'd come to the definitive source for a real answer. Is there a way to determine the length of a string in pixels?
I don't have the actual meters in fron tof me, but what I'm trying to accomplish is (what should be) a simple Month /day/suffix// year string, but when I try aligning them from the right I get the S in September cut in half, like I've run out of window space, and it makes no difference what I set the window size to. If I align to the Left, when the month changes to say July, I end up with a huge gap between the Month and the date. I know I'm doing something wrong, but I can't figure out what. But I know knowing pixel positions would really be a help.
Any ideas?
It is currently April 19th, 2024, 11:08 am
String size in pixels
-
- Posts: 23
- Joined: September 11th, 2012, 3:36 am
-
- Rainmeter Sage
- Posts: 2603
- Joined: March 23rd, 2015, 5:26 pm
Re: String size in pixels
Do you really need to display these in different meters? Unless you want different vertical spacing, you can display all these in a single meter, using inline options if needed. That way this issue isn't a problem. If you DO need different meters, then DON'T set the meter width (let it default), then use relative positioning on the meters (not absolute positioning).
This will place the day 2 pixels after the month, and the year 2 pixels after the day, with no need to know how wide anything is.
Otherwise, just put everything in a single meter, like so:
Inline Options are very powerful, so if you need something fancy, try that instead of either of the above.
If you really really want to know the width of a string, you can create a hidden string meter, set the text to the value you want, and then get the width using syntax like [HiddenMeter:W], which gets you the width of the meter HiddenMeter. See Section Variables.
Code: Select all
[MeterMonth]
MeasureName=YourMonthNameMeasure
Text=%1
X=0
Y=0
[MeterDay]
MeasureName=YourDayMeasure
Text=%1
X=2R
Y=0
[MeterYear]
MeasureName=YourYearMeasure
Text=%1
X=2R
Y=0
Otherwise, just put everything in a single meter, like so:
Code: Select all
[MeterMonth]
MeasureName=YourMonthNameMeasure
MeasureName=YourDayMeasure
MeasureName=YourYearMeasure
Text=%1 %2 %3
X=0
Y=0
If you really really want to know the width of a string, you can create a hidden string meter, set the text to the value you want, and then get the width using syntax like [HiddenMeter:W], which gets you the width of the meter HiddenMeter. See Section Variables.
-
- Posts: 23
- Joined: September 11th, 2012, 3:36 am
Re: String size in pixels
SilverAzide, You're right, I'm making this much more complicated than I have to. But I don't set the width at all in this skin, and I almost certain the first example you showed is what I tried doing and i didn't work. I must be doing something wrong. Probably some simple syntax error. I'll check it out again. Thanks.
-
- Rainmeter Sage
- Posts: 16144
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Re: String size in pixels
Yep, there is a solution.
If besides SilverAzide's reply you still want to know the width (size) of the meter, you can use the :W (and :H for height) parameter of the section variables. Since you didn't post a code, here is an example:
Code: Select all
[Rainmeter]
DynamicWindowSize=1
[MeasureTime]
Measure=Time
Format=%#c
[MeterTime]
Meter=String
MeasureName=MeasureTime
Fontsize=15
FontFace=Segoe UI
FontColor=220,220,220
AntiAlias=1
StringAlign=Left
x=0
y=0
Text=%1
SolidColor=0,0,0,150
Padding=15,5,15,5
[MeterWidth]
Meter=String
MeasureName=MeasureTime
Fontsize=15
FontFace=Segoe UI
FontColor=220,220,220
AntiAlias=1
StringAlign=Left
x=0r
y=5R
Text=[MeterTime:W]
DynamicVariables=1
SolidColor=0,0,0,150
Padding=15,5,15,5
What you have to know here is that you absolutely need the DynamicVariables=1 option on all measures or meters where you do want to use the width returned by the [MeterTime:W] section variable (because section variables require to set on the dynamic variables).
-
- Rainmeter Sage
- Posts: 7125
- Joined: February 27th, 2015, 2:38 pm
- Location: Terra Yincognita
Re: String size in pixels
This. To avoid confusion though, "hidden" should be replaced by "invisible", since, strictly speaking, a hidden meter has its width and height set to 0, as per the manual.SilverAzide wrote: ↑September 12th, 2020, 12:46 am If you really really want to know the width of a string, you can create a hidden string meter, set the text to the value you want, and then get the width using syntax like [HiddenMeter:W], which gets you the width of the meter HiddenMeter. See Section Variables.
Invisible meters are a great (and only, as far as I know) "automated" way of setting a subsequent meter's dimensions to be an exact fit of the largest possible value the latter could take, according to the font's characteristics.
Last edited by Yincognito on September 12th, 2020, 8:40 am, edited 1 time in total.
-
- Posts: 23
- Joined: September 11th, 2012, 3:36 am
Re: String size in pixels
Balala / Yincognito, this may be the answer I was looking for, thanks! I'm going to play around and see what works best. Thanks again.
-
- Rainmeter Sage
- Posts: 7125
- Joined: February 27th, 2015, 2:38 pm
- Location: Terra Yincognita
Re: String size in pixels
Yeah, I was on mobile at that time and couldn't post a basic sample code, so here it is (notice the Alpha value of 0 in [MeterMonthMaxW]'s FontColor option, which makes it invisible):
Code: Select all
[Variables]
[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1
BackgroundMode=2
SolidColor=47,47,47,255
---Measures---
[MeasureMonth]
Measure=String
String=May
---Meters---
[MeterMonthMaxW]
Meter=STRING
FontFace=Tahoma
FontColor=255,255,255,0
FontSize=10
AntiAlias=1
Text="September"
UpdateDivider=-1
[MeterMonth]
Meter=STRING
FontFace=Tahoma
FontColor=255,255,255,255
FontSize=10
AntiAlias=1
X=0r
Y=0r
MeasureName=MeasureMonth
Text="%1"
Code: Select all
StringAlign=Right
X=0R
Also, assuming you don't know which of "December" and "September" strings is the longest in pixels (or you don't want to bother finding out), you can use something like this:
Code: Select all
[Variables]
[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1
BackgroundMode=2
SolidColor=47,47,47,255
---Measures---
[MeasureMonth]
Measure=String
String=May
---Meters---
[MeterMonthMaxH]
Meter=STRING
FontFace=Tahoma
FontColor=255,255,255,0
FontSize=10
AntiAlias=1
Text="bp"
[MeterMonthMaxW]
Meter=STRING
FontFace=Tahoma
FontColor=255,255,255,0
FontSize=10
AntiAlias=1
X=0r
Y=0r
H=[MeterMonthMaxH:H]
Text="September#CRLF#December"
DynamicVariables=1
[MeterMonth]
Meter=STRING
FontFace=Tahoma
FontColor=255,255,255,255
FontSize=10
AntiAlias=1
X=0r
Y=0r
W=[MeterMonthMaxW:W]
H=[MeterMonthMaxH:H]
MeasureName=MeasureMonth
Text="%1"
DynamicVariables=1
-
- Rainmeter Sage
- Posts: 16144
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Re: String size in pixels
Alright, I'm gladif you found the answer. If any other question arises, fell free to come back with it.