在backword中搜索attributionString中的单词并获取范围Swift

时间:2015-08-15 07:39:18

标签: swift nsattributedstring nsrange

如何从字符串末尾向后搜索中的空格“ attributedString ”,然后获取该空格的范围?

我从一个rtf文件中获取一个属性字符串,然后将其划分为多个属性字符串,以便我可以在屏幕上显示这些字符串。

但是当我检索属性字符串时,例如在500范围内,子字符串的最后一个字是不完整的。

以下是 AppDelegate 类代码:

//  AppDelegate.swift

struct ScreenSize
{
static let SCREEN_WIDTH         = UIScreen.mainScreen().bounds.size.width
static let SCREEN_HEIGHT        = UIScreen.mainScreen().bounds.size.height
static let SCREEN_MAX_LENGTH    = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
static let SCREEN_MIN_LENGTH    = min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
}
struct DeviceType
{
static let IS_IPHONE_4_OR_LESS  = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
static let IS_IPHONE_5          = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0
static let IS_IPHONE_6          = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0
static let IS_IPHONE_6P         = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0
static let IS_IPAD              = UIDevice.currentDevice().userInterfaceIdiom == .Pad && ScreenSize.SCREEN_MAX_LENGTH == 1024.0
}
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var pageText:[NSAttributedString] = []
var startIndex = 0
var endIndex = 500
var divideFactor = Int()

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    if DeviceType.IS_IPHONE_4_OR_LESS
    {
    endIndex = 700
    }
    else if DeviceType.IS_IPHONE_5
    {
    endIndex = 900
    }
    else if DeviceType.IS_IPHONE_6
    {
    endIndex = 1300
    }
    else if DeviceType.IS_IPHONE_6P
    {
    endIndex = 1700
    }
    else
    {
    endIndex = 2000
    }



    UINavigationBar.appearance().barTintColor = UIColor(red: 144.0/255, green:  14.0/255, blue: 0/255, alpha: 1.0)
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
    UIBarButtonItem.appearance().tintColor = UIColor.whiteColor()
    UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)

    for var i=1 ; i<=1 ; i++ {
        if let rtfPath = NSBundle.mainBundle().URLForResource("Quran2", withExtension: "rtf") {
            let attributedStringWithRtf = NSMutableAttributedString(fileURL: rtfPath, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil, error: nil)!

            var lengthOfRtf = attributedStringWithRtf.length
            divideFactor = Int(lengthOfRtf/endIndex)
            println(divideFactor)
            self.updateTextFont(attributedStringWithRtf) (valueFactor: divideFactor)

        }
    }

    return true
}
func updateTextFont(mystring:NSMutableAttributedString) (valueFactor:Int) {

    let screenSizeMain: CGRect = UIScreen.mainScreen().bounds
    let myAttriText:NSMutableAttributedString = mystring.mutableCopy() as! NSMutableAttributedString
    myAttriText.beginEditing()
    myAttriText.enumerateAttributesInRange(NSMakeRange(0, myAttriText.length), options: NSAttributedStringEnumerationOptions.Reverse) { (attribute, range, stop) -> Void in

        var mutableAttributes = NSDictionary(dictionary: attribute)
        var font:UIFont = mutableAttributes.objectForKey(NSFontAttributeName) as! UIFont
        var newFont:UIFont = font.fontWithSize(font.pointSize)

        if DeviceType.IS_IPAD
        {
            newFont = font.fontWithSize(font.pointSize+7)

        }
        var fontProperties:UIFontDescriptor = font.fontDescriptor()
        let sizeNumber:Float = fontProperties.fontAttributes()[UIFontDescriptorSizeAttribute] as! Float
        myAttriText.addAttribute(NSFontAttributeName, value: newFont, range: range)

    }

    for var i=0; i < valueFactor; i++ {

        let range =  NSMakeRange(startIndex, endIndex)
        var nsText = myAttriText.attributedSubstringFromRange(range)
        pageText.append(nsText)
        println(endIndex)

    }

    myAttriText.endEditing()

}

func visibleRangeOfTextView(textView: NSAttributedString) -> NSRange {
    return NSMakeRange(startIndex, endIndex)

}

func applicationWillResignActive(application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


}

1 个答案:

答案 0 :(得分:0)

已编辑的版本(更新问题后)

获取最终空格范围以编写String扩展名的一种方法。

extension String {

  func getMatches(regex: String, options: NSStringCompareOptions?) -> [Range<String.Index>] {
    var arr = [Range<String.Index>]()
    var rang = Range(start: self.startIndex, end: self.endIndex)
    var foundRange:Range<String.Index>?

    repeat
    {
        foundRange = self.rangeOfString(regex, options: options ?? [], range: rang, locale: nil)

        if let a = foundRange {
            arr.append(a)
            rang.startIndex = foundRange!.endIndex
        }
    }
        while foundRange != nil
    return  arr
   }

}

然后可以按如下方式实施:

let attrStr = NSAttributedString(string: "Hello, playground. How are you, today?")
// obtain range of final space
let ranges = attrStr.string.getMatches(" ", options: [])
let rangeOfFinalSpace = ranges[advance(ranges.endIndex, -2, ranges.startIndex)]

其他方法

这是一个匆忙的回复,代码可能会被进一步剥离。另外值得考虑的是将NSCharacterSet用于空格和换行符和/或NSScanner。在从字符串的最终索引的长度中减去该索引之前,反转字符串并获取空格的索引也可以优化事物。

注意:代码是用Swift 2(Xcode 7 beta 5)编写的