9

テキストを両端揃えにしたいのですUILabelが、うまくいかないようです。ただし、別のアラインは左、右、中央の作業です。
XCode 7.2 を使用しています。シミュレーターと実際のデバイスでテストしましたが、同じ問題が発生します

位置揃え ここに画像の説明を入力

私のテキスト:

Don't worry, your data will not be sold.Don't worry,your data wills not be sold. Connecting your accounts will benefit your E score and your profile viewing experience. Don't worry, your data will not be sold.Don't worry, your data wills not be sold. Connecting your accounts will benefit your ECT score and your profile viewing experience.

フォント付き:Helvetica Neue 13.0およびトレーリング/リーディング: 10

ここでテキストを揃えるために align in を使用すると、同じ問題が発生します ここに画像の説明を入力

なぜこれが私に起こったのかわかりません。修正するための指示を教えてください。どんな助けでも大歓迎です

4

5 に答える 5

5

UILabel のバグのようですが、ストーリーボードを少し変更するだけで修正できます。
NSTextAlignments の同じ行にある[詳細] ボタンをクリックし、 のように Head Indent を少し追加し0.1 ます。
ここに画像の説明を入力

あなたの UILabel は問題なく動作します。
ここに画像の説明を入力

于 2016-04-03T10:08:01.400 に答える
2

これはうまくいくはずです。シミュレーターから得られるものは次のとおりです。 ここに画像の説明を入力

私がやったこと:

  1. UILabel をストーリーボードにドラッグ アンド ドロップし、いくつかの制約と色も追加します。
  2. テキストを「帰属」として設定します
  3. テキスト フィールドにテキストを入力します。
  4. 正当化をクリックします。
  5. 警察を変える
  6. 行数 0。

この時点で、次のものが必要です。 ここに画像の説明を入力

ストーリーボードからコントローラーに IBOutlet を追加します (Ctrl + コントローラーの上部にドラッグします)。次のようになります。 ここに画像の説明を入力

viewDidLoad fct にいくつかのコードを追加します。

let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = NSTextAlignment.Justified

        let attributedString = NSAttributedString(string: label.text!, attributes: [ NSParagraphStyleAttributeName: paragraphStyle, NSBaselineOffsetAttributeName: NSNumber(float: 0)
            ])

        label.attributedText = attributedString
        label.numberOfLines = 0

最後に、シミュレーターを実行して、期待どおりに動作するかどうかを確認します。 ここに画像の説明を入力

PS: xCode 7.2 では確実に動作します。それは両方のバージョンで私にとってはうまくいきます。

于 2016-03-25T10:47:00.363 に答える
1

これが今私の問題を解決するための1つの解決策です。alignment justified私は自分のUILabelプログラムで設定し、それは動作します

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
            paragraph.alignment = NSTextAlignmentJustified;

    NSDictionary *attribute = @{
                                        NSParagraphStyleAttributeName: paragraph,
                                        NSFontAttributeName: self.describesLabel.font,
                                        NSBaselineOffsetAttributeName:[NSNumber numberWithFloat:0]
                                        };
    NSAttributedString *attributeMessage = [[NSAttributedString alloc] initWithString:self.describesLabel.text attributes:attribute];
    self.describesLabel.attributedText = attributeMessage;
于 2016-03-28T10:15:10.113 に答える
0

属性付きのテキストを使用しないでください。プレーン テキストを使用し、すべてのテキストを選択して両端揃えにします。ストーリーボードのプレーンテキストに属性を変更することで修正された同じ問題に直面しています。うまくいかない場合は、コードで修正する必要があります:-

属性付きテキストの使用:-

 NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new;
 paragraphStyle.alignment                = NSTextAlignmentJustified;

 NSDictionary *attrsDictionary           = [NSDictionary dictionaryWithObjectsAndKeys:yourFont,NSFontAttributeName,paragraphStyle,NSParagraphStyleAttributeName, nil];

 NSAttributedString *attributeMessage    = [[NSAttributedString alloc] initWithString:yourTextString attributes:attrsDictionary];
 self.yourLabel.attributedText           = attributeMessage;

プレーンテキストの使用:-

self.yourLabel.textAlignment             = NSTextAlignmentJustified;
self.yourLabel.font                      = yourFont;
self.yourLabel.text                      = yourTextString;

それが役に立てば幸い...

于 2016-04-02T12:51:57.607 に答える
0

ストーリーボードまたはペン先から機能しない場合は、プログラムで実行できます

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentJustified;


NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"Your text here"];
                                                             attributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                         paragraphStyle, NSParagraphStyleAttributeName ,
                                                                         [NSNumber numberWithFloat:0],NSBaselineOffsetAttributeName,
                                                                         nil]];

self.yourLabel.attributedText = string;
于 2016-04-03T17:52:41.333 に答える