本文介绍了在特定位置截断UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用表格视图来显示书籍列表,其中每个单元格都有 UILabel ,其中显示了该书的名称,另一个 UILabel 显示图书的作者

I use a table view to show a list of books, where each cell has a UILabel that shows the book's name and another UILabel the shows the book's author(s)

我的问题是关于作者标签。一本书可以有多位作者,我希望它的行为如下:

My question is about the author(s) label. A book can have multiple authors, and I want it to behave as follows:


  • 如果书中有一位作者('John Colman) ')标签应该是:John Colman

  • 如果书中有多个作者('John Colman','Bob Night','Michael')标签应为:John Colman +2作者

现在问题是这样,我希望标签在'+'之前被截断。例如,如果第一个作者姓名很长,让我们说'Benjamin Walter Jackson',我希望标签看起来像这样:

Now the problem is this, I want the label to be truncated before the '+'. So for example, if the first author name is long, lets say 'Benjamin Walter Jackson', I want the label to look like this:

"Benjamin Walter Ja... +2 authors"

当然默认行为会截断标签最后,所以它看起来像这样:

The default behaviour of course truncates the label in the end, so it looks like this:

"Benjamin Walter Jackson +2 au..."

如果我使用中间截断,则无法保证它会在正确的位置截断标签(在'+之前) ')

If I use the middle truncate, there's no promise that it will truncate the label in the right place (before the '+')

我正在寻找一种尽可能高效的方法,而不会影响表格视图的滚动性能。

I'm looking for a way to do it and as efficient as possible, without impacting the scroll performance of the table view.

推荐答案

编辑:广泛使用任何截断位置字符串的解决方案。以前的版本仅在字符串 @+的实例中被截断。编辑允许您定义截断发生的位置。

Generalized the solution to work with any "truncation location" string. Previous version only truncated at instance of string @" +". Edit allows you to define where you want the truncation to happen.

我从(这是从)和量身定制,以满足您的需求。创建一个新的 NSString 接口,您可以在其中发送字符串以进行自定义截断。

I took my answer from this question (which was an answer modified from the answer on this site) and tailored it to fit your needs. Create a new NSString interface where you can send your string to be custom-truncated.

注意:此解决方案是仅适用于iOS 7+。要在iOS 6中使用,请在 NSString + TruncateToWidth.m sizeWithFont:而不是 sizeWithAttributes: / strong> file。

NOTE: This solution is for iOS 7+ only. To use in iOS 6, use sizeWithFont: instead of sizeWithAttributes: in the NSString+TruncateToWidth.m file.

NSString + TruncateToWidth.h

@interface NSString (TruncateToWidth)
- (NSString*)stringByTruncatingAtString:(NSString *)string toWidth:(CGFloat)width withFont:(UIFont *)font;
@end

NSString + TruncateToWidth.m

#import "NSString+TruncateToWidth.h"

#define ellipsis @"…"

@implementation NSString (TruncateToWidth)

- (NSString*)stringByTruncatingAtString:(NSString *)string toWidth:(CGFloat)width withFont:(UIFont *)font
{
    // If the string is already short enough, or
    // if the 'truncation location' string doesn't exist
    // go ahead and pass the string back unmodified.
    if ([self sizeWithAttributes:@{NSFontAttributeName:font}].width < width ||
        [self rangeOfString:string].location == NSNotFound)
        return self;

    // Create copy that will be the returned result
    NSMutableString *truncatedString = [self mutableCopy];

    // Accommodate for ellipsis we'll tack on the beginning
    width -= [ellipsis sizeWithAttributes:@{NSFontAttributeName:font}].width;

    // Get range of the passed string. Note that this only works to the first instance found,
    // so if there are multiple, you need to modify your solution
    NSRange range = [truncatedString rangeOfString:string];
    range.length = 1;

    while([truncatedString sizeWithAttributes:@{NSFontAttributeName:font}].width > width
           && range.location > 0)
    {
        range.location -= 1;
        [truncatedString deleteCharactersInRange:range];
    }

    // Append ellipsis
    range.length = 0;
    [truncatedString replaceCharactersInRange:range withString:ellipsis];

    return truncatedString;
}

@end

使用它:

// Make sure to import the header file where you want to use it
myLabel.text = [@"Benjamin Walker Jackson + 2 authors" stringByTruncatingAtString:@" +" toWidth:myLabel.frame.size.width withFont:myLabel.font];
// Sample Result: Benjamin Walte... + 2 authors

这篇关于在特定位置截断UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:14