ios - 属性字符串
<p><p> <a href="/image/sgLcQ.png" rel="noreferrer noopener nofollow">a text that has some part with yellow background</a>我需要在黄色文本的左侧和右侧进行一些填充,并且需要将标签“3”作为中心。我正在使用属性文本。任何人都可以帮我解决这个问题。下面是我正在使用的代码。附加截图为好吧。</p>
<pre><code>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;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>恐怕唯一的解决方案是设置 <code>offset</code>(在 <code>KerningAdjustment</code> 上使用)具有适当的值。 </p>
<p>对比</p>
<ul>
<li>偏移量 = 0.5</li>
</ul>
<p> <a href="/image/c4tsE.png" rel="noreferrer noopener nofollow"><img src="/image/c4tsE.png" alt="enter image description here"/></a> </p>
<ul>
<li>偏移量 = 10</li>
</ul>
<p> <a href="/image/OK5ER.png" rel="noreferrer noopener nofollow"><img src="/image/OK5ER.png" alt="enter image description here"/></a> </p></p>
<p style="font-size: 20px;">关于ios - 属性字符串,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/47247106/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/47247106/
</a>
</p>
页:
[1]