5

我想在 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 个)

任何帮助表示赞赏

4

6 回答 6

12

如果字符串长度超过 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;
于 2013-03-15T09:47:02.373 回答
4

要仅显示 500 个字符,只需使用以下代码:

NSString *string = YOUR_TEXT;
if ([string length] >500) {
    string = [string substringToIndex:500];
}

希望这会帮助你。

祝一切顺利 !!!

于 2013-03-15T09:34:09.673 回答
2

以下是在 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
}
于 2016-04-07T17:52:57.163 回答
1

试试这个,它会帮助你。

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]
于 2013-03-15T09:38:37.647 回答
0

它可以简单地通过

NSString *LabelNewText = [YourLabelName.text substringToIndex:500];

此处将显示 500 或少于 500 个字符,所有超过该字符的字符将被截断。

NSString *string = LabelNewText.text;

if ([string length] >500)
{
   string = [string substringToIndex:500];                                       
}
于 2013-03-15T09:35:23.650 回答
0

斯威夫特 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("...")
        }
于 2017-01-05T10:58:45.883 回答