gpt4 book ai didi

ios - 属性字符串

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:06:25 25 4
gpt4 key购买 nike

a text that has some part with yellow background我需要在黄色文本的左侧和右侧进行一些填充,并且需要将标签“3”居中。我正在为其使用属性文本。任何人都可以帮助我吗?下面是我正在使用的代码。将屏幕截图附加为好吧。

string CardTileText = "3 Shared Documents";
NSAttributedString decoratedText = CardTileText.GetAttributedStringFromHtml("#ffe601","3");


public static NSAttributedString GetAttributedStringFromHtml(this string source, UIColor color, string identifier)
{
var atts = new UIStringAttributes();
UIFont newConnFont = UIFont.FromName("NotoSans-Bold", 16);
NSRange range = GetRangeFor(source, identifier);
NSNumber offset = 5;

NSMutableParagraphStyle para = new NSMutableParagraphStyle
{
Alignment = UITextAlignment.Left
};
NSMutableAttributedString attributedString = new NSMutableAttributedString(source, atts);
attributedString.AddAttribute(UIStringAttributeKey.BackgroundColor, color, range);
attributedString.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Black, range);
attributedString.AddAttribute(UIStringAttributeKey.Font, newConnFont, range);
attributedString.AddAttribute(UIStringAttributeKey.KerningAdjustment, offset, range);
attributedString.AddAttribute(UIStringAttributeKey.ParagraphStyle, para, range);

return attributedString;
}

static NSRange GetRangeFor(string source, string substring)
{
var range = new NSRange
{
Location = source.IndexOf(substring, StringComparison.Ordinal),
Length = substring.Length
};
return range;
}

最佳答案

恐怕唯一的解决办法是为 offset(在 KerningAdjustment 上使用)设置一个合适的值。

对比一下

  • 偏移量 = 0.5

enter image description here

  • 偏移量 = 10

enter image description here

关于ios - 属性字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47247106/

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