Display of control text too long

This processing is not limited to one kind of control. If it is a single line, in order to display everything, sizeToFit is also used. If it is for unification, redundant text will become... You need to use:

self.nameBtn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;

Click lineBreakModel to see:

@property(nonatomic)        NSLineBreakMode    lineBreakMode;   // default is NSLineBreakByTruncatingTail. used for single and multiple lines of text

He has several models:

// NSParagraphStyle
typedef NS_ENUM(NSInteger, NSLineBreakMode) {
    NSLineBreakByWordWrapping = 0,     	// Wrap at word boundaries, default
    NSLineBreakByCharWrapping,		// Wrap at character boundaries
    NSLineBreakByClipping,		// Simply clip
    NSLineBreakByTruncatingHead,	// Truncate at head of line: "...wxyz"
    NSLineBreakByTruncatingTail,	// Truncate at tail of line: "abcd..."
    NSLineBreakByTruncatingMiddle	// Truncate middle of line:  "ab...yz"
} NS_ENUM_AVAILABLE(10_0, 6_0);

It can be seen that there are A...B, AB,..., AB and so on.

You can choose what you need.

If the image is included, you can judge the required width of the text by yourself, and then control the offset

For example:

 [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
    CGFloat width = kMainScreenWidth - 60;
    width = (width > [button.titleLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:kMainBoldFontT36} context:nil].size.width)?[button.titleLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:kMainBoldFontT36} context:nil].size.width:width;
    [button setImageEdgeInsets:UIEdgeInsetsMake(0,  width+ 15, 0, -width - 15)];

Or, you can use uitextview as a control to display text

-(UITextView *)infoTextView{
    if (!_infoTextView) {
        _infoTextView = [[UITextView alloc]init];
        _infoTextView.text = self.info;
        _infoTextView.textAlignment = NSTextAlignmentLeft;
        _infoTextView.textColor = kMainColor000000;
        _infoTextView.font = kMainFontT28;
        _infoTextView.backgroundColor = [UIColor clearColor];
        _infoTextView.userInteractionEnabled = YES;
        _infoTextView.editable = NO;
        _infoTextView.selectable = NO;
        _infoTextView.showsVerticalScrollIndicator = NO;
        _infoTextView.contentInset = UIEdgeInsetsMake(-8, -4, 0, -8);
        CGSize size = CGSizeMake(10.0f, 10.0f);
        [_infoTextView setContentSize:size];
        [self.bgView addSubview:_infoTextView];
    return _infoTextView;

It should be noted that if you turn off user interaction, when there are too many words, you can't slide, edit, select or select. But it seems that the showsVerticalScrollIndicator can't turn off... emmmm

Posted on Fri, 31 Jan 2020 03:35:33 -0800 by beanman1