简体   繁体   中英

How to make a UILabel truncate at character boundaries?

I have some UILabels next to each other horizontally which will contain names.

I can't find any way of getting the text to be truncated at a character boundary - I've tried a line break mode with NSLineBreakByCharWrapping but the characters get chopped off in the middle

在此处输入图片说明

I've tried the other line break modes but can't get rid of the partial character (x in this particular example), I don't want part of a character displayed obviously as it looks no good.

Its not feasible to limit the output to a hard coded number of characters because iiiiiiii is a totally different width to wwwwwwww for example. Also I don't want … appearing within the text because as its narrow then there would be too few characters left if part of the available space is consumed with ...

    contactItem.name.frame = nameFrame;
    contactItem.name.font = [UIFont systemFontOfSize:12];
    contactItem.name.textColor = [UIColor whiteColor];
    contactItem.name.textAlignment = NSTextAlignmentCenter;
    contactItem.name.lineBreakMode = NSLineBreakByCharWrapping;
    [self.scrollView addSubview: contactItem.name];

contactItem.name is the UILabel(s). nameFrame is getting horizontally incremented for each contactItem.

Maybe calculate text size yourself and react when it's too long?

Calculating UILabel Text Size

I don't know if this the simplest solution for this problem - but for sure it's the working solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM