swift筆記(七)

1.圖文混排

// 需求:點(diǎn)贊[點(diǎn)贊]請(qǐng)多多支持
        // 1.創(chuàng)建'點(diǎn)贊'屬性字符串
        let attrStr1 = NSAttributedString(string: "點(diǎn)贊")
        
        // 2.創(chuàng)建顯示[點(diǎn)贊]圖片的屬性字符串
        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.創(chuàng)建'請(qǐng)多多支持'屬性字符串
        let attrStr3 = NSAttributedString(string: "請(qǐng)多多支持")
        
        // 4.拼接三個(gè)屬性字符串
        let attrMStr = NSMutableAttributedString()
        attrMStr.appendAttributedString(attrStr1)
        attrMStr.appendAttributedString(attrStr2)
        attrMStr.appendAttributedString(attrStr3)
        
        // 5.顯示demoLabel
        self.demoLabel.attributedText = attrMStr




// 需求:小碼哥:www.520it.com
    // 小碼哥:顯示紅色
    // www.520it.com 顯示藍(lán)色
    // 1.設(shè)置demoLabel文字顏色為紅色
    self.demoLabel.textColor = UIColor.redColor()
    
    // 2.創(chuàng)建屬性字符串
    let attrString = NSAttributedString(string: "www.520it.com", attributes: [NSForegroundColorAttributeName : UIColor.blueColor(), NSFontAttributeName : UIFont.systemFontOfSize(12)])
    
    // 3.創(chuàng)建可變的屬性字符串
    let attrMString = NSMutableAttributedString(string: "小碼哥:", attributes: [NSFontAttributeName : UIFont.systemFontOfSize(30)])
    attrMString.appendAttributedString(attrString)
    
    // 4.顯示字符串
    self.demoLabel.attributedText = attrMString

2.正則表達(dá)式

/*
    練習(xí):
    練習(xí)1:匹配abc
    練習(xí)2:包含一個(gè)寫a~z,后面必須是0~9 -->[a-z][0-9]或者[a-z]\d
    練習(xí)3:必須第一個(gè)是字母,第二個(gè)是數(shù)字 -->^[a-z][0-9]$
    練習(xí)4:必須第一個(gè)是字母,字母后面跟上4~9個(gè)數(shù)字
    練習(xí)5:不能是數(shù)字[^0-9]

    練習(xí)6:QQ匹配:^[1-9]\\d{4,11}$
    1.都是數(shù)字
    2.5~12位

    練習(xí)7:手機(jī)號(hào)碼匹配^1[3578]\\d{9}$
    1.以13/15/17/18
    2.長(zhǎng)度是11
 */

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let str = "17334332342"
        
        matchPhone(str)
    }
    
    func matchPhone(strPhoneNum : String) {
        // 聯(lián)系二:匹配點(diǎn)好號(hào)碼
        let pattern = "^1[3578][0-9]{9}$"
        
        // 查看是否匹配
        matchString(strPhoneNum, pattern: pattern)
    }
    
    func matchQQ(strQQ : String) {
        // 練習(xí)一:匹配QQ號(hào)碼 d324234
        // 1.全部是數(shù)字
        // 2.5~14位置
        // 3.開頭不能是0
        let pattern = "^[1-9]\\d{4,13}"
        
        // 4.通過(guò)規(guī)則創(chuàng)建正則表達(dá)式對(duì)象
        matchString(strQQ, pattern: pattern)
    }
    
    func matchString(str : String, pattern : String) {
        // 1.通過(guò)規(guī)則創(chuàng)建正則表達(dá)式對(duì)象
        let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive)
        
        // 2.匹配QQ號(hào)碼
        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.定義規(guī)則
        // [a-z] : 匹配字母
        // [0-9]或\d : 匹配數(shù)字
        // {1,10} : 有1位到10
        // {0,} : 有0,或者無(wú)限多位
        // . : 任意的符號(hào)(除了換行符)
        // ^ : 1.寫在^[]外面,必須以某規(guī)則開頭 2.寫在[^]規(guī)則的取反
        // $ : 必須以某規(guī)則結(jié)尾
        // *? : 表示有1位或者多少,但是一旦復(fù)合規(guī)則,就會(huì)直接返回
        let pattern = "^[^a-z].*?"
        
        // 2.通過(guò)規(guī)則,創(chuàng)建正則表達(dá)式對(duì)象
        let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive)
        
        // 3.利用對(duì)象匹配字符串
        /*
        // 匹配一個(gè)字符串,并且將匹配結(jié)果以數(shù)組的形式返回
        // 參數(shù)一:要匹配的字符串
        // 參數(shù)二:0
        // 參數(shù)三:要匹配該字符串中的哪一個(gè)返回
        public func matchesInString(string: String, options: NSMatchingOptions, range: NSRange) -> [NSTextCheckingResult]
        
        // 匹配一個(gè)字符串,將匹配結(jié)果的個(gè)數(shù)返回
        public func numberOfMatchesInString(string: String, options: NSMatchingOptions, range: NSRange) -> Int
        
        // 匹配一個(gè)字符串,將第一個(gè)結(jié)果返回
        public func firstMatchInString(string: String, options: NSMatchingOptions, range: NSRange) -> NSTextCheckingResult?
        
        // 匹配一個(gè)字符串,將第一個(gè)結(jié)果的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:【動(dòng)物尖叫合輯】#肥豬流#貓頭鷹這么尖叫[偷笑]、@南哥: 老鼠這么尖叫、兔子這么尖叫[吃驚]、@花滿樓: 莫名奇#小籠包#妙的笑到最后[好愛(ài)哦]!~ http://t.cn/zYBuKZ8/"
        
        // 1.創(chuàng)建匹配規(guī)則
        // 1.1.表情的規(guī)則
        // let pattern = "\\[.*?\\]"
        
        // 1.2.匹配@名字:
        // let pattern = "@.*?:"
        
        // 1.3.匹配話題 #....#
        // let pattern = "#.*?#"
        
        // 1.4.匹配URL
        let pattern = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?"
        
        // 2.利用規(guī)則創(chuàng)建一個(gè)正則表達(dá)式對(duì)象
        let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive)
        
        // 3.匹配結(jié)果
        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)
        }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容