2017-11-12 6 views
1

a text that has some part with yellow background黄色のテキストの左右にいくつかのパディングが必要で、センターにするためにラベル "3"が必要です。私はthis.Belowが私に助けてくれます。使用しています.Attaching screenshotも同様です。アトリビュートされた文字列

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; 
     } 

答えて

0

私が唯一の解決策は、適切な値で(KerningAdjustmentで使用されている)offsetを設定することで怖いです。ここ

比較

  • オフセット= 0.5

enter image description here

  • オフセット= 10
enter image description here

であります

+0

しかし、それは私に3つの値を与えることはありません。左側に余分なパディングがありません。 –

+0

AFAIK、 'ParagraphStyle'は文字列全体に作用しないので、単一の値を中央に置くことはできません。 –