UILabel
のテキストが切り捨てられると、デフォルトでは3つのドットが挿入されます。 これらの文字を変更するか、無効にすることはできますか?UILabelで切り捨て文字を変更するには?
答えて
ます。また、これにより
[lbl setAdjustsFontSizeToFitWidth:YES];
を設定することができますされて あなたが欲しいものは、テキストを切り捨てる必要はないだろうかと、あなたのlabel
に完全なテキストを表示することができます。
私はカスタムフォント(UIAppFonts-keyで設定)を使用しているために動作しません – Felix
-[UILabel setLineBreakMode:]
およびUILineBreakModeCharacterWrap
をご覧ください。 -[UILabel lineBreakMode]
のデフォルト値はUILineBreakModeTailTruncation
で、最後に省略記号が表示されます。
Javanatorは、自分で切り捨てる必要があると言いました。 NSString
クラスのUIKitの追加で sizeWithFont:forWidth:lineBreakMode:
メッセージを使用して、特定のフォントの文字列の幅を取得してください。これにより、すべてのタイプのフォントが処理されます。
私はあなたのコードところ、これまでに開くことができますカスタム切り捨てクラスを書かれています。以下の方法を使用してください。切り捨てが行われた場合はtrueを返し、ラベルのデフォルトのフレーム幅を使用する場合はMaxWidthを0のままにできます。 maxWidthをフレーム幅よりも小さくして、フレーム境界内で短くします。 (変換のためのいくつかの迅速な3件のコメント付き)
スイフト2
使用:
Truncater.replaceElipsis(forLabel: label, withString: "???")
let didTruncate = Truncater.replaceElipsis(forLabel: label, withString: "1234", andMaximumWidth: 50) //maxWidth is not number of chars, but label width in CGFloat
クラス:
import UIKit
class Truncater {
class func replaceElipsis(forLabel label:UILabel, withString replacement:String) -> Bool {
return replaceElipsis(forLabel: label, withString: replacement, andMaximumWidth:0)
}
class func replaceElipsis(forLabel label:UILabel, withString replacement:String, andMaximumWidth width:CGFloat) -> Bool {
if(label.text == nil){
return false
}
let origSize = label.frame;
var useWidth = width
if(width <= 0){
useWidth = origSize.width //use label width by default if width <= 0
}
label.sizeToFit()
let labelSize = label.text!.sizeWithAttributes([NSFontAttributeName: label.font]) //.size(attributes: [NSFontAttributeName: label.font]) for swift 3
if(labelSize.width > useWidth){
let original = label.text!;
let truncateWidth = useWidth;
let font = label.font;
let subLength = label.text!.characters.count
var temp = label.text!.substringToIndex(label.text!.endIndex.advancedBy(-1)) //label.text!.substring(to: label.text!.index(label.text!.endIndex, offsetBy: -1)) for swift 3
temp = temp.substringToIndex(temp.startIndex.advancedBy(getTruncatedStringPoint(subLength, original:original, truncatedWidth:truncateWidth, font:font, length:subLength)))
temp = String.localizedStringWithFormat("%@%@", temp, replacement)
var count = 0
while temp.sizeWithAttributes([NSFontAttributeName: label.font]).width > useWidth {
count+=1
temp = label.text!.substringToIndex(label.text!.endIndex.advancedBy(-(1+count)))
temp = temp.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) //remove this if you want to keep whitespace on the end
temp = String.localizedStringWithFormat("%@%@", temp, replacement)
}
label.text = temp;
label.frame = origSize;
return true;
}
else {
label.frame = origSize;
return false
}
}
class func getTruncatedStringPoint(splitPoint:Int, original:String, truncatedWidth:CGFloat, font:UIFont, length:Int) -> Int {
let splitLeft = original.substringToIndex(original.startIndex.advancedBy(splitPoint))
let subLength = length/2
if(subLength <= 0){
return splitPoint
}
let width = splitLeft.sizeWithAttributes([NSFontAttributeName: font]).width
if(width > truncatedWidth) {
return getTruncatedStringPoint(splitPoint - subLength, original: original, truncatedWidth: truncatedWidth, font: font, length: subLength)
}
else if (width < truncatedWidth) {
return getTruncatedStringPoint(splitPoint + subLength, original: original, truncatedWidth: truncatedWidth, font: font, length: subLength)
}
else {
return splitPoint
}
}
}
オブジェクティブC
+ (bool) replaceElipsesForLabel:(UILabel*) label With:(NSString*) replacement MaxWidth:(float) width
クラス:
//=============================================Header=====================================================
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface CustomTruncater : NSObject
+ (bool) replaceElipsesForLabel:(UILabel*) label With:(NSString*) replacement MaxWidth:(float) width;
@end
//========================================================================================================
#import "CustomTruncater.h"
@implementation CustomTruncater
static NSString *original;
static float truncateWidth;
static UIFont *font;
static int subLength;
+ (bool) replaceElipsesForLabel:(UILabel*) label With:(NSString*) replacement MaxWidth:(float) width {
CGRect origSize = label.frame;
float useWidth = width;
if(width <= 0)
useWidth = origSize.size.width; //use label width by default if width <= 0
[label sizeToFit];
CGSize labelSize = [label.text sizeWithFont:label.font];
if(labelSize.width > useWidth) {
original = label.text;
truncateWidth = useWidth;
font = label.font;
subLength = label.text.length;
NSString *temp = [label.text substringToIndex:label.text.length-1];
temp = [temp substringToIndex:[self getTruncatedStringPoint:subLength]];
temp = [NSString stringWithFormat:@"%@%@", temp, replacement];
int count = 0;
while([temp sizeWithFont:label.font].width > useWidth){
count++;
temp = [label.text substringToIndex:(label.text.length-(1+count))];
temp = [temp stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; //remove this if you want to keep whitespace on the end
temp = [NSString stringWithFormat:@"%@%@", temp, replacement];
}
label.text = temp;
label.frame = origSize;
return true;
}
else {
label.frame = origSize;
return false;
}
}
+ (int) getTruncatedStringPoint:(int) splitPoint {
NSString *splitLeft = [original substringToIndex:splitPoint];
subLength /= 2;
if(subLength <= 0)
return splitPoint;
if([splitLeft sizeWithFont:font].width > truncateWidth){
return [self getTruncatedStringPoint:(splitPoint - subLength)];
}
else if ([splitLeft sizeWithFont:font].width < truncateWidth) {
return [self getTruncatedStringPoint:(splitPoint + subLength)];
}
else {
return splitPoint;
}
}
@end
- 1. UILabel記号を切り捨てる
- 2. UILabelを500文字に制限し、IOSlabelを切り捨てます
- 3. KRL:文字列を切り捨てる
- 4. NSAttributedString UILabelの末尾切り捨て
- 5. 文字列を切り捨てます。
- 6. 切り捨ては、文字列
- 7. PLSQL:切り捨てによる数字の変換と切り捨て
- 8. 文字数でJava文字列を切り捨てます
- 9. SASでインポートするときに文字を切り捨てる
- 10. xsd切り捨て文字列C#
- 11. 文字列からnアルファベット文字を切り捨てる
- 12. Powershell - 文字列内の文字を切り捨てます
- 13. 文字列内の空白文字を切り捨てます
- 14. 文字列の文字を切り捨てます。
- 15. insert文のSQLを長い文字列に切り捨てる
- 16. MySQL CONCATは文字列を内部で切り捨てます。
- 17. SHA256を切り捨てずに文字列をGuidに変換する
- 18. UILabelの切り捨て "..."(スペース+ ...)の代わりに "..."
- 19. UILabelテキストが大きなフォントサイズで切り捨てられる
- 20. 文字列の最後の2文字を切り捨て、Oracleで数値に変換する方法は?
- 21. IE8はjson文字列を1986文字に切り捨てます
- 22. ActionBarのタイトルは1文字に切り捨てられます
- 23. 文字列メソッドを使用してリスト内の文字列を変更しました - 切り捨て
- 24. QLabelはサイズ変更時にテキストを切り捨てます
- 25. 複数行UILabelが切り捨てられています
- 26. pysparkで文字列を切り捨てる
- 27. Javascriptで文字列を切り捨てる(制約付き)
- 28. lessで開くときヌル文字を切り捨てる
- 29. Python - 文字列中の文字列を切り捨てて文字列を文字列に連結する
- 30. AJAX "00"文字列を "0"に切り捨てる
はFYI 3つの少しドットは 'ellipsis' – Jacksonkr