相关疑难解决方法(0)

NSRange到Range <String.Index>

我怎么能转换NSRangeRange<String.Index>斯威夫特?

我想使用以下UITextFieldDelegate方法:

    func textField(textField: UITextField!,
        shouldChangeCharactersInRange range: NSRange,
        replacementString string: String!) -> Bool {

textField.text.stringByReplacingCharactersInRange(???, withString: string)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

nsstring nsrange swift ios8

245
推荐指数
6
解决办法
11万
查看次数

来自Swift Range的NSRange?

问题:当我使用使用Range的Swift String时,NSAttributedString接受NSRange

let text = "Long paragraph saying something goes here!"
let textRange = text.startIndex..<text.endIndex
let attributedString = NSMutableAttributedString(string: text)

text.enumerateSubstringsInRange(textRange, options: NSStringEnumerationOptions.ByWords, { (substring, substringRange, enclosingRange, stop) -> () in

    if (substring == "saying") {
        attributedString.addAttribute(NSForegroundColorAttributeName, value: NSColor.redColor(), range: substringRange)
    }
})
Run Code Online (Sandbox Code Playgroud)

产生以下错误:

错误:'Range'不能转换为'NSRange'aligolsString.addAttribute(NSForegroundColorAttributeName,value:NSColor.redColor(),range:substringRange)

macos ios nsrange swift

153
推荐指数
8
解决办法
11万
查看次数

斯威夫特:点击UILabel文本的一部分

我有一个问题,"boundingRectForGlyphRange"总是返回CGRect.zero"0.0,0.0,0.0,0.0"."boundingRectForGlyphRange"无效.例如,我正在编写触摸UILabel功能的部分文本.我的文字的第一部分是"任何文字",第二部分是"阅读更多".我希望点击识别器仅在我触摸"READ MORE"时才能工作.如果我触摸UILabel上的任何一点,"CGRectContainsPoint"总是返回true,然后调用该动作

这是我的代码:

override func viewDidLoad() {
        super.viewDidLoad()

        // The full string

        let firstPart:NSMutableAttributedString = NSMutableAttributedString(string: "Lorem ipsum dolor set amit ", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(13)])
        firstPart.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(),
            range: NSRange(location: 0, length: firstPart.length))
        info.appendAttributedString(firstPart)

        // The "Read More" string that should be touchable
        let secondPart:NSMutableAttributedString = NSMutableAttributedString(string: "READ MORE", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(14)])
        secondPart.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(),
            range: NSRange(location: 0, length: secondPart.length))
        info.appendAttributedString(secondPart)

        lblTest.attributedText = info

        // Store range of chars we want to detect touches for
        moreStringRange = NSMakeRange(firstPart.length, secondPart.length)
        print("moreStringRange\(moreStringRange)") …
Run Code Online (Sandbox Code Playgroud)

nslayoutmanager nsattributedstring uilabel ios swift

11
推荐指数
7
解决办法
2万
查看次数