UILabel 中Line Breaks属性.

Clip

 

 

Character Wrap

Word Wrap

 

Truncate Head

Truncate Middle

Truncate Tail

 

 

#import "LabelTestViewController.h"     
@implementation LabelTestViewController     
/*   
 Accessing the Text Attributes   
 text  property     
 font  property     
 textColor  property     
 textAlignment  property     
 lineBreakMode  property       
 enabled  property     
 Sizing the Label’s Text   
 adjustsFontSizeToFitWidth  property     
 baselineAdjustment  property     
 minimumFontSize  property   无例   
 numberOfLines  property     
 Managing Highlight Values   
 highlightedTextColor  property     
 highlighted  property     
 Drawing a Shadow   
 shadowColor  property     
 shadowOffset  property     
 Drawing and Positioning Overrides   
 – textRectForBounds:limitedToNumberOfLines: 无例    
 – drawTextInRect:  无例   
 Setting and Getting Attributes   
 userInteractionEnabled  property     
 */    
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.     
- (void)viewDidLoad {     
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 20.0, 200.0, 50.0)];     
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 80.0, 200.0, 50.0)];     
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 140.0, 200.0, 50.0)];     
    UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 200.0, 200.0, 50.0)];     
    UILabel *label5 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 260.0, 200.0, 50.0)];     
    UILabel *label6 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 320.0, 200.0, 50.0)];     
    UILabel *label7 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 380.0, 200.0, 50.0)];     
    //设置显示文字     
    label1.text = @"label1";     
    label2.text = @"label2";     
    label3.text = @"label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--";     
    label4.text = @"label4--label4--label4--label4--";     
    label5.text = @"label5--label5--label5--label5--label5--label5--";     
    label6.text = @"label6";     
    label7.text = @"label7";     
    //设置字体:粗体,正常的是 SystemFontOfSize     
    label1.font = [UIFont boldSystemFontOfSize:20];     
    //设置文字颜色  
    label1.textColor = [UIColor orangeColor];     
    label2.textColor = [UIColor purpleColor];     
    //设置文字位置     
    label1.textAlignment = UITextAlignmentRight;     
    label2.textAlignment = UITextAlignmentCenter;     
    //设置字体大小适应label宽度     
    label4.adjustsFontSizeToFitWidth = YES;     
  
    //设置label的行数     
    label5.numberOfLines = 2;    
    UIlabel.backgroudColor=[UIColor clearColor]; //可以去掉背景色   
 
    //设置高亮     
    label6.highlighted = YES;     
    label6.highlightedTextColor = [UIColor orangeColor];     
    //设置阴影     
    label7.shadowColor = [UIColor redColor];     
    label7.shadowOffset = CGSizeMake(1.0,1.0);     
    //设置是否能与用户进行交互     
    label7.userInteractionEnabled = YES;     
    //设置label中的文字是否可变,默认值是YES     
    label3.enabled = NO;     
    //设置文字过长时的显示格式     
    label3.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中间     
//  typedef enum {     
//      UILineBreakModeWordWrap = 0,     
//      UILineBreakModeCharacterWrap,     
//      UILineBreakModeClip,//截去多余部分     
//      UILineBreakModeHeadTruncation,//截去头部     
//      UILineBreakModeTailTruncation,//截去尾部     
//      UILineBreakModeMiddleTruncation,//截去中间     
//  } UILineBreakMode;     
    //如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为     
    label4.baselineAdjustment = UIBaselineAdjustmentNone;     
//  typedef enum {     
//      UIBaselineAdjustmentAlignBaselines,     
//      UIBaselineAdjustmentAlignCenters,     
//      UIBaselineAdjustmentNone,     
//  } UIBaselineAdjustment;     
    [self.view addSubview:label1];     
    [self.view addSubview:label2];     
    [self.view addSubview:label3];     
    [self.view addSubview:label4];     
    [self.view addSubview:label5];     
    [self.view addSubview:label6];     
    [self.view addSubview:label7];     
    [label1 release];     
    [label2 release];     
    [label3 release];     
    [label4 release];     
    [label5 release];     
    [label6 release];     
    [label7 release];     
    [super viewDidLoad];     
}     
/*   
 // Override to allow orientations other than the default portrait orientation.   
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {   
 // Return YES for supported orientations   
 return (interfaceOrientation == UIInterfaceOrientationPortrait);   
 }   
 */    
- (void)didReceiveMemoryWarning {     
    // Releases the view if it doesn't have a superview.     
    [super didReceiveMemoryWarning];     
    // Release any cached data, images, etc that aren't in use.     
}     
- (void)viewDidUnload {     
    // Release any retained subviews of the main view.     
    // e.g. self.myOutlet = nil;     
}     
- (void)dealloc {     
    [super dealloc];     
}     
@end  
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以按照以下代码来进行布局: ```swift import SnapKit class YourTableViewCell: UITableViewCell { private let label = UILabel() private let imageView = UIImageView() override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) // Add subviews to contentView contentView.addSubview(label) contentView.addSubview(imageView) // Set label properties label.numberOfLines = 0 // Set constraints for label label.snp.makeConstraints { make in make.top.equalToSuperview().offset(5) make.left.right.equalToSuperview() } // Set constraints for imageView imageView.snp.makeConstraints { make in make.top.equalTo(label.snp.bottom).offset(10) make.left.right.equalToSuperview() make.bottom.equalToSuperview().offset(-10) make.height.equalTo(0) } } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func configure(with text: String, image: UIImage?) { label.text = text // Update constraints for imageView based on the presence of an image if let image = image { imageView.image = image imageView.snp.updateConstraints { make in make.height.equalTo(50) make.top.equalTo(label.snp.bottom).offset(10) make.bottom.equalToSuperview().offset(-10) } } else { imageView.image = nil imageView.snp.updateConstraints { make in make.height.equalTo(0) make.top.equalTo(label.snp.bottom).offset(0) make.bottom.equalToSuperview().offset(-10) } } } } ``` 在上述代码,我们使用了 `snp` 的链式语法来设置布局。我们首先初始化了一个 `UILabel` 和一个 `UIImageView` 并将它们添加到 `contentView` 上。然后,我们设置了 `UILabel` 的一些属性,如 `numberOfLines` 来使其多行显示。接着,我们设置了 `UILabel` 和 `UIImageView` 的约束。在 `configure(with:image:)` 方法,我们根据传递进来的 `image` 参数来更新 `UIImageView` 的约束。如果 `image` 不为 `nil`,则我们将 `UIImageView` 的高度设置为 50,并将其放置在 `UILabel` 的下方。如果 `image` 为 `nil`,则我们将 `UIImageView` 的高度设置为 0,使其不可见,并将其放置在 `UILabel` 的正下方。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值