2025年Label的用法

Label的用法发现 UILabel 好像没有点击事件之类的 今天算是搜到了一点点我想要的 还有要设置那个下划线之类的 还没有去查找呢 慢慢来吧 import ViewControll h interface ViewControll property weak nonatomic

大家好,我是讯享网,很高兴认识大家。

发现UILabel好像没有点击事件之类的,今天算是搜到了一点点我想要的,还有要设置那个下划线之类的,还没有去查找呢,慢慢来吧

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *btn;
@property (weak, nonatomic) IBOutlet UILabel *tmpLabel;

@end

@implementation ViewController
// MARK: jijijd
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //初始化一个label
    //    CGRect label1Rect = CGRectMake(10, 10, 100, 100);
    //    UILabel *label1 = [[UILabel alloc] initWithFrame:label1Rect];
    //    label1.text = @"偶是label1";
    //    label1.font = [UIFont boldSystemFontOfSize:20];//设置字体大小
    
    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 =  NSTextAlignmentRight;
    label2.textAlignment =  NSTextAlignmentCenter;
    
    //设置字体大小适应label宽度 字体会变小 或者变大吧
    label4.adjustsFontSizeToFitWidth = YES;
    
    //设置label的行数
    label5.numberOfLines = 2;
    label5.backgroundColor = [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 = NSLineBreakByTruncatingMiddle;//截去中间
    //  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];//添加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];
    
    NSString * htmlString = @"<html><body> Some html string \n <font size=\"13\" color=\"red\">This is some text!</font> </body></html>";
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    UILabel * myLabel = [[UILabel alloc] initWithFrame:self.view.bounds];
    myLabel.attributedText = attrStr;
    [self.view addSubview:myLabel];
    
    
    //label点击有交互事件产生
    UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 480.0, 200.0, 150.0)];
    l.text = @"点击点击点击 ";
    l.userInteractionEnabled=YES;
    UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTouchUpInside:)];
    
    [l addGestureRecognizer:labelTapGestureRecognizer];
    [self.view addSubview:l];
    
//    label1.textColor  = UICol
}
//TODO:做todo事件
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) labelTouchUpInside:(UITapGestureRecognizer *)recognizer{
    
    UILabel *label=(UILabel*)recognizer.view;
    
    NSLog(@"%@被点击了",label.text);
    
    
    
}
//FIXME:吧啦吧啦
- (void) doit{
    ;
}
@end

讯享网
讯享网#pragma mark - 加载视图 - (void)viewDidLoad { [super viewDidLoad]; CGRect frame = CGRectMake(20 ,20 , 300, 300); self.myLabel = [[UILabel alloc]initWithFrame:frame]; self.myLabel.text = @"呵呵哒"; //设置字体 self.myLabel.font = [UIFont boldSystemFontOfSize:20]; //设置字体颜色 self.myLabel.textColor = [UIColor orangeColor]; //设置对齐方式 self.myLabel.textAlignment = NSTextAlignmentCenter; //设置字体大小适应Label宽度 self.myLabel.adjustsFontSizeToFitWidth = YES; //设置行数 self.myLabel.numberOfLines = 2; //设置阴影 self.myLabel.shadowColor = [UIColor redColor]; self.myLabel.shadowOffset = CGSizeMake(2.0, 2.0); //设置缩略显示方式 self.myLabel.lineBreakMode = NSLineBreakByTruncatingTail; [self.view addSubview: self.myLabel]; }

让Label自适应,即为根据文字的高度来显示特定的高度


讯享网

#pragma mark - 加载视图 - (void)viewDidLoad { [super viewDidLoad]; self.myLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 45, 0, 0)]; //背景色 self.myLabel.backgroundColor = [UIColor lightTextColor]; //行数 [self.myLabel setNumberOfLines:0]; //缩略方式 self.myLabel.lineBreakMode = NSLineBreakByTruncatingTail; //字体 UIFont *fonts = [UIFont fontWithName:@"Arial" size:12]; self.myLabel.font = fonts; self.myLabel.text = @"ni hao 我是我市我是卧室卧室卧室ni hao 我是我市我是卧室卧室卧室ni hao 我是我市我是卧室卧室卧室ni hao 我是我市我是卧室卧室卧室ni hao 我是我市我是卧室卧室卧室ni hao 我是我市我是卧室卧室卧室ni hao 我是我市我是卧室卧室卧室ni hao 我是我市我是卧室卧室卧室ni hao 我是我市我是卧室卧室卧室ni hao 我是我市我是卧室卧室卧室"; int width = self.view.frame.size.width; int paddingLeft = 15; int paddingTop = 45; NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:13]}; CGSize msgSize = [self.myLabel.text boundingRectWithSize:CGSizeMake(width - 2 * paddingLeft, 0) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size; [self.myLabel setFrame:CGRectMake(paddingLeft, paddingTop, width - paddingLeft, msgSize.height)]; [self.view addSubview:self.myLabel]; } 

设置下划线删除线

讯享网 //显示中划线 NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName : [NSNumber numberWithInteger : NSUnderlineStyleSingle]}; //显示下划线 NSDictionary *attribtUnderlineDic = @{NSUnderlineStyleAttributeName : [NSNumber numberWithInteger : NSUnderlineStyleSingle]}; NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:self.myLabel.text attributes:attribtUnderlineDic]; self.myLabel.attributedText = attribtStr; 





小讯
上一篇 2025-02-11 22:12
下一篇 2025-01-13 17:57

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/90248.html