How to highlight only text in UILabel - IOS

后端 未结 4 1651
轻奢々
轻奢々 2020-12-14 22:57

I just want to highlight only text in UILabel, I have tried by giving backgroundColor for label, but it is highlighting the empty spaces also looks

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 23:36

    You can have a UIImageView, set its background color of your choice then place your UIlabel on it. That will surely solve your problem. Here is Workaround code :

     UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 100, 40)];
     [imgView setBackgroundColor:[UIColor brownColor]];
     [self.view addSubview:imgView];
    
    
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setTextAlignment:UITextAlignmentCenter];
    label.text = @"My Label";
    [imgView addSubview:label];
    
    
    [label release];
    [imgView release];
    

    You can also achieve the same using Interface Builder.

提交回复
热议问题