从RTF转换时如何在NSAttributedText上设置边距

时间:2016-12-08 04:05:48

标签: ios xamarin nsattributedstring rtf

我使用此代码将一些RTF文本转换为NSAttributedString(Xamarin C#,但同样的逻辑适用于Swift / Obj-C)

NSData data = NSData.FromString(RTFString);
NSDictionary opt = NSDictionary.FromObjectAndKey(NSObject.FromObject("NSDocumentTypeDocumentAttribute"), NSObject.FromObject(NSDocumentType.RTF));
NSDictionary docatt = null;
NSError error = null;
NSAttributedString sRTF = new NSAttributedString(data, opt, out docatt, ref error);

这很有效,但在转换RTF文本时,它似乎设置了默认的右边距值,因此文本不会流过UILabel的整个宽度。原始RTF文本中没有设置边距值。

如何更改或设置生成的属性文本的右边距值,使其流过标签的整个宽度?

示例RTF

sample.rtf

{\\rtf1\\ansi\\ansicpg1252\\uc1\\htmautsp\\deff2{\\fonttbl{\\f0\\fcharset0 Times New Roman;}{\\f2\\fcharset0 Segoe UI;}}{\\colortbl\\red0\\green0\\blue0;\\red255\\green255\\blue255;\\red255\\green165\\blue0;}\\loch\\hich\\dbch\\pard\\plain\\ltrpar\\itap0{\\lang1033\\fs26\\f2\\cf0 \\cf0\\ql{\\f2 {\\b\\fs30\\cf2\\ltrch 1.  Gum/Lip Irritation:  }{\\fs24\\ltrch Whitening gel that comes into contact with gum tissue or the lips can become inflammed or bleached.  The inflammation or whitening effect is temporary.  You may feel a stinging and/or tingling sensation on these soft tissues during the treatment.2.  Tooth Sensitivity: You may experience some tooth sensitivity during the first 24 hours after your treatment.  Underlying tooth/gum disease such as existing sensitivity, cracked teeth, open cavities, leaking fillings, exposed roots or other dental conditions may worsen or prolong tooth sensitivity after treatment.3.  Spots or Streaks:  There is a potential to develop white spots or streaks on your teeth due to calcium deposits that naturally occur in teeth.  These spots are not caused by Beaming White gel.  The gel just brings these calcium deposits out and makes them more visible.  This effect will fade over time.4.  Relapse:  After your treatment, it is natural for teeth color to regress over time.  This relapse can be slowed down by avoiding staining agents such as coffee, tea, tobacco, red wine, colas etc.  It is very important to avoid these staining agents, especially during the first 24 hours after treatment.  I understand that the results of my treatment are not permanent, that up to 3 applications may be needed for desired effect and maintanence treatment every recommended very 4-6 months.}\\li0\\ri0\\sa0\\sb0\\fi0\\ql\\par}\r\n}\r\n}

Yellow shows extent of UITextView, with right margin

1 个答案:

答案 0 :(得分:0)

插入一个极右边距(\margrXXXX),或仅0 中的一个也也可以工作,但并不总是(?),以覆盖你的默认边距rtf字符串,允许UILabel.Frame的整个宽度用于段落回流点。

在:

enter image description here

后:

{\rtf1\ansi\ansicpg1252\uc1\htmautsp\margr20000\deff2{\fo...‌​

enter image description here