zoukankan      html  css  js  c++  java
  • ios开发 之 设置多种文字颜色/背景色/文字下划线/行间距 NSString

    • NSMutableAttributedString常见的属性:

    • NSFontAttributeName 字体

    • NSForegroundColorAttributeName 文字颜色

    • NSBackgroundColorAttributeName 背景颜色

    • NSStrikethroughStyleAttributeName 删除线(默认是0,无删除线)

    • NSUnderlineStyleAttributeName 下划线(默认是0,无下划线)

    • NSParagraphStyleAttributeName 设置段落/间距

    • 使用方法:

    • 为某一范围内文字设置多个属性

    • - (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;

    • 为某一范围内文字添加某个属性

    • - (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;

    • 为某一范围内文字添加多个属性

    • - (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;

    •     UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 100)];
          label.numberOfLines = 0;
          label.font = [UIFont systemFontOfSize:18];
               
          NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:@"你说你最爱丁香花,因为你的名字就是它,多么忧郁的花,多愁善感的人啊!"];
           
          //设置文字颜色以及字体、删除线
          NSDictionary * dict = @{
                                  NSForegroundColorAttributeName:[UIColor redColor],
                                  NSFontAttributeName:[UIFont systemFontOfSize:13],
                                  NSStrikethroughStyleAttributeName:@"1"};
           
          //从下标0开始,长度为18的内容设置多个属性,dict里面写的就是设置的属性
          [str setAttributes:dict range:NSMakeRange(0, 18)];
           
          //设置背景颜色以及下划线
          NSDictionary * dict1 = @{
                                   NSBackgroundColorAttributeName:[UIColor yellowColor],
                                   NSUnderlineStyleAttributeName:@"1"};
           
          //从下标14开始,长度为6的内容添加多个属性,dict1里面写的就是添加的属性
          [str addAttributes:dict1 range:NSMakeRange(14, 6)];
           
          //从下标21开始,长度为2的内容添加字体属性,设置其字号为22
          [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22] range:NSMakeRange(21, 2)];
           
          label.attributedText = str;
          [self.view addSubview:label];
          UITextView *titleText = [[UITextView alloc] initWithFrame:CGRectMake(10, 200, 300, 300)];
          titleText.text = @"说不上为什么,我变得很主动。若爱上一个人,什么都会值得去做。我想大声宣布,对你依依不舍。连隔壁邻居都猜到我现在的感受,河边的风在吹着头发飘动,牵着你的手一阵莫名感动。我想带你回我的外婆家,一起看着日落,一直到我们都睡着。我想就这样牵着你的手不放开,爱能不能够永远单纯没有悲哀,我想带你骑单车,我想和你看棒球,想这样没担忧唱着歌一直走!";
          titleText.font = [UIFont systemFontOfSize:12];
           
          NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
          paragraphStyle.lineSpacing = 10;//行间距
           
          //设置字号和行间距
          NSDictionary *ats = @{
                                NSFontAttributeName : [UIFont systemFontOfSize:16.0f],
                                NSParagraphStyleAttributeName : paragraphStyle,
                                };
           
          titleText.attributedText = [[NSAttributedString alloc] initWithString:titleText.text attributes:ats];//设置行间距
           
          [self.view addSubview:titleText];

      如果是给Label设置的行间距,设置完以后,获取label的高度方法:

    •  //获取设置文本间距以后的高度,210是控件的宽度,需要写一致,不然获取的高度有问题
         CGRect fram = [dataLabel.attributedText boundingRectWithSize:CGSizeMake(210, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
         NSLog(@"-----高度是%f",fram.size.height);

      原博客地址:http://my.oschina.net/linxiaoxi1993/blog/469561

  • 相关阅读:
    MVC模式-----struts2框架(2)
    MVC模式-----struts2框架
    html的<h>标签
    jsp脚本元素
    LeetCode "Paint House"
    LeetCode "Longest Substring with At Most Two Distinct Characters"
    LeetCode "Graph Valid Tree"
    LeetCode "Shortest Word Distance"
    LeetCode "Verify Preorder Sequence in Binary Search Tree"
    LeetCode "Binary Tree Upside Down"
  • 原文地址:https://www.cnblogs.com/csdnIOS/p/5196242.html
Copyright © 2011-2022 走看看