gpt4 book ai didi

ios - 根据Xamarin.iOS中的Frame调整UILabel的字体大小

转载 作者:行者123 更新时间:2023-12-01 16:23:30 25 4
gpt4 key购买 nike

我想动态调整UILabel的文本大小。 UILabel的框架将动态更改。每当更改UILabel的Frame时,字体大小应与frame匹配。

我试过下面的代码,但是没有效果。

label.AdjustsFontSizeToFitWidth = true

终于我找到了解决方案。

这是我的计算
    void ResizeLabelFontSize(CGRect frame,UILabel label)
{
int fontSize = 3;
UIFont font = UIFont.SystemFontOfSize(fontSize);
CGSize size = label.Text.StringSize(font);
while (frame.Width > size.Width)
{
fontSize++;
font = UIFont.SystemFontOfSize(fontSize);
size = label.Text.StringSize(font);
}
label.Font = UIFont.SystemFontOfSize(fontSize);
}

最佳答案

您可以看到AdjustsFontSizeToFitWidth定义here
如文档所述,字体大小将由减小为rodt的以适合标签的边界矩形,因此字体不会随着标签的框架而动态变大,但是您可以在开始时将字体设置为非常大以达到您的要求,请参阅我的测试结果 。
程式码片段:

UILabel label;
public override void ViewDidLoad()
{
base.ViewDidLoad();

label = new UILabel(frame: new CGRect(0, 100, 100, 100));
label.BackgroundColor = UIColor.Red;
label.TextColor = UIColor.White;
label.Font = UIFont.SystemFontOfSize(1000);
label.AdjustsFontSizeToFitWidth = true;
label.LineBreakMode = UILineBreakMode.Clip;
label.BaselineAdjustment = UIBaselineAdjustment.AlignCenters;
label.Text = "label";
View.AddSubview(label);
}

partial void ButtonIncrease(UIButton sender)
{
CGRect rect = label.Frame;
rect.Width = 300;
rect.Height = 300;
label.Frame = rect;
}

partial void ButtonDecrease(UIButton sender)
{
CGRect rect = label.Frame;
rect.Width = 100;
rect.Height = 100;
label.Frame = rect;
}
enter image description here

关于ios - 根据Xamarin.iOS中的Frame调整UILabel的字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46428358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com