當UILabel
的文本被截斷時,默認情況下會插入3個點。 是否可以更改這些字符或禁用它們?如何更改UILabel中的截斷字符?
3
A
回答
0
0
您還可以設置
[lbl setAdjustsFontSizeToFitWidth:YES];
有了這個就沒有必要截斷的文字,你可以在你的label
顯示完整的文本。
1
看-[UILabel setLineBreakMode:]
和UILineBreakModeCharacterWrap
。 -[UILabel lineBreakMode]
的默認值是UILineBreakModeTailTruncation
,這會導致末尾的省略號。
0
由於Javanator說你不得不做自己的截斷。您可以使用UIKit添加到NSString
類中的 sizeWithFont:forWidth:lineBreakMode:
消息來獲取具有特定字體的字符串的寬度。這將處理所有類型的字體。
9
我寫了一個自定義的截斷類,您可以跳進你的代碼在以往任何時候。請使用下面的方法。如果截斷髮生,它將返回true,如果你只想使用標籤的默認幀寬,MaxWidth可以保留爲0。將maxWidth設置爲小於幀寬度的值,以在其幀範圍內縮短它。
夫特2(帶有用於將一些迅速3條評論)
用法:
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尾截斷
- 3. UILabel截斷
- 4. UILabel中的NSAttributedString尾截斷
- 5. 將UILabel限制爲500個字符並截斷IOS中的UIlab
- 6. UILabel被截斷的文本
- 7. UILabel的文本截斷
- 8. 如何檢查UILabel是否被截斷?
- 9. UILabel多行截斷中間整個字符串不工作
- 10. UILabel文本被截斷爲大字號
- 11. Powershell - 截斷字符串中的字符
- 12. UILabel文本被截斷
- 13. 多行UILabel正在截斷
- 14. UILabel在滾動後截斷
- 15. 動態UILabel截斷文本
- 16. UILabel視圖中的滾動被截斷
- 17. 截斷字符串
- 18. 截斷字符串
- 19. 如何截斷perl中特定字符的字符串?
- 20. 如何更改UIlabel的字體?
- 21. 如何更改uilabel的字體?
- 22. 如何檢測字符串截斷
- 23. 如何用特殊字符截斷HTML?
- 24. 的WriteFile()截斷字符串
- 25. 字符串截斷的ClientDataSet
- 26. Rails的截斷字符串
- 27. 如何在Java中的5分隔符後截斷字符串?
- 28. UILabel lineBreakMode,在特定字符中斷
- 29. 如何判斷何時UILabel將被截斷和/或其換行符位置將改變
- 30. UILabel用左邊的省略號截斷
FYI三個小點被稱爲`ellipsis` – Jacksonkr 2012-04-18 12:36:31