gpt4 book ai didi

swift - 从后台搜索 attributeString 中的单词并获取范围 Swift

转载 作者:行者123 更新时间:2023-11-30 14:08:41 24 4
gpt4 key购买 nike

如何从字符串末尾向后搜索 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:.
}


}

最佳答案

编辑版本(问题更新后)

获取最终空格范围以编写字符串扩展的一种方法。

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) 编写的

关于swift - 从后台搜索 attributeString 中的单词并获取范围 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32022593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com