2016-10-26 6 views
1

私は帰属テキストを含むUITextViewを持っています。 私はそれを中心にしようとしています(テキストが帰属するため、textView.TextAlignmentは機能しません)。AttributedTextの配置方法は?

どうすればいいですか? 私はこの試みた:

var attributes = new CTStringAttributes() { }; 
var paragraphStyleSettings = new CTParagraphStyleSettings() { Alignment = CTTextAlignment.Center }; 
attributes.ParagraphStyle = new CTParagraphStyle(paragraphStyleSettings); 
textView.AttributedText = attributedString; 

をしかし、私は、次のクラッシュを取得:Foundation.MonoTouchException - NSInvalidArgumentException [NSCFType textBlocks]: unrecognized selector sent to instance ... enter image description here

だから私はそれがCTParagraphStyleSettingsが使用されるべき正しい方法ではないだと思いますか?しかし、私はXamarinの構文で使用例を見つけることができませんでした...どのように私のテキストの水平方向の配置を設定することができますか?

答えて

3

スウィフトでは、CoreTextの代わりにUIKitのクラスを使用します。

let paragraphStyle = NSMutableParagraphStyle() 
    paragraphStyle.alignment = .center 
    let attributes = [NSParagraphStyleAttributeName: paragraphStyle, NSForegroundColorAttributeName : UIColor.gray, NSFontAttributeName : UIFont(name: "Helvetica", size: 18)!] 
    let attributedString = NSAttributedString(string: "some text", attributes: attributes) 

私はXamarinプログラマじゃないと、私のC#は錆びですが、Xamarinはequivalent functionsを持っています。

+3

ありがとうございます!もし誰かが同じことを探しているのであれば、Xamarinの構文で 'var attributes = new UIStringAttributes(){}; var paragraphStyle = new NSMutableParagraphStyle(){}; \t \t \t paragraphStyle.Alignment = UITextAlignment.Center; \t \t \t attributes.ParagraphStyle = paragraphStyle; ' –