[英]limit the UILabel to 500 character and than Truncate the UIlabel in IOS
我想在UILabel中顯示前500個字符,並且如果有500個以上的字符,則要顯示“截斷”圖標。但是我不知道如何限制500個字符來截斷文本?
這是我的代碼
label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 350, self.bounds.size.width, 30)];
// In this case value of self.bounds.size.width is "427"
label2.backgroundColor = [UIColor clearColor];
label2.numberOfLines = 2;
label2.textAlignment = UITextAlignmentCenter;
label2.font = [UIFont systemFontOfSize:13];
[self addSubview:label2]
//Here is Implimentation code of my label
NSString *temp = [galleryEntryTree objectForKey:@"description"];// calling lebel text from database
coverView.label2.text = temp;
coverView.label2.adjustsFontSizeToFitWidth = NO;
coverView.label2.lineBreakMode = UILineBreakModeTailTruncation;
告訴我,我如何顯示至少500個字符,然后截斷它(如果超過500個)
任何幫助表示贊賞
如果字符串超過500個字符,則只需截斷它。 僅警告:請確保不要在代理對中間中斷它:
NSString *temp = [galleryEntryTree objectForKey:@"description"];
if ([temp length] > 500) {
NSRange range = [temp rangeOfComposedCharacterSequencesForRange:(NSRange){0, 500}];
temp = [temp substringWithRange:range];
temp = [temp stringByAppendingString:@" …"];
}
coverView.label2.text = temp;
要僅顯示500個字符,請使用以下代碼:
NSString *string = YOUR_TEXT;
if ([string length] >500) {
string = [string substringToIndex:500];
}
希望這會幫助你。
祝一切順利 !!!
這是在Swift 2.2中的操作方法:
let maxLength = 500
if originalString.characters.count > maxLength {
let range = originalString.rangeOfComposedCharacterSequencesForRange(Range<String.Index>(originalString.startIndex ..< originalString.startIndex.advancedBy(maxLength)))
let tmpValue = originalString.substringWithRange(range).stringByAppendingString(" …")
// use tmpValue
}
試試這個,對您有幫助。
label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 350, self.bounds.size.width, 30)];
// In this case value of self.bounds.size.width is "427"
label2.text=@"your text................................";
if([label2.text length]>500)
label2.text=[label2.text substringToIndex:500];
label2.backgroundColor = [UIColor clearColor];
label2.numberOfLines = 2;
label2.textAlignment = UITextAlignmentCenter;
label2.font = [UIFont systemFontOfSize:13];
[self addSubview:label2]
可以簡單地通過
NSString *LabelNewText = [YourLabelName.text substringToIndex:500];
此處將顯示500個或少於500個字符,超過此限制的所有字符將被截斷。
NSString * string = LabelNewText.text;
if ([string length] >500)
{
string = [string substringToIndex:500];
}
Swift 3.0版本
let maxLength = 300 //char length
if originalString.characters.count > maxLength {
let range = originalString.rangeOfComposedCharacterSequences(for: originalString.startIndex..<originalString.index(originalString.startIndex, offsetBy: maxLength))
let tmpValue = originalString.substring(with: range).appending("...")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.