首页 > 其他分享 >iOS 在UILabel显示不同的字体和颜色(转)

iOS 在UILabel显示不同的字体和颜色(转)

时间:2023-08-23 12:34:14浏览次数:37  
标签:NSMakeRange iOS value UILabel range 字体 str addAttribute

在项目开发中,我们经常会遇到在这样一种情形:在一个UILabel 使用不同的颜色或不同的字体来体现字符串,在iOS 6 以后我们可以很轻松的实现这一点,官方的API 为我们提供了UILabel类的attributedText, 使用不同颜色和不同字体的字符串,我们可以使用NSAttributedText 和 NSMutableAttributedText 类来实现。

现实代码:

@interface ViewController : UIViewController
@property (nonatomic, strong) IBOutlet UILabel *attrLabel;
- (IBAction)next:(id)sender;
@end

 

 .m文件 在viewDidLoad方法中添加以下代码:

self.title = @"For iOS 6 & later";
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 5)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(6, 12)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];
attrLabel.attributedText = str;

效果图:

iOS 在UILabel显示不同的字体和颜色(转)_字符串

如果想在iOS6.0以前版本实现这个效果,需要使用到一个第三方库TTTAttributedLabel,同时还有导入CoreText.frame框架.



作者:稻草人11223


标签:NSMakeRange,iOS,value,UILabel,range,字体,str,addAttribute
From: https://blog.51cto.com/u_13188203/7201349

相关文章

  • ios8 UITableView设置 setSeparatorInset:UIEdgeInsetsZero不起作用的解决办法
    在ios7中,UITableViewCell左侧会有默认15像素的空白。这时候,设置setSeparatorInset:UIEdgeInsetsZero能将空白去掉。但是在ios8中,设置setSeparatorInset:UIEdgeInsetsZero已经不起作用了。下面是解决办法首先在viewDidLoad方法加入以下代码: if([self.tableViewrespondsToSelect......
  • ios中利用NSDateComponents、NSDate、NSCalendar判断当前时间是否在一天的某个时间段
    应用中设置一般会存在这样的设置,如夜间勿扰模式,从8:00-23:00,此时如何判断当前时间是否在该时间段内。难点主要在于如何用NSDate生成一个8:00的时间和23:00的时间,然后用当前的时间跟这俩时间作对比就好了。下面提供两条思路:法1.用NSDate生成当前时间,然后转为字符串,从字符串中取出当前的......
  • iOS开发之--获取验证码倒计时及闪烁问题解决方案
    大家在做验证码的时候一般都会用到倒计时,基本上大家实现的方式都差不多,先贴出一些代码来..-(void)startTime{__blockinttimeout=59;//倒计时时间dispatch_queue_tqueue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);dispatch_source......
  • ios开发之--NSNumber的使用
    什么是NSNumber?NSArray/NSDictionary中只能存放oc对象,不能存放基本数据类型,如果想把基本数据类型放进去,需要先把基本数据类型转换成OC对象,代码如下:inta=10;intx=100;floatf1=3.23f;doubled1=2.34;//int--->obj//numberWithInt把in......
  • ios开发之--UISearchDisplayController的简单使用
    控件就不介绍了,UISearchDisplayController就是把searbar和tableview结合到一块了,直接上代码:.h#import<UIKit/UIKit.h>@interfaceThirdViewController:UIViewController<UITableViewDelegate,UITableViewDataSource>{NSArray*_array;NSArray*_filterData;UI......
  • ios开发之--pop到指定页面
    1推出到根视图控制器[self.navigationControllerpopToRootViewControllerAnimated:YES];2推出到指定的视图控制器 for(UIViewController*controllerinself.navigationController.viewControllers){if([controllerisKindOfClass:[AViewControllerclass]]){......
  • ios开发之--ios11适配:TableView的heightForHeaderInSection设置高度无效/UISearchBar
    更新到ios11,然后使用x-code9运行项目,发现tableview的-(CGFloat)tableView:(UITableView*)tableViewheightForHeaderInSection:(NSInteger)section方法不走,所以页面也华丽丽的变成了一排的cell,通过查看文档和资料,原来是ios11默认开启self-sizing,把这个属性关系即可,具体代码如下:sel......
  • ios开发之--NSString和NSArray互转
    将string字符串转换为array数组NSArray*array=[StrcomponentsSeparatedByString:@","];//分隔符逗号将array数组转换为string字符串 NSString*tempString=[mutableArraycomponentsJoinedByString:@","];//分隔符逗号作者:稻草人11223......
  • ios开发之--使用toolbar调整item之间的间隔
    toolbar的item有很多种样式,其实经常使用的就几种,UIBarButtonSystemItemFixedSpace木棍:可以理解为固定的长度UIBarButtonSystemItemFlexibleSpace 弹簧:可以理解为弹性的长度 UIBarButtonItem有两个barbutton的格式  添加到toolbaritems=@[木棍,按钮,弹簧,按钮,木棍];木棍是固定长度......
  • ios开发之--ZHPickView输出格式不出现 +0000
    这样写就不会输出+0000了NSDate*select=[_datePickerdate];NSDateFormatter*dateFormatter=[[NSDateFormatteralloc]init];[dateFormattersetDateFormat:@"yyyy-MM-ddHH:mm:ss"];_resultString=[dateFormatterstringFromDate:select];输出+0000的原因是_......