2016-08-05 37 views
0

Ich versuche, die Begrenzung rect für eine NSAttributedString zu berechnen und dann diese attributierte Zeichenfolge in eine UITextView. Ich möchte die UITextView Basis auf den Text zu dimensionieren. Der Grund, den ich manuell berechnen möchte, ist, weil dieses UITextView in einem UITableViewCell ist und ich nicht automatische Dimension auf meinen Zellenhöhen verwenden möchte.NSAttributedString GetBoundingRect mit Zeichenumbruch nicht Wortumbruch

string myString = "This is some test string that should be long enough to be word wrapped"; 

var boldAttributes = new UIStringAttributes(); 
boldAttributes.ForegroundColor = UIColor.Black; 
boldAttributes.Font = AppDelegate.SegoeUIBold.WithSize (14); 

var attributedString = new NSMutableAttributedString (myString, boldAttributes); 

var rect = attributedString.GetBoundingRect(new CGSize(UIScreen.MainScreen.Bounds.Width - 16, 10000), NSStringDrawingOptions.UsesFontLeading|NSStringDrawingOptions.UsesLineFragmentOrigin, null); 
var height = Math.Ceiling(rect.Height); 

Dies funktioniert die meiste Zeit. Es gibt jedoch einige spezielle Fälle, in denen dies fehlschlägt. In diesem Fall habe ich eine Textzeile, die in 3 Zeilen aufgeteilt wird.

Dies ist einiger Test-String,
sollte lang genug sein Wort
gewickelt zu sein.

Also meine GetBoundingRect sollte eine Höhe groß genug für 3 Zeilen zurückgeben. Aber es gibt nur eine Höhe zurück, die groß genug für 2 Zeilen ist. Ich habe festgestellt, dass, wenn ich die UITextViews Verpackung in die Zeichenverpackung ändern scheint alles gut zu funktionieren. So sieht meine Zeichenfolge wie folgt am Ende:

Dies ist einige Test-String,
e lang genug b sollte Wort gewickelt zu sein.

Ich kann nicht herausfinden, wie attributedString.GetBoundingRect() zu Word-Wrapping statt Zeichenumbruch zu bekommen. Irgendwelche Ideen?

Antwort

-1

Versuchen Sie dieses zu verwenden:

public override void ViewDidLoad() 
    { 
     nfloat padding = 70; 
     nfloat tfWidth = UIScreen.MainScreen.Bounds.Width - 2 * padding; 

     string myString = "This is some test string that should be long enough to be word wrapped"; 

     UILabel tf = new UILabel (new CGRect (padding,100,tfWidth,0)); 
     tf.Text = myString; 
     tf.BackgroundColor = UIColor.Red; 
     tf.Lines = int.MaxValue; 
     tf.LineBreakMode = UILineBreakMode.WordWrap; 
     tf.Font = UIFont.SystemFontOfSize(14); 
     this.View.AddSubview (tf); 

     NSString nsStr = new NSString(myString); 
     nfloat height = nsStr.GetBoundingRect (
      new CoreGraphics.CGSize (tfWidth,float.MaxValue), 
      NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading, 
      new UIStringAttributes(){Font = tf.Font}, 
      null).Size.Height; 
     tf.Frame = new CGRect (tf.Frame.Location, new CGSize (tfWidth, height)); 
    } 

Und in UITableViewSource ich diesen Code verwenden:

public override nfloat GetHeightForRow (UITableView tableView, Foundation.NSIndexPath indexPath) 
    { 
     CellItem item = dataList [indexPath.Row]; 
     NSString nsStr = new NSString(item.Message); 
     nfloat textWidth = item.CellWidth; 
     nfloat height = nsStr.GetBoundingRect (
      new CoreGraphics.CGSize (textWidth,float.MaxValue), 
      NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading, 
      new UIStringAttributes(){Font = item.Font}, 
      null).Size.Height; 
      return height; 
    } 

Sie verschiedene Schrift für jede Zelle verwenden können, wenn Sie wollen.

Ich hoffe, es kann Ihnen helfen.