LoginSignup
6
7

More than 5 years have passed since last update.

UILabelとUITextViewで、textAlignment設定時における文末の半角/全角スペースの扱いについて

Posted at

概要

interfaceBuilderを利用せずにコードから生成したUILabelを右寄せにした際、文末に入れてあるはずの空白が消えていました。そのときの調査メモです。

環境

  • iOS8,9系シュミレータ,実機
  • Xcode7.1

UILabelで実験

頭の中では文末に入れてある空白がそのまま維持されて右寄せされる事を期待していたのですが、実際には文頭に移動? していた模様です。

textAlignment 文末空白どうなるか
NSTextAlignmentLeft 勿論そのまま
NSTextAlignmentCenter 文頭と文末に均等に分かれる
NSTextAlignmentRight 文頭に移動する

// 文末半角
NSAttributedString *noBreakSpaceTxt = [[NSAttributedString alloc]initWithString:@"UILabel.NoBreakSpaceX5     "];

// 文末全角
NSAttributedString *ideoGraohicSpaceTxt = [[NSAttributedString alloc]initWithString:@"UILabel.IdeoGraohicSpaceX3   "];

NSArray *alignments = @[@(NSTextAlignmentLeft),@(NSTextAlignmentCenter),@(NSTextAlignmentRight),@(NSTextAlignmentLeft),@(NSTextAlignmentCenter),@(NSTextAlignmentRight)];

// 確認用表示
for (int i = 0; i < alignments.count; i++) {

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, baseHeight*(i+1), self.view.frame.size.width, baseHeight)];

    if (i < 3 ) {
        label.attributedText = noBreakSpaceTxt;
    }else{
        label.attributedText = ideoGraohicSpaceTxt;
    }

    label.backgroundColor = [UIColor randomColor]; // カテゴリ利用
    NSNumber *alignment = alignments[i];
    label.textAlignment = alignment.integerValue;
    [self.view addSubview:label];
    [label sizeToFit];
}

UITextViewで実験

上記スニペットのUILabel生成部分をUITextViewに差し替えて確認してみました。

UITextViewでは右寄せにするとUILabelと違って文頭に移動すらせずに、空白はトリミングされてしまう模様です。

右寄せ時でも文末の空白を維持したいときの対応策

結局調べただけで対応はしてないのですのが、Twitterに投げたら\u00a0でreplaceしてみるといけるかも?という情報を上司から頂きました。というかまず社内チャットに投げるべきでした。ありがたやありがたや。

UILabel

UILabelの場合、文末の空白数だけ\u00a0等でリプレイスしようが結局文頭に移動してしまいますのでリプレイスでは右寄せ時の文末空白は維持できません。

stackoverflowの投稿にて、文末にピリオド追加して透明にしてしまえば良いという対応方法もあるようです。

UITextView

文末のスペースが半角スペースならその空白数だけ\u00a0でreplaceすれば似たような表示になります。全角は\u3000でもreplaceできませんでした。

文章が短い場合と絶対に半角スペースしか入ってこない事が保証できるならこの対応で問題ないとは思いますが、長文の時なんかはreplaceが相当呼ばれる可能性もあるので、文末からの空白数えてリプレイスするみたいな処理が必要ですかね。

Technical Support Incident の回答

会社のTSIが結構余っていて使わせてもらったので、テクニカルに問い合わせてみました。

\tでリプレイスするか,上述の透明化する方法を利用してみては?という回答でした。

Hi Sato Sang,

I don’t see a good way to preventing UILabel from trimming the ending space. The following methods seem to work:

1. Replace the space character with a tab character (‘\t’).
2. Use NSAttributedString and set the background color to clear color.

But could you please elaborate why you need to keep the ending space? We can probably figure out a better solution for that.

> DESCRIPTION OF PROBLEM
> I intentionally do want a blank space character at the end of my string.
> And I do not want it to be automatically trimmed away When I set NSTextAlignmentRight for UILabel and UITextView.
>
> As one of the above solutions, It was working in the UITextView with the replace blankSpace to \u00a0.
>
> But in the case of IDEOGRAPHIC SPACE, not working replace space to \u3000.
>
> A better way would be appreciated to tell. And How to stop UILabel from trimming the space at the end ?
> STEPS TO REPRODUCE
> 略

似たようなとこでハマっているstackOverflowの投稿

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7