Comments (21)
Then to calculate height use this:
public interface ITextMeter
{
double MeasureTextSize(string text, double width, double fontSize, string fontName = null);
}
[assembly: Xamarin.Forms.Dependency(typeof(TextMeterImplementation))]
namespace YourNamespace.iOS
{
public class TextMeterImplementation : ITextMeter
{
//public static Xamarin.Forms.Size MeasureTextSize(string text, double width, double fontSize, string fontName = null)
public double MeasureTextSize(string text, double width, double fontSize, string fontName = null)
{
var nsText = new NSString(text);
var boundSize = new SizeF((float)width, float.MaxValue);
var options = NSStringDrawingOptions.UsesFontLeading | NSStringDrawingOptions.UsesLineFragmentOrigin;
if (fontName == null)
{
fontName = "HelveticaNeue";
}
var attributes = new UIStringAttributes {
Font = UIFont.FromName(fontName, (float)fontSize)
};
var sizeF = nsText.GetBoundingRect(boundSize, options, attributes, null).Size;
//return new Xamarin.Forms.Size((double)sizeF.Width, (double)sizeF.Height);
return (double)sizeF.Height;
}
}
}
[assembly: Xamarin.Forms.Dependency(typeof(TextMeterImplementation))]
namespace YourNamespace.Droid
{
public class TextMeterImplementation : ITextMeter
{
private Typeface textTypeface;
public double MeasureTextSize(string text, double width, double fontSize, string fontName = null) {
var paint = new TextPaint(PaintFlags.AntiAlias | PaintFlags.SubpixelText);
paint.TextSize = (float)fontSize;
paint.SetTypeface(GetTypeface(fontName));
int lineCount = 0;
int index = 0;
int length = text.Length;
while(index < length - 1) {
index += paint.BreakText(text, index, length, true, (float)width, null);
lineCount++;
}
var bounds = new Rect();
paint.GetTextBounds(text, 0, length, bounds);
return (double)lineCount * bounds.Height() + 3;
}
private Typeface GetTypeface(string fontName)
{
if (fontName == null)
{
return Typeface.Default;
}
if (textTypeface == null)
{
textTypeface = Typeface.Create(fontName, TypefaceStyle.Normal);
}
return textTypeface;
}
}
}
from carouselview.
Hi Joshua. I still need to figure it out how to propagate request layout down to each children. Stay tuned. Thanks
from carouselview.
By the way, you dont need to specify HeightRequest for everything. For example grid row * and Auto works. Layouts FillAndExpand works. Padding, spacing properties works. Only some elements like Label, Button, BoxView needs HeightRequest...
from carouselview.
Label HeightRequest = to their font size :)
from carouselview.
@alexrainman Yeah I tried that, but depending on the font, it isn't exactly the right size. I got that working by manually setting it up. It seems that Orientation="Horizontal"
on StackLayouts doesn't work and Positioning in an AbsoluteLayout
doesn't work either.
from carouselview.
Its possible to calculate the height using font size and the amount of text to place. I will do my testing based on your comments. Thanks
from carouselview.
J, you are right, Horizontal StackLayout doesn't works and no idea why :) you can use a two column Grid instead.
from carouselview.
I tested AbsoluteLayout and it works like a charm :)
from carouselview.
And RelativeLayout works too.
from carouselview.
@alexrainman AbsoluteLayout
works depending on what position you use. If you use AutoSize
for height or width then it won't work. I'm using PositionProportional
and I had to define height and width for all the elements to get it to work.
from carouselview.
That's the problem, AutoSize doesn't works. As i said, you must request height 'til i find the way of requesting layout to all children. This the first version of the Control and it took me while to understand the obscure non-documented Xamarin.Forms RenderFactory API. I will share with you a way to calculate label height in a cross-platform way :)
from carouselview.
@alexrainman I figured out a way to calculate the height by just adding a few pixels to the font size, but the issue is calculating the width, it is quite difficult.
from carouselview.
This is what i use to calculate the width:
var width = Navigation.NavigationStack.First().Width - x;
x = sum of padding from left and right sides. If you have columns, then just do the math :)
from carouselview.
I found a fix for this. Expect version 2 soon :)
from carouselview.
Hi, I tested your fix and I left a comment.
Can you help me ?
https://gist.github.com/alexrainman/82b00160ab32bef9e69dee6d460f44fa#comments
Thank you
from carouselview.
Can you use CVLabel custom control provided by the plugin and let me know if height is correct? Thats what i advice, use CVLabel and for the rest of controls provide HeightRequest with OnPlatform using the default ones.
from carouselview.
I've just use your TextMeterImplementation with Xamarin Label. It works on iOS but not on Android.
Now It works on Android for me.
I fixed my issue with this :
public float GetDensity()
{
return global::Android.App.Application.Context.Resources.DisplayMetrics.Density;
}
public double MeasureTextSize(string text, double width, double fontSize, string fontName = null)
{
var textView = new TextView(global::Android.App.Application.Context);
textView.Typeface = GetTypeface(fontName);
textView.SetText(text, TextView.BufferType.Normal);
textView.SetTextSize(ComplexUnitType.Dip, (float)fontSize);
int widthMeasureSpec = Android.Views.View.MeasureSpec.MakeMeasureSpec(
(int)width, MeasureSpecMode.AtMost);
int heightMeasureSpec = Android.Views.View.MeasureSpec.MakeMeasureSpec(
0, MeasureSpecMode.Unspecified);
textView.Measure(widthMeasureSpec, heightMeasureSpec);
var density = global::Android.App.Application.Context.Resources.DisplayMetrics.Density;
return ((double)textView.MeasuredHeight / density) + 5;
}
Example :
var density = textMeter.getDensity() -> 1 for iOS and global::Android.App.Application.Context.Resources.DisplayMetrics.Density for Android
Height = (int)textMeter.MeasureTextSize(alert.CurrentText, (App.ScreenWidth / nbColumns) - 30 * density, 15.0, "Roboto-Regular");
EDIT: 09/01/2016
I didn't work when I changed textsize in android settings (display section) because I used Dip instead of Sp (recommended for text size)
but this work for me now :
public float GetDensity()
{
return global::Android.App.Application.Context.Resources.DisplayMetrics.Density;
}
public double MeasureTextSize(string text, double width, double fontSize, string fontName = null)
{
var textView = new TextView(global::Android.App.Application.Context);
textView.Typeface = GetTypeface(fontName);
textView.SetText(text, TextView.BufferType.Normal);
textView.SetTextSize(ComplexUnitType.Sp, (float)fontSize);
int widthMeasureSpec = Android.Views.View.MeasureSpec.MakeMeasureSpec(
(int)width, MeasureSpecMode.AtMost);
int heightMeasureSpec = Android.Views.View.MeasureSpec.MakeMeasureSpec(
0, MeasureSpecMode.Unspecified);
textView.Measure(widthMeasureSpec, heightMeasureSpec);
var density = global::Android.App.Application.Context.Resources.DisplayMetrics.Density;
return ((double)textView.MeasuredHeight / density) + 5;
}
from carouselview.
It doesn't works for me :(
This is what i use internally in TextMeter Android:
private Typeface textTypeface;
public double MeasureTextSize(string text, double width, double fontSize, string fontName = null)
{
var textView = new TextView(global::Android.App.Application.Context);
textView.Typeface = GetTypeface(fontName);
textView.SetText(text, TextView.BufferType.Normal);
textView.SetTextSize(ComplexUnitType.Px, (float)fontSize);
int widthMeasureSpec = AViews.View.MeasureSpec.MakeMeasureSpec(
(int)width, AViews.MeasureSpecMode.AtMost);
int heightMeasureSpec = AViews.View.MeasureSpec.MakeMeasureSpec(
0, AViews.MeasureSpecMode.Unspecified);
textView.Measure(widthMeasureSpec, heightMeasureSpec);
return (double)textView.MeasuredHeight;
}
private Typeface GetTypeface(string fontName)
{
if (fontName == null)
{
return Typeface.Default;
}
if (textTypeface == null)
{
textTypeface = Typeface.Create(fontName, TypefaceStyle.Normal);
}
return textTypeface;
}
CVLabel.cs
public class CVLabel : Label
{
public CVLabel()
{
SetBinding(Label.HeightRequestProperty, new Binding("WidthRequest", BindingMode.Default, new LabelHeightConverter(), this, null, this));
}
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
WidthRequest = width;
this.LayoutTo(new Rectangle(this.X, this.Y, width, height));
this.InvalidateMeasure();
}
}
And it works with your text or any other.
from carouselview.
Hi, I'm using your implementation because I want to see how much big are my labels (I need to know the measure of my text in pixels). I see that when I set a fontSize of 20 I have i.e. 268 as measured size. What's the measurement unit?
Thank you
from carouselview.
If my work is helping you, please help me back: https://xamarinhq.wufoo.com/forms/nominate-a-xamarin-mvp/
from carouselview.
This is what i have done that is community visible:
- Alex has been pretty active sharing his knowledge and answering questions in StackOverflow, GitHub and Xamarin Forums.
- Alex has been open sourcing Xamarin components and plugin with the community, with more of 10 repositories being maintained on GitHub.
- One of Alex's Xamarin.Forms plugin, the CarouselView, is currently being pulled into the Xamarin.Forms core (xamarin/Xamarin.Forms#853). As an stable widget, this control has become the standard for Xamarin.Forms carousel screens with more that 16K downloads in NuGet and more than 100 solved and not open issues.
- Another Alex's plugin, the SegmentedControl (https://github.com/alexrainman/SegmentedControl), is a big candidate to be also pulled into Xamarin.Forms core.
- Plus Xamarin.Forms UI related plugins, Alex shares plugins to set app icon Badge, Google Analytics and KeyChain access.
- Alex shares his knowledge about cutting edge technologies like Couchbase NoSQL and MobileIron integration with Xamarin Apps.
from carouselview.
Related Issues (20)
- Object Reference Error in Xamarin.Forms 4.8. and higher HOT 5
- Rotate mobile orientation
- iOS: Elements inside carousel are not accessible HOT 1
- Error linking release version 6.0 HOT 1
- error: invalid path '.vs/CarouselView/xs/project-cache/Demo.iOS-Debug|iPhoneSimulator.json'
- 1 mb nuget size?
- [iOS] ShowIndicators at false doesnt work HOT 4
- [BUG][Android] Crash when CarouselView is nested in a ScrollView, inside a TabViewItem (XCT) HOT 6
- not appearing on Android while ok on iOS HOT 4
- [Bug?] CarouselViewControl FlowDirection property wont set the flowdirection HOT 2
- [Request] Start loading the next & previous view HOT 1
- first view is empty sometimes on version 6
- CarouselView does not appear on app start HOT 4
- Using Xamarin.Forms 5.0.0.2244 with upgrade to version 6.0, iOS does not exist in the namespace CarouselView.FormsPlugin
- Xamarin UWP Position Property does not update
- PeekAreaInsets property not found
- Couldn't Viewable Frame Bottom Content Something Covered Bottom Area on Xamairn IOS
- MAUI support HOT 5
- Last item in carouselview is not showing
- (iOS) 6.0.0 CarouselViewRenderer.Dispose Null reference Unhandled Exception:
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from carouselview.