2012-05-01 10 views
5

私のiPhoneプロジェクトには幅と高さが固定されたUILabelがありますが、ユーザーは見ている。テキストがUILabelのために大きくなることがあります。それは、文字列 '...'が行末に追加されるときです。私はこの文字列を他のものに変更できるかどうか疑問に思います。例えば、 '(もっと)'。UILabelの内容が一致しない場合、テキストの最後にデフォルトの '...'を変更します

ありがとうございます!

+0

ラベルのフォントサイズを自動的に調整するには、ラベルの 'adjustsFontSizeToFitWidth'プロパティをYESに設定することを検討しましたか? – kevboh

+0

@thematerikは、最小フォントサイズに達した後、省略記号を置き換えて切り捨てるときの動作について話していると思います。これは興味深い質問です。 – danh

答えて

1

は残念ながら、そのようなオプションがiOS版には含まれていないように見える、これと同様の質問ごとに:How to change truncate characters in UILabel?

しかし、前述の問題の状態で回答として、これは簡単に自分で行うことができます。あなたが本当にする必要があるのは、文字列が切り捨てられた場所を見つけて、選択した終了文字に必要な量を引くことです。その後、残りの文字列を別の文字列に入れます。

この方法では、この答えにも有用であろう:

Javanatorは、あなたがあなた自身の切り捨てを行う必要があると述べたよう。あなたは特定のフォントで文字列の幅を取得するために、NSStringクラスへのUIKitの追加でsizeWithFont:forWidth:lineBreakMode:メッセージを使用します。これにより、すべてのタイプのフォントが処理されます。あなたは、これはあなたのために働く場合

[self setText:self.label withText:@"Now is the time for all good men to come to the aid of their country" andTruncationSuffix:@" more"]; 

ことができます:私は

- (void)setText:(UILabel *)label withText:(NSString *)text andTruncationSuffix:(NSString *)truncationSuffix { 

    // just set the text if it fits using the minimum font 
    // 
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:label.minimumFontSize]]; 
    if (size.width <= label.bounds.size.width) { 
     label.text = text; 
     return; 
    } 

    // build a truncated version of the text (using the custom truncation text) 
    // and shrink the truncated text until it fits 
    NSInteger lastIndex = text.length; 
    CGFloat width = MAXFLOAT; 

    NSString *subtext, *ellipticalText; 

    while (lastIndex > 0 && width > label.bounds.size.width) { 
     subtext = [text substringToIndex:lastIndex]; 
     ellipticalText = [subtext stringByAppendingString:truncationSuffix]; 
     width = [ellipticalText sizeWithFont:[UIFont systemFontOfSize:label.minimumFontSize]].width; 
     lastIndex--; 
    } 
    label.text = ellipticalText; 
} 

はこのようにそれを呼び出す...そう構築されており、かろうじてこれをテストし、これは興味深い質問だと思った

1

UILabelのサブクラスを追加し、これを使用してsetText:メソッドをオーバーライドし、truncatedSuffixというプロパティを追加することを検討してください。

+0

テキストが設定された時点で、特にAutolayoutを使用している場合、ラベルの幅がまだ決定されていない可能性があります。 – ozgur

+0

@ozgur - ええ、この投稿はかなり古いです、自動レイアウトとすべての新しいタイポグラフィのものがあれば。 'sizeWithFont'さえ廃止されました。多分私はいつかそれを書き直すでしょう。 – danh

1

iOSバージョン8.0を使用している場合は、ResponsiveLabelを使用できます。ここでは、カスタマイズされた切り捨てトークンを提供したり、タップ可能なアクションを定義することができます。

NSString *expansionToken = @"Read More ..."; 
NSString *str = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."; 
NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithString:kExpansionToken attributes:@{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:self.customLabel.font}]; 
[self.customLabel setAttributedTruncationToken:attribString withAction:^(NSString *tappedString) { 
NSLog(@"Tap on truncation text"); 
}]; 
[self.customLabel setText:str withTruncation:YES]; 
関連する問題