swift

swift笔记(七)

2017-07-16  本文已影响10人  阿凡提说AI

1.图文混排

// 需求:点赞[点赞]请多多支持
        // 1.创建'点赞'属性字符串
        let attrStr1 = NSAttributedString(string: "点赞")
        
        // 2.创建显示[点赞]图片的属性字符串
        let attachment = NSTextAttachment()
        let fontHeight = self.demoLabel.font.lineHeight
        attachment.bounds = CGRect(x: 0, y: 0, width: fontHeight, height: fontHeight)
        attachment.image = UIImage(named: "lxh_haobang")
        let attrStr2 = NSAttributedString(attachment: attachment)
        
        // 3.创建'请多多支持'属性字符串
        let attrStr3 = NSAttributedString(string: "请多多支持")
        
        // 4.拼接三个属性字符串
        let attrMStr = NSMutableAttributedString()
        attrMStr.appendAttributedString(attrStr1)
        attrMStr.appendAttributedString(attrStr2)
        attrMStr.appendAttributedString(attrStr3)
        
        // 5.显示demoLabel
        self.demoLabel.attributedText = attrMStr




// 需求:小码哥:www.520it.com
    // 小码哥:显示红色
    // www.520it.com 显示蓝色
    // 1.设置demoLabel文字颜色为红色
    self.demoLabel.textColor = UIColor.redColor()
    
    // 2.创建属性字符串
    let attrString = NSAttributedString(string: "www.520it.com", attributes: [NSForegroundColorAttributeName : UIColor.blueColor(), NSFontAttributeName : UIFont.systemFontOfSize(12)])
    
    // 3.创建可变的属性字符串
    let attrMString = NSMutableAttributedString(string: "小码哥:", attributes: [NSFontAttributeName : UIFont.systemFontOfSize(30)])
    attrMString.appendAttributedString(attrString)
    
    // 4.显示字符串
    self.demoLabel.attributedText = attrMString

2.正则表达式

/*
    练习:
    练习1:匹配abc
    练习2:包含一个写a~z,后面必须是0~9 -->[a-z][0-9]或者[a-z]\d
    练习3:必须第一个是字母,第二个是数字 -->^[a-z][0-9]$
    练习4:必须第一个是字母,字母后面跟上4~9个数字
    练习5:不能是数字[^0-9]

    练习6:QQ匹配:^[1-9]\\d{4,11}$
    1.都是数字
    2.5~12位

    练习7:手机号码匹配^1[3578]\\d{9}$
    1.以13/15/17/18
    2.长度是11
 */

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let str = "17334332342"
        
        matchPhone(str)
    }
    
    func matchPhone(strPhoneNum : String) {
        // 联系二:匹配点好号码
        let pattern = "^1[3578][0-9]{9}$"
        
        // 查看是否匹配
        matchString(strPhoneNum, pattern: pattern)
    }
    
    func matchQQ(strQQ : String) {
        // 练习一:匹配QQ号码 d324234
        // 1.全部是数字
        // 2.5~14位置
        // 3.开头不能是0
        let pattern = "^[1-9]\\d{4,13}"
        
        // 4.通过规则创建正则表达式对象
        matchString(strQQ, pattern: pattern)
    }
    
    func matchString(str : String, pattern : String) {
        // 1.通过规则创建正则表达式对象
        let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive)
        
        // 2.匹配QQ号码
        let results = regex.matchesInString(str, options: NSMatchingOptions(rawValue: 0), range: NSRange(location: 0, length: str.characters.count))
        
        for result in results {
            print(result.range)
        }
    }
    
    func test(str : String) {
        // 1.定义规则
        // [a-z] : 匹配字母
        // [0-9]或\d : 匹配数字
        // {1,10} : 有1位到10
        // {0,} : 有0,或者无限多位
        // . : 任意的符号(除了换行符)
        // ^ : 1.写在^[]外面,必须以某规则开头 2.写在[^]规则的取反
        // $ : 必须以某规则结尾
        // *? : 表示有1位或者多少,但是一旦复合规则,就会直接返回
        let pattern = "^[^a-z].*?"
        
        // 2.通过规则,创建正则表达式对象
        let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive)
        
        // 3.利用对象匹配字符串
        /*
        // 匹配一个字符串,并且将匹配结果以数组的形式返回
        // 参数一:要匹配的字符串
        // 参数二:0
        // 参数三:要匹配该字符串中的哪一个返回
        public func matchesInString(string: String, options: NSMatchingOptions, range: NSRange) -> [NSTextCheckingResult]
        
        // 匹配一个字符串,将匹配结果的个数返回
        public func numberOfMatchesInString(string: String, options: NSMatchingOptions, range: NSRange) -> Int
        
        // 匹配一个字符串,将第一个结果返回
        public func firstMatchInString(string: String, options: NSMatchingOptions, range: NSRange) -> NSTextCheckingResult?
        
        // 匹配一个字符串,将第一个结果的range返回
        public func rangeOfFirstMatchInString(string: String, options: NSMatchingOptions, range: NSRange) -> NSRange
        */
        let results = regex.matchesInString(str, options: NSMatchingOptions(rawValue: 0), range: NSRange(location: 0, length: str.characters.count))
        
        for result in results {
            print(result.range)
        }
    }
}






        let str = "@jack12:【动物尖叫合辑】#肥猪流#猫头鹰这么尖叫[偷笑]、@南哥: 老鼠这么尖叫、兔子这么尖叫[吃惊]、@花满楼: 莫名奇#小笼包#妙的笑到最后[好爱哦]!~ http://t.cn/zYBuKZ8/"
        
        // 1.创建匹配规则
        // 1.1.表情的规则
        // let pattern = "\\[.*?\\]"
        
        // 1.2.匹配@名字:
        // let pattern = "@.*?:"
        
        // 1.3.匹配话题 #....#
        // let pattern = "#.*?#"
        
        // 1.4.匹配URL
        let pattern = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?"
        
        // 2.利用规则创建一个正则表达式对象
        let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive)
        
        // 3.匹配结果
        let results = regex.matchesInString(str, options: NSMatchingOptions(rawValue: 0), range: NSRange(location: 0, length: str.characters.count))
        
        for result in results {
            let chs = (str as NSString).substringWithRange(result.range)
            print(chs)
        }

上一篇下一篇

猜你喜欢

热点阅读