4 つの基本的なボタン (太字、斜体、下線 + 色) を備えたリッチ テキスト エディター ツールバーを作成しようとしています。
public override UIView InputAccessoryView
{
get
{
UIToolbar toolbar = new UIToolbar(new RectangleF(0, 0, 320, 30));
var fixedSpace = new UIBarButtonItem(UIBarButtonSystemItem.FixedSpace, null)
{
Width = 26
};
var flexibleSpace = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace, null);
boldItem = new UIBarButtonItem("B", UIBarButtonItemStyle.Plain, (o, e) => { GoBold(); });
italicItem = new UIBarButtonItem("I", UIBarButtonItemStyle.Plain, (o, e) => { GoItalic(); });
underlineItem = new UIBarButtonItem("U", UIBarButtonItemStyle.Plain, (o, e) => { GoUnderlined(); });
redItem = new UIBarButtonItem("R", UIBarButtonItemStyle.Plain, (o, e) => { GoRed(); });
toolbar.Items = new UIBarButtonItem[] { underlineItem, fixedSpace, italicItem, flexibleSpace, boldItem, fixedSpace, redItem };
return toolbar;
}
}
protected NSRange GetTextRange()
{
return new NSRange(
textField.GetOffsetFromPosition(textField.BeginningOfDocument, textField.SelectedTextRange.start),
textField.GetOffsetFromPosition(textField.SelectedTextRange.start, textField.SelectedTextRange.end)
);
}
protected void GoItalic()
{
NSMutableAttributedString text = new NSMutableAttributedString(textField.AttributedText);
text.AddAttribute(CTStringAttributeKey.Font, UIFont.ItalicSystemFontOfSize(20f), GetTextRange());
textField.AttributedText = text;
}
protected void GoUnderlined()
{
NSMutableAttributedString text = new NSMutableAttributedString(textField.AttributedText);
text.AddAttribute(CTStringAttributeKey.UnderlineStyle, new NSNumber((int) NSUnderlineStyle.Single), GetTextRange());
//text.AddAttribute(CTStringAttributeKey.UnderlineStyle, new NSNumber(NSUnderlineStyle.Single as int), GetTextRange());
textField.AttributedText = text;
}
protected void GoBold()
{
NSMutableAttributedString text = new NSMutableAttributedString(textField.AttributedText);
text.AddAttribute(CTStringAttributeKey.Font, UIFont.BoldSystemFontOfSize(20f), GetTextRange());
textField.AttributedText = text;
}
protected void GoRed()
{
NSMutableAttributedString text = new NSMutableAttributedString(textField.AttributedText);
text.AddAttribute(CTStringAttributeKey.UnderlineColor, UIColor.Red, GetTextRange());
textField.AttributedText = text;
}
ボールドとイタリックは問題なく機能しますが、下線と色を作成する方法はありません:/、下手なキャストで選択範囲に下線を適用することに成功しましたが、それをどのように行うべきか知りたい.
NSUnderlineStyle.Singleはオブジェクトが期待される列挙型を返すため、キャストの問題に直面しているようです。色についても同じことが言えます