设置UILabel上面的文字显示不同的字体和颜色

    /**************************************显示内容是英文***********************************************/

    

    // 创建显示英文的Label初始化并确定位置

    UILabel *labelofEN = [[UILabelalloc]initWithFrame:CGRectMake(0,200, [[UIScreenmainScreen]bounds].size.width,100)];

    // 构造字符串

    NSMutableAttributedString *str1 = [[NSMutableAttributedStringalloc]initWithString:@"Hello World China Is Strong"];

    // 为字符串中的字符添加不同的颜色

    [str1 addAttribute:NSForegroundColorAttributeNamevalue:[UIColorblueColor]range:NSMakeRange(0,11)];

    [str1 addAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor]range:NSMakeRange(12,15)];

    // 为字符串中的字符添加不同的字体

    [str1 addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"Arial-BoldItalicMT"size:30.0]range:NSMakeRange(0,11)];

    [str1 addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"HelveticaNeue-Bold"size:30.0]range:NSMakeRange(12,15)];

    // Label的显示内容

    labelofEN.attributedText = str1;

    // Label的内容居中显示

    labelofEN.textAlignment =NSTextAlignmentCenter;

    // 把Label展示到画布上

    [self.viewaddSubview:labelofEN];

  

    

  /**************************************显示内容是中文***********************************************/

    

    // 创建显示中文的Label初始化并确定位置

    UILabel *labelofCH = [[UILabelalloc]initWithFrame:CGRectMake(0,300, [[UIScreenmainScreen]bounds].size.width,100)];

    // 构造字符串

    NSMutableAttributedString *str2 = [[NSMutableAttributedStringalloc]initWithString:@"我爱中国"];

    // 为字符串中的字符添加不同的颜色

    [str2 addAttribute:NSForegroundColorAttributeNamevalue:[UIColorblueColor]range:NSMakeRange(0,1)];

    [str2 addAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor]range:NSMakeRange(1,1)];

    [str2 addAttribute:NSForegroundColorAttributeNamevalue:[UIColorpurpleColor]range:NSMakeRange(2,1)];

    [str2 addAttribute:NSForegroundColorAttributeNamevalue:[UIColororangeColor]range:NSMakeRange(3,1)];

    // 为字符串中的字符添加不同的字体

    [str2 addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"Arial-BoldItalicMT"size:30.0]range:NSMakeRange(0,1)];

    [str2 addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"HelveticaNeue-Bold" size:30.0]range:NSMakeRange(1,1)];

    [str2 addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"Courier-BoldOblique"size:30.0]range:NSMakeRange(2,1)];

    [str2 addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"American Typewriter"size:30.0]range:NSMakeRange(3,1)];

    // Label的显示内容

    labelofCH.attributedText = str2;

    // Label的内容居中显示

    labelofCH.textAlignment =NSTextAlignmentCenter;

    // 把Label展示到画布上

    [self.viewaddSubview:labelofCH];


效果图:



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值