美文网首页
ios开发 之 设置多种文字颜色/背景色/文字下划线/行间距

ios开发 之 设置多种文字颜色/背景色/文字下划线/行间距

作者: 清风沐沐 | 来源:发表于2016-10-27 17:45 被阅读432次

    摘要: 一段文字中,有多种不同的文字颜色和背景色,可以用NSMutableAttributedString富文本来设置

    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;

    ** 练练swift3.0 这里给一段swift代码**

        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            
            let label = UILabel(frame: CGRect(x: 30, y: (UIScreen.main.bounds.size.height - 100) / 2 - 100, width: UIScreen.main.bounds.size.width - 60, height: 100))
            label.backgroundColor = UIColor.blue
            label.numberOfLines = 0
            label.font = UIFont.systemFont(ofSize: 20)
            view.addSubview(label)
            
            let mutableAttributedString = NSMutableAttributedString(string: "人生若只如初见,何事西风悲画扇,等闲变却故人心,却道故人心易变")
    
    
            var dict = [NSForegroundColorAttributeName:UIColor.red,
                         NSFontAttributeName:UIFont.systemFont(ofSize: 13),
                         NSStrikethroughStyleAttributeName:"1"] as [String : Any]
    
            mutableAttributedString.setAttributes(dict, range: NSRange(location: 0, length: 16))
    
            dict = [NSBackgroundColorAttributeName:UIColor.green,
                    NSUnderlineStyleAttributeName:1] as [String : Any]
    
            mutableAttributedString.setAttributes(dict, range: NSRange(location: 16, length: 8))
    
            mutableAttributedString.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 30), range: NSRange(location: 24, length: 7))
    
            label.attributedText = mutableAttributedString
            
            
            let textView = UITextView(frame: CGRect(x: 30, y: (UIScreen.main.bounds.size.height - 100) / 2 + 100, width: UIScreen.main.bounds.size.width - 60, height: 100))
            textView.text = "最喜那一低头的温柔,恰似水莲花瓣的娇羞  --- 最喜那一低头的温柔,恰似水莲花瓣的娇羞   ---  最喜那一低头的温柔,恰似水莲花瓣的娇羞  ---  最喜那一低头的温柔,恰似水莲花瓣的娇羞   ---   最喜那一低头的温柔,恰似水莲花瓣的娇羞"
            textView.font = UIFont.systemFont(ofSize: 12)
            view.addSubview(textView)
            
            let mutableParagraphStyle = NSMutableParagraphStyle()
            mutableParagraphStyle.lineSpacing = 20
            
            dict = [NSFontAttributeName:UIFont.systemFont(ofSize: 16),
                    NSParagraphStyleAttributeName: mutableParagraphStyle
                     ]
            
            textView.attributedText = NSAttributedString(string: textView.text, attributes: dict)
            
        }
    

    相关文章

      网友评论

          本文标题:ios开发 之 设置多种文字颜色/背景色/文字下划线/行间距

          本文链接:https://www.haomeiwen.com/subject/jphsuttx.html