这个处理不局限一种控件,如果是单行的话,为了显示全乎,使用sizeToFit也行,如果是为了统一,就多余文字变成...就需要使用:
self.nameBtn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
点进去lineBreakModel可以的看到:
@property(nonatomic) NSLineBreakMode lineBreakMode; // default is NSLineBreakByTruncatingTail. used for single and multiple lines of text
他有几种模式:
// 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);
可以看到有A...B、AB...、...AB等多种。
可以自己选择需要的。
如果包含图片,可以自己判断一下文字所需宽度,之后在控制偏移
比如:
[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)];
或者 ,你也可以用uitextview来当做显示文字的控件
-(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;
}
这里需要注意的是如果你关闭了用户交互,那么当文字过多的时候,就不可以滑动了,编辑,选中关了要不就可以选中了,但是貌似showsVerticalScrollIndicator关不掉。。。emmmm
转载至链接:https://my.oschina.net/rainwz/blog/1863614