UITextView 自动换行 Android

UITextView word-wrap like Android

我有以下 Android TextView:

<TextView
    fontPath="fonts/Ubuntu-Light.ttf"
    android:layout_width="50sp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Moyennement"
    android:id="@+id/txt_score2"
    android:gravity="center"
    android:textSize="8sp"
    android:textColor="@color/indicator_score_text" />

和对应的iOS UITextView(代码):

var smiley = new UITextView { Text = name, ScrollEnabled = false, Font = FontHelpers.GenerateUIFont("Ubuntu-Light", 8), };
smiley.TextContainer.MaximumNumberOfLines = 2;
smiley.TextContainer.LineBreakMode = UILineBreakMode.CharacterWrap;
smiley.SizeToFit();

在 iOS 上,它给了我以下结果:

但在 Android 上,不适合的文本“Moyennement”被正确截断:

我在这里错过了什么? 提前致谢。

试试这个:

<TextView
    fontPath="fonts/Ubuntu-Light.ttf"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Moyenne-\nment"
    android:id="@+id/txt_score2"
    android:gravity="center"
    android:textSize="8sp"
    android:textColor="@color/indicator_score_text" />

我找到了一个解决方案,它结合了 Andreas Oetjen 的建议和使用 UILabel 而不是 UITextView。

NSMutableParagraphStyle paragraphStyle = new NSMutableParagraphStyle();
paragraphStyle.HyphenationFactor = 1.0f;
var hyphenAttribute = new UIStringAttributes();
hyphenAttribute.ParagraphStyle = paragraphStyle;
var attributedString = new NSAttributedString(str: name, attributes: hyphenAttribute);
var smiley = new UILabel { AttributedText = attributedString, Font = FontHelpers.GenerateUIFont("Ubuntu-Light", 10), TranslatesAutoresizingMaskIntoConstraints = false };
smiley.Lines = 2;
smiley.TextAlignment = UITextAlignment.Center;

不再使用 LineBreakMode,因为它与 HyphenationFactor 冲突。这可能也适用于 UITextView。